This is a bit of an odd one and apologies if I don\'t explain it very well.
I am using the following simple code to send messages that I have popped off a queue, usi
TCP is a stream protocol. You cannot assume that the 'packets' you Send() are going to be delivered and received in tact. At the transmitting end, the Nagle algorithm tries to combine the data that was written in separate Send() calls to optimize delivery of the data. At the receiving end you'll Read() what is stored in the TCP buffer. If will be a combination of the transmitted packets if there's any delay. This is further complicated by the routers in between the transmitter and receiver, they are allowed to fragment an IP packet, turning a large one into multiple small ones, to accommodate the transmission channel's maximum packet size (MTU).
In other words, there is no reliable way to ensure that the packets are delivered the way they were sent. A simple workaround for this is first transmitting the size of the packet. At the receiving end you first read that size, then know how to count off the received bytes to reconstruct the packet.
My mistake, appears that it was an issue with a blocking read in the end.
I am not sure if this is your problem and I am working off of a vague memory from yesteryear so please excuse the vagueness.
I seem to remember that Socket.Select()
would return indicating it has zero data to read if the stream has an error. You may want to try passing a copy of the stream list in to the error list parameter and see if you get any in error.
Your code appears to be sending to the same socket regardless of what is in writelist
. I would modify it to operate on the writelist
even if you only have a single socket. Otherwise you'll be attempting to send to a socket even if it has not necessarily indicated it is ready for data yet (i.e. if the Socket.Select()
is returning for some other reason, such as my hunch above). This may cause the write operation to block and may be the cause of the delay you are witnessing if you are operating with more than one socket.
Finally, you can break out of your readyToWrite
loop as soon as the flag is set. Better still you can recode this as:
bool readyToWrite = writelist.Any();
But I would still suggest you replace this with a foreach
loop on writelist
:
foreach (Socket sock in writelist)
{
// do stuff
}
That's nicely odd. Please consider using WireShark to trace the messages and see where they're going. Unless I'm missing something obvious, I find it difficult to understand why the messages believe that they were received but really haven't. Could the client be waiting?
try int.MaxValue instead of 120000000
What does your client code look like?
Normally, I'd say that since you are running you are only sending messages locally the behavior you are seeing is almost definitely caused by Nagle's Algorithm, but setting the NoDelay property disables that.