Link to home
Start Free TrialLog in
Avatar of Yasir_Malik
Yasir_Malik

asked on

OnReceive() event question

Hello,
Is an OnReceive() event fired for each message received or is it fired once for a set of messages received? For example, let's say that ten messages are sent by the server and OnReceive() fires. Should I keep on doing multiple Receive()s? If so, how do I know when to stop reading (in Unix recv() blocks)?
Or are there 10 OnReceive()s fired?
Thanks.
Avatar of Jaime Olivares
Jaime Olivares
Flag of Peru image

10 OnReceive events are fired.
Avatar of Yasir_Malik
Yasir_Malik

ASKER

Ok.  Why is it that sometimes the messages sent to my client application get "lost" even though Ethereal it says my PC has received data.  The problem I am having is that because of Nagle's algorithm, send()s I do on the server are being clumped together in one packet.  Each message I send from the server has a four byte header with the first two bytes indicating the size of the message not including the header.  Here's my code:
CArchive ar_in;

void MySocket::OnRecieve(int error_code)
{
 uchar header[4];
 uchar *data;
 ushort size;

 do
 {
  ar_in.Read(header, sizeof(header));
  size = (header[0]<<8) | header[1];

  data = new uchar[size];
  ar_in.Read(data, size);
  send_off(header, data);
 } while(!ar_in.isBufferEmpty());
}
What kind of frames are you sending? TCP or UDP?
UDP are not guaranteed to arrive successfully, so usually developers implements an error checking and correction protocol.
Also, will be useful to implement a "start of frame" and "end of frame" control characters, besides the header, this will prevent you to get confused even in case 2 frame are mixed in just 1 OnReceive.
I'm using TCP.   According to Ethereal, I'm getting everything.  But is there something wrong with my code?  It's pretty straight forward.  I read four bytes, and based on the first two bytes, I read more bytes.  Does CArchive::Read() cause issues if you do more than one in an OnReceive() event?  However, sometimes, my program works fine.
I don't understand how using an eof of frame would help, though.
Thanks.
Sometimes I don't get any events fired.  Is there a reason for this?  Is there a different way to receive messages from a socket?
I'm convinced that Windows is playing tricks with me!  Each time OnReceive is fired, I log output to a file.  When I received three messages in three separate packets from the server, I only get two events fired.  This driving me insane.  Is there any reason why this would happen?  Sometimes everything works fine.
Have a look to MSDN example about using OnReceive event:
http://msdn2.microsoft.com/en-us/library/wxzt95kb.aspx
Notice base class' OnReceive() function is called and IOCtrl() function is used to determine if there is pending data. Also try to implement a Start of Frame and End of Frame characters, in case 2 frames are mixed in just 1 OnReceive() event.
I'll saw that but I thought it didn't apply to me because my app does not hang and I can still send and receive stuff over a socket even though I don't get all the events fired.  I guess it wouldn't hurt to try.
What is the purpose of doing CSocket::OnReceive(nerrorcode) in the OnReceive function?
That doesn't do anything.  The problem is that my OnReceive event is not being fired.  Sometimes the events are fired fine.  However, after I run into the problem of the events not being fired for the first time, I never get events fired again after that.
After I get the message, I send the message off to a bunch of threads.  I notify the threads of a message by firing an event for the thread using CEvent::SetEvent.  Could this cause any problems for the other events?
For "dwExpected" I chose 8 because that's the minimum size message I can receive from the server.
Figured out what the problem was:  you can't do two Receive()s on one OnReceive() event.  MS has documented that as well.  All though MS only mentions Receive(), I guess CArchive::Read() produces the same results.
I was doing one Receive() before as well, but I would sometimes get only two events fired even though I sent three messages.  The problem was that messages were being coalesced into one packet because of Nagle's algorithm.  I wrote an algorithm to handle that and partial sends as well.
And all of this just in time for the demo.
Please refund.
ASKER CERTIFIED SOLUTION
Avatar of CetusMOD
CetusMOD
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial