Sending an file as in a stream

  • Thread starter Thread starter help
  • Start date Start date
H

help

I wonder how you can send a file in a stream using the
tcp/ip protocoll?
 
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top