I want to know when we receive all bytes from a socket without Socket.Disconnect() method?
Yet , I use this code to receive all bytes , but i use Socket.Disconnect()
While the socket is open, there is no way of doing this without causing it to block when it reaches the end (expecting more data).
There are several ways of doing it:
0
being a common choice; tricky when sending arbitrary binary data, though)If it was a NetworkStream
, for example, using a length prefix:
int expecting = //TODO: read header in your chosen form
int bytesRead;
while(expecting > 0 && (bytesRead = stream.Read(buffer, 0,
Math.Min(expecting, buffer.Length))) > 0)
{
// TODO: do something with the newly buffered data
expecting -= bytesRead;
}