|
Hopefully somebody can help me with this problem. I am fairly new with socket connection. I know you guys will want to see some code but unfortunately this is hard to do as my code is on a classified system. I would prefer not to have to type all the necessary code. However if I have to I will.
I have a server-client socket connect and I use CSocket. Once the connection is made, the server sends 168-byte message to the client once per second. In the client OnReceive function I read the data and then write it to a file. In the OnIdle function of the client I also check if the connection with the server is still up by sending a one-byte message to the server. My problem is that it works for a while and then the client OnReceive stops being called by the framework.
Using debugging I know for a fact that the server is still sending the 168-byte messages every seconds without any errors. I also know that the client is still sending the one-byte message to the server and the server is still receiving it. Both programs seem to be working just fine except for the fact that the OnReceive is no longer being called and therefore I am no longer writing data to the file.
Anybody can help me? Thanks!
BTW it never stops at the same place. I can sometimes receive 2K but other times I can receive a lot more. The most I got was 94,168 bytes.
-- modified at 13:41 Tuesday 6th February, 2007
jpyp
|
|
|
|
|
Hmmm sockets have been around for 24 years and you're using CSocket - classified socket code?
Just teasin.
Anyway, is the protocol TCP?
If you are using OnIdle(), what are you returning from it?
Does it perform better if you move the mouse around occasionally?
Mark
|
|
|
|
|
One other question - Are you pausing your app's main (user interface) thread anywhere, with
Sleep() or a loop? This will prevent the necessary message handling by CSocket which it relies
on to make the OnReceive() call.
Mark
|
|
|
|
|
|
Cool! That's a good one
When you go to higher tranfer rates I'd recommend using CAsyncSocket and/or Sockets APIs and using
WSAEventSelect instead of WSAAsyncSelect. Much better performance and you have complete control
over the events. You can either
a) recv() until there's no more data and go back to waiting for an FD_READ event, which will
occur when more data is in the socket's receive buffer. Or...
b) recv() a certain amount of bytes and go back to waiting for an FD_READ event. If more bytes
are available for receiving then the FD_READ event is set immediately after the last recv() call.
Thank you for the update!
Mark
|
|
|
|
|
The basic advice is that you should make exactly one single call to Receive (which calls the underlying winsock recv() function) from inside your OnReceive function (which the MFC framework calls in response to an FD_READ notification).
This advice can't be found in the MFC documentation, either for CSocket or for CAsnycSocket.
It's found, however, in the documentation for the underlying Winsock functions. For example, see the documentation for WSAAsyncSelect() (which is the Winsock function that causes Windows-style messages to be posted to your application from the network stack) at http://msdn2.microsoft.com/en-us/library/ms741540.aspx[^], which includes this quote:
MSDN at WSAASyncSelect wrote: With these semantics, an application need not read all available data in response to an FD_READ message — a single recv in response to each FD_READ message is appropriate. If an application issues multiple recv calls in response to a single FD_READ, it can receive multiple FD_READ messages. Such an application can require disabling FD_READ messages before starting the recv calls by calling WSAAsyncSelect with the FD_READ event not set.
The point of this is to suggest that you get closer to the Winsock API when you start programming for high-rate transfers. There a a few dozen of these "gotcha's" that you don't learn/know about when using the MFC classes, particularly the CSocket class. Personally, I would avoid CSocket. CAsyncSocket is fine, since it's reasonably close to the Winsock API, but you still need to understand the underlying API if you expect your program to work correctly.
Mike
|
|
|
|
|
Thanks a lot to both of you for your advice. I will definitely take them under consideration when I move to high-rate transfers.
jpyp
|
|
|
|
|
Is it possible somehow to derive several classes from a singleton class? Or would it be better to just create a global singleton and create an instance whenever required?
I want to create some sort of logger class, which would write the errors/logs to a file and optionaly display a dialog. The obvious place for such a class would be the top of the heirarchy. But in my case some classes are derived from multiple others.
|
|
|
|
|
I don't see exactly what you are trying to do... What is the exact 'scheme' that you are trying to get ?
Could you explain a little bit more in detail ? (Maybe with some code example).
|
|
|
|
|
It goes like this. I have a rather large collection of classes, all intertwined with each other. There is only one class at the bottom of the hierarchy, but it branches out towards the top. Now an error could happen anywhere within this structure. When one does happen I need to be able to log it to a file, and/or display it to the user. Currently All my error codes are HRESULTs and I have created a simple macro to pop-up a dialog box thinking I would just change the macro when it comes to dealing with the errors in the proper way.
I don't want to have to open a file, seek and write everytime I need to write to it; so I'm thinking of creating a static class which would map the file to memory and keep it open for the duration of the process. Obviously it would need to be a singleton due to the number of classes which need to access the file. But I can't think where to place such a class in this hierarchy.
|
|
|
|
|
I still don't understand fully your problem. Why don't you have a logger class which is completely 'external' to your class hierarchy. This class is simply a singleton and you can initialize it once at the startup of your program. Then, each time you need it, you get the singleton instance and call a function that log text.
BTW, as we speak about singleton, I suppose that you are refering to the singleton design pattern right (and not just a simple global instance) ? So, in that case, you really don't need to 'place' your class somewhere.
|
|
|
|
|
Cedric Moonen wrote: I suppose that you are refering to the singleton design pattern right
Yes I am.
I was just thinking the same as what you suggested. I was worrying about having an open handle to a file, but I wasn't thinking that with a singleton the c'tor and d'tor are only called once. I could even implement reference counting.
Why ain't I thinking straight tonight...
|
|
|
|
|
WalderMort wrote: I was worrying about having an open handle to a fil
Well, you don't need. If you provide the filename of your file at the startup of your program, you can still keep it as a data member of your class and then reopen the file (in append mode) to log the new message.
(If you are interested, I can post a very simple singleton logger class that I did).
|
|
|
|
|
Cedric Moonen wrote: (If you are interested, I can post a very simple singleton logger class that I did).
Thanks for the offer, but I would prefer to have a dab at it myself.
Cedric Moonen wrote: reopen the file (in append mode)
I was thinking more along the lines of mapping the file to memory. Due to the nature of the program, I would have to divide the file into sections ( OS version, DLL versions, Errors, Bank account numbers.. ) anyway you get the meaning.
|
|
|
|
|
I have a program that takes automatically reads points from a device connected through USB. The program will take these points and store it in a comma seperated Excel file. What I want to be able to do is graph these points using Excel from C++. I'm guessing the best way to do this is to make a macro that graphs the points and then making the C++ program call the macro. But I have no idea how to do this and I'm actually completely new to it. I do know how to make the macro in Excel. But how do I call the macro from C++??? I"m using Win32 API. Does anyone have experience with this?
Thank You!
-- modified at 16:16 Tuesday 6th February, 2007
-- modified at 16:18 Tuesday 6th February, 2007
------------------------
Impossible is Nothing
|
|
|
|
|
I want to insert a large binary data in to database( SQL server 2000 ) using VC6.
First I tried with RFX_Binary, with CByteArray but in that case I was able to insert only 8000 byte of data. After that data trunctaion error occures.
Then I tried with CLongBinary but in MFC RBX_LongBinary don't support. As I got assert at
<br />
void AFXAPI RFX_LongBinary(CFieldExchange* pFX, LPCTSTR szName,<br />
CLongBinary& value)<br />
{<br />
:<br />
:<br />
:<br />
case CFieldExchange::BindParam:<br />
ASSERT(FALSE);<br />
:<br />
:<br />
:<br />
}<br />
Please suggest any method to try.
Regards
Anil
|
|
|
|
|
|
in SQL table its image type.
When I write SQL statement in SQL manager I am able to insert more than 8K.
Its in the MFC side giving data truncation exception.
Regards
Anil
Regards
Anil
|
|
|
|
|
What type of field is the CByteArray object mapped to in the SQL table? The char, varchar, binary and varbinary types are limited to 8,000 bytes.
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
in SQL table its image type.
When I write SQL statement in SQL manager I am able to insert more than 8K.
Its in the MFC side giving data truncation exception.
Regards
Anil
|
|
|
|
|
What does your RFX_Binary() statement look like?
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
RFX_Binary(pFX, _T("[@File]"), mFileParam, 800000);
here I have given size more that 8000
Regards
Anil
|
|
|
|
|
i have to open an xml file in txt format. ie we would normally do right click open file with note pad no. similarly i have to open a file named system.xml as system.txt programmatically and then i have to read the file. does anyone having code for this. this should be done in c programming. help me. thanx in advance
Arise Awake Stop Not Till ur Goal is Reached.
|
|
|
|
|
|
i tried fopen. but this normally used to open a file in a specified format na. i need to open an already created xml file as text file in my program and then i have to read the contents. i think fopen cant be used.
|
|
|
|