Sending an file as in a stream

  • Thread starter Thread starter help
  • Start date Start date
William Ryan said:

A few dodgy bits of code in there. (I know you're not responsible, but
I'm pointing them out for the sake of posterity.)
byte[] recs = new byte[32767];
int rcount = sock.Receive(recs,recs.Length,0) ;
string clientmessage = System.Text.Encoding.ASCII.GetString(recs) ;
clientmessage = clientmessage.Substring(0,rcount);

Why not just use

string clientmessage = Encoding.ASCII.GetString (recs, 0, rcount);

?

There's also the dodginess of assuming that the whole command will come
in a single packet which worries me.

Another dodgy bit:
Byte[] sender = new Byte[1024];
sender = new Byte[1024];
sender = Encoding.ASCII.GetBytes(cmd);

Why allocate two blocks of 1K and then ignore them? (There are similar
bits elsewhere, and the use of variables declared further out than they
need to be.)

I'd personally use a TcpClient, which makes things a little bit easier.
It's important to remember that it's all a stream though, so don't
expect things to come in neatly formed amounts.

Basically, the ideas are mostly okay in the page linked above, but I'd
advise anyone to look through the code *very* carefully before adopting
any of it.
 
Back
Top