1

In a case i have a stream and i want to copy the content into a network stream i do somthing like this.

  NetworkStream ns=new NetworkStream(Sck);//sck is a socket of course.
 stream.CopyTo(ns);

Then,in the other side im trying to get the content:

MemoryStream stream = new MemoryStream();
            ns.CopyTo(stream);

But nothing actually happend.. the program stoppes after the ns.copy line.. nothing actually going on... Any idea why is this? i aleardy have algorithm to read the data but i just want to send the stream over a networkstream..

1 Answers1

0

The receiver must know when all data is received. Here, you can do this by shutting down the socket for sending on the server. Easiest way to do that is to call Shutdown, then Close or Dispose. That way the client will complete copying the stream.

usr
  • 162,013
  • 33
  • 219
  • 345
  • all right thanks. And If I would Iike to send data constantly? –  Aug 04 '15 at 20:33
  • Then you cannot use CopyTo on the receiver because it always drains the stream. You have to devise an application protocol that allows the receiver to tell messages apart. Advice: Do not use sockets. Use a higher level protocol that handles all of that. What about WCF? HTTP? WebSockets also. – usr Aug 04 '15 at 20:34
  • allright i guess youre right. :) look im devloping a screen sharing application and it has to send data very fast(the data size can be between 3kb and up to 200kb!) so i do need a fast ,efficient and simple protocol. wouldnt networkstream do this work? why do you reccomend wfc or http? thanks in advance i truly appreciate your help :) @usr –  Aug 05 '15 at 07:46