473,397 Members | 2,028 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

BinaryReader Not Completely Reading NetworkSocket

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

Jul 21 '05 #1
2 2398
Chris P. <c@c.com> wrote:
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.


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
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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #2
That was it. Thanks for your help.

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Chris P. <c@c.com> wrote:
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.


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
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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Jul 21 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

29
by: | last post by:
Hi, I am trying to optimize the reading of a huge binary file into a byte... I am doing the following.. byte ba = new byte;
1
by: gladiator | last post by:
Hello MVPs: I am not sure when to use binaryreader in .net is best? In fact ,I am coding a program which converts data in text files into DB. but i have to use fixed length to read data in...
14
by: Laszlo Szijarto | last post by:
Can BinaryReader be forced to read a stream, say a TCP/IP stream or memory stream or even file stream in big endian order or do I have to write something custom to reverse the byte order? So, for...
2
by: Bob Rock | last post by:
I already found an alternative way to accomplish this (using ReadBytes), still I'd like to understand why I'm getting and error reading a text file using the following method. The exception is...
3
by: Kevin Trojanowski | last post by:
I'm about to rip out what little hair I have left; I have a class that uses a BinaryReader, and the data is getting munged. A subset of the code follows; I've removed the parts that aren't...
6
by: Question with BinaryReader | last post by:
I use BinaryReader to read my binary dafa files, when i call ReadBytes, why it always return more 4 bytes. The following is my code. FileStream fs = new FileStream(file, FileMode.OpenOrCreate,...
1
by: Vitaly | last post by:
// Open input file and create the BinaryReader. br = new BinaryReader(new FileStream("Test.dat", FileMode.Open, FileAccess.Read)); // Read binary data. d = br.ReadDouble(); A question is...
0
by: Dave | last post by:
I'm reading chars from a file and displaying them in a label on a form. Following is a listing of the code section. Could anyone tell me the best way to do this? What I have works but wanted to...
2
by: Chris P. | last post by:
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.