Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I use StreamReader/Writer with NetworkStream for C# server/client?

I'm in the process of building a client/server based game for a project(feeling quite out my depth). I've been using TCPclient and a multi-threaded socket server. Everything is working fine at the moment but I have been using StreamReader and StreamWriter to communicate between both client and server.

I keep seeing examples like this for recieving data:

byte[] data = new byte[1024];
int recv = sock.ReceiveFrom(data, ref ep);
string stringData = Encoding.ASCII.GetString(data, 0, recv);

and this for sending:

byte[] data = Encoding.ASCII.GetBytes("This is a test message");
server.SendTo(data, iep);

I was wondering what are the benefits of using this over streamReader? Would using this also be buffering?

Thanks in advance.

like image 921
SlowForce Avatar asked Sep 11 '26 01:09

SlowForce


1 Answers

It's just a different style. ReceiveFrom and SendTo are more Unix-style, and using a StreamReader is more of a .NET style. StreamReader would be of more use to you, as you could then pass it to other methods that accept a TextReader.

like image 159
John Saunders Avatar answered Sep 12 '26 14:09

John Saunders



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!