[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 503
  • Last Modified:

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.
0
Yasir_Malik
Asked:
Yasir_Malik
  • 10
  • 3
1 Solution
 
Jaime OlivaresCommented:
10 OnReceive events are fired.
0
 
Yasir_MalikAuthor Commented:
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());
}
0
 
Jaime OlivaresCommented:
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.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Yasir_MalikAuthor Commented:
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.
0
 
Yasir_MalikAuthor Commented:
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?
0
 
Yasir_MalikAuthor Commented:
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.
0
 
Jaime OlivaresCommented:
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.
0
 
Yasir_MalikAuthor Commented:
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.
0
 
Yasir_MalikAuthor Commented:
What is the purpose of doing CSocket::OnReceive(nerrorcode) in the OnReceive function?
0
 
Yasir_MalikAuthor Commented:
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?
0
 
Yasir_MalikAuthor Commented:
For "dwExpected" I chose 8 because that's the minimum size message I can receive from the server.
0
 
Yasir_MalikAuthor Commented:
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.
0
 
Yasir_MalikAuthor Commented:
Please refund.
0
 
CetusMODCommented:
PAQed with points refunded (100)

CetusMOD
Community Support Moderator
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 10
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now