BinaryReader Not Completely Reading NetworkSocket

C

Chris P.

I have a C# application that connects to Perl application on a UNIX server.
I am able to connect and communicate both directions with the server, but
there are several occasions when it appears that the BinaryReader only reads
part of the message sent by the server.

Here is the main flow of my application.
1. Open Socket
2. Read 4 byte integer
3. Read byte array that is the size of the integer in step 2.
4. Put message in collection
5. Repeat Steps 2 through 5

Simple enough? Where my problem occurs, is that sometimes step 3 reads fewer
bytes than the integer that was read in step 2. For example, the integer in
step 2 was 3564, but the string that was parsed was only 2756 bytes long.
When we get back to the top of the loop, the integer in step 2 is
1014391148, which if you break it down byte by byte is the next 4 characters
in the string. The app crashes because it "lost it's place." I thought by
setting the socket as Blocking it would wait until it read the number of
bytes in the array (This is how I've done similar things in Java), but it
hasn't seemed to help. Also, I have tried to add Thread.Sleep(750) between
step 2 and step 3. That seems to prevent most errors from occurring, but I
am not happy with that being a solution.

Any suggestions?


Here is the way that I am declaring my socket and readers:

socket = new Socket(AddressFamily.InterNetwork,
SocketType.Stream,ProtocolType.Tcp);
socket.Blocking = true;
socket.Connect(endPoint);
socket.SetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.ReceiveTimeout, 0);
socket.SetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.SendTimeout, 0);
stream = new NetworkStream(socket, FileAccess.ReadWrite, true);
reader = new BinaryReader(stream);
writer = new BinaryWriter(stream);

Here is my main loop:

while (!_stop)
{
msgLength = new byte[4];
if (reader.Read(msgLength,0,4) > 0)
{
// This stops 90% of errors, but is bad practice.
// Thread.Sleep(750);

message = new byte[length];
if (reader.Read(message,0,(int)length) > 0)
{
serverMessage = enc.GetString(message,0,(int)length);
putReceiveMessage(serverMessage);
}
}
}
 
J

Jon Skeet [C# MVP]

Chris P. said:
I have a C# application that connects to Perl application on a UNIX server.
I am able to connect and communicate both directions with the server, but
there are several occasions when it appears that the BinaryReader only reads
part of the message sent by the server.

<snip>

Please see my response on another newsgroup you posted your question
on.
 
S

Sami Vaaraniemi

Chris P. said:
I have a C# application that connects to Perl application on a UNIX server.
I am able to connect and communicate both directions with the server, but
there are several occasions when it appears that the BinaryReader only reads
part of the message sent by the server.

Here is the main flow of my application.
1. Open Socket
2. Read 4 byte integer
3. Read byte array that is the size of the integer in step 2.
4. Put message in collection
5. Repeat Steps 2 through 5

Simple enough? Where my problem occurs, is that sometimes step 3 reads fewer
bytes than the integer that was read in step 2.

BinaryReader.Read documentation says:

"Return Value
The total number of characters read into the buffer. This might be less than
the number of characters requested if that many characters are not currently
available, or it might be zero if the end of the stream is reached."

In other words, to read N bytes from a socket, you will have to read in a
loop until you have all N bytes.

Regards,
Sami
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top