468,532 Members | 1,782 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,532 developers. It's quick & easy.

Performance issues with BinaryReader/BinaryWriter

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.
Nov 16 '05 #1
4 10841
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.

Nov 16 '05 #2
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" <mg*******@taxussi.no.com.spam.pl> wrote in message
news:ct**********@nemesis.news.tpi.pl...
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.


Nov 16 '05 #3
Paul Steele <pa*********@acadiau.ca> wrote:
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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Paul Steele <pa*********@acadiau.ca> wrote:
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.


D'oh! I guess I got thrown off the track when I found BinaryReader/Writer...
Nov 16 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Leon Jollans | last post: by
20 posts views Thread by Daniel Billingsley | last post: by
3 posts views Thread by liljester | last post: by
1 post views Thread by Claire | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.