When EndReceive returns null bytes

I am trying to get the best handle when using sockets asynchronously. According to this article http://msdn.microsoft.com/en-us/library/bew39x2a(v=VS.85).aspx I have to check the number of bytes returned by EndReceive and if it is zero I know I have all data, and if it is nonzero, there may or may not be more data. It makes sense, but when I call BeginReceive for the last time, it's often a few minutes before the callback function is called ... I'm guessing something should be timed out, but changing the Socket.ReceiveTimeout property doesn't seem to have any effect.

Is this the correct pattern to determine when I have received all the data? Especially when I don’t know the format of the message I receive?

0


source to share


2 answers


It depends on what you mean by "all data". Is the other end of the socket left? If not, you haven't really read all the data because the server could be sending more at any moment.



If the other end has closed the socket, then the callback should happen pretty quickly.

+3


source


Since TCP is a stream-oriented protocol, there are 3 general ways to know when a message has been completely received:



  • the length of the message is known (either because of the fixed length or because something in the protocol tells you how long it is)
  • there is a delimiter at the end of the post which you can check for
  • connection closed
0


source







All Articles