.NETGURU
BinaryReader Not Completely Reading NetworkSocket
Messages   Related Types
This message was discovered on microsoft.public.dotnet.general.
Responses highlighted in red are from those people who are likely to be able to contribute good, authoratitive information to this discussion. They include Microsoft employees, MVP's and others who IMHO contribute well to these kinds of discussions.
Post a new message to this list...

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);
}
}
}

Reply to this message...
 
    
Jon Skeet [C# MVP] (VIP)
Chris P. <Click here to reveal e-mail address> wrote:
[Original message clipped]

There's no guarantee that BinaryReader.Read will read as much as you
requested. The thing to do is loop round until you've read everything
you want to.

My own BinaryReader class, available from
http://www.pobox.com/~skeet/csharp/miscutil" target="_blank">http://www.pobox.com/~skeet/csharp/miscutil
keeps reading until either it's reached the end of the stream or it's
read as much as you requested, but I believe the normal
BinaryReader.Read is just like Stream.Read.

--
Jon Skeet - <Click here to reveal e-mail address>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Reply to this message...
 
    
Chris P.
That was it. Thanks for your help.

"Jon Skeet [C# MVP]" <Click here to reveal e-mail address> wrote in message
news:Click here to reveal e-mail address...
[Original message clipped]

Reply to this message...
 
 
System.IO.BinaryReader
System.IO.BinaryWriter
System.IO.FileAccess
System.IO.Stream
System.Net.Sockets.AddressFamily
System.Net.Sockets.NetworkStream
System.Net.Sockets.ProtocolType
System.Net.Sockets.Socket
System.Net.Sockets.SocketOptionLevel
System.Net.Sockets.SocketOptionName
System.Net.Sockets.SocketType
System.Threading.Thread




ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
Ad


Need Dot Net Interview Questions?
Ask ExamGuru, Inc. for advice and help on Passing .Net Interviews
.Net Projects
Best-of-breed application framework for .NET projects, developed by ExamGuru, Inc. and ExamGuru IT
Free .net Help
Commission ExamGuru, Inc. and his team for your next bespoke software project
FogBUGZ
The only bug tracking system carefully crafted with one goal in mind: helping teams create great software.
Awesome Tools
If you don't know about these, you're missing out... IT Certification Questions
IT Interview Questions
Free Oracle 10g Training
MCSE Boortcamp
Cisco Study Guides
Cheap Study Guides
Exact Questions
Dot Net Interview Questions
Oracle OCP
Cheap Travel
Designer Perfumes - Wholesale Prices
Free Programming Tutorials
 
ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
 Copyright © ExamGuru, Inc. 2001-2006
Contact Us - Terms of Use - Privacy Policy - www.dot-net-guru.com - www.examguru.net - www.oraclesource.net - www.itinterviews.net - www.examguru.net/ITCertification