My client-server application uses a basic implementation as provided by this MSDN article. However, the variation is that I am not using the <EOF>
type of delimiter because the way my application works is this:
- Server waits for client
- Client connects
- Server waits for clients to request something
- Client eventually requests something (like "Please send CV of Hans Passant in PDF format?")
- Server replies appropriately
- Server waits for clients to request something (i.e. goes back to Step 3 again)
This cycle continues until the connection is closed.
In reference to this SO question, is it possible, via any of the .NET implementations of receiving data from a socket (Socket.Receive, TCP.GetStream().Read(...)...etc.) is it possible that there will be a time when a Read or Receive operation will return a false 0
? (e.g. some data is read already, the next packet gets stucked in traffic temporarily and the Read/Receive operation does not know, so it just assumes there is nothing more to read and just returns zero).
My code below:
public void recieveClientData()
{
byte[] bs = new byte[1];// 'commStream' is NetworkStream of the socket
commStream.Read(bs, 0, 1);//blocks until something comes in
// Buffer for reading data
Byte[] bytes = new Byte[1024];
using (MemoryStream m = new MemoryStream())
{
//scoop what we have yet
m.Write(bs, 0, 1);
//get the remaining
int length;
while ((length = commStream.Read(bytes, 0, bytes.Length)) != 0)
{
m.Write(bytes, 0, length);
}
//At this point, 'm' is sent for asyncronously processing
...
//wait for another data from this client
recieveClientData();
}
}
Now at the line while ((length = commStream.Read(bytes, 0, bytes.Length)) != 0)
, let us assume that the client is sending the server a PDF file of about 32Mb (so the server can save it for future retrieval), is it possible that when the line commStream.Read(bytes, 0, bytes.Length)
is executing in a loop, a network lag is preventing that stream from having something to read, such that it returns 0
(meaning there is a network delay of some kind on a network packet but before that packet eventually makes it through and gets transported, commStream.Read
has already read 0
and returned)
Note that I can't use delimiters here because transmitted data is not always string.