Performance issues with BinaryReader/BinaryWriter

  • Thread starter Thread starter Paul Steele
  • Start date Start date
P

Paul Steele

I am writing a client/server application that communicates over tcp. The
code I use to initiate the connection is as follows:

NetworkStream networkStream = new NetworkStream(ClientSocket);
toClient = new BinaryWriter(networkStream);
fromClient = new BinaryReader(networkStream);

There is similar code on the client side. This set up works fine and
communication does occur. However, the communication is very slow. This is
especially noticable if I transfer larger amounts of data such as a file.

If I change the code around and use StreamReader/StreamWriter, performance
is significantly faster. I suspect it's some kind of buffering issue. Any
suggestions would be appreciated.
 
Hi Paul,

I'm not familiar with BinaryReader/Writer but if you're
saying that StreamReader/Writer is faster then you'll
need to think about using of that.

If you are writing (or reading) values one by one then
think about buffering them in the buffer (byte[]) and
write them in the pack.

HTH
Marcin
 
I initially used StreamReader/StreamWriter, but encountered problems with
unwanted character set conversions. I couldn't figure out how to use them to
send a stream of binary data that could not be altered by the receiver. Some
of the clients could be potentially using Chinese or other foreign
languages, and if I used stream.Write(buffer) on the sending side and then
read the data on the client side with stream.Read(buffer), certain
characters would be changed slightly. I am by no means an expert in Unicode
and the various conversions the .NET does automatically. I guess the
question I should ask is how can I set up a TCP stream that lets me tranfer
binary data without fear of implicit character conversion?

Marcin Grzêbski said:
Hi Paul,

I'm not familiar with BinaryReader/Writer but if you're
saying that StreamReader/Writer is faster then you'll
need to think about using of that.

If you are writing (or reading) values one by one then
think about buffering them in the buffer (byte[]) and
write them in the pack.

HTH
Marcin
I am writing a client/server application that communicates over tcp. The
code I use to initiate the connection is as follows:

NetworkStream networkStream = new NetworkStream(ClientSocket);
toClient = new BinaryWriter(networkStream);
fromClient = new BinaryReader(networkStream);

There is similar code on the client side. This set up works fine and
communication does occur. However, the communication is very slow. This
is especially noticable if I transfer larger amounts of data such as a
file.

If I change the code around and use StreamReader/StreamWriter,
performance is significantly faster. I suspect it's some kind of
buffering issue. Any suggestions would be appreciated.
 
Paul Steele said:
I initially used StreamReader/StreamWriter, but encountered problems with
unwanted character set conversions. I couldn't figure out how to use them to
send a stream of binary data that could not be altered by the receiver.

You don't. Readers/writers are specifically for character data, not
binary data. If you want binary data, just use a Stream.
 
Jon Skeet said:
You don't. Readers/writers are specifically for character data, not
binary data. If you want binary data, just use a Stream.

D'oh! I guess I got thrown off the track when I found BinaryReader/Writer...
 
Back
Top