Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In your server side code you are only calling <code>Read()</code> once, but you can't assume the data will be available when you call read. You have to continue reading in a loop until no more data is available. See the full example below.</p> <p>I have tried to reproduce your issue with the minimal amount of code and was not able to. The server prints out the clients message everytime. No special settings such as <code>NoDelay</code> and no explicit <code>Close()</code> or <code>Flush()</code>, just <code>Using</code> statements which ensures all resources are properly disposed. </p> <pre><code>class Program { static int port = 123; static string ip = "1.1.1.1"; static AutoResetEvent waitHandle = new AutoResetEvent(false); static void Main(string[] args) { StartServer(); waitHandle.WaitOne(); for (int x=0; x&lt;1000; x++) { StartClient(x); } Console.WriteLine("Done starting clients"); Console.ReadLine(); } static void StartClient(int count) { Task.Factory.StartNew((paramCount) =&gt; { int myCount = (int)paramCount; using (TcpClient client = new TcpClient(ip, port)) { using (NetworkStream networkStream = client.GetStream()) { using (StreamWriter writer = new StreamWriter(networkStream)) { writer.WriteLine("hello, tcp world #" + myCount); } } } }, count); } static void StartServer() { Task.Factory.StartNew(() =&gt; { try { TcpListener listener = new TcpListener(port); listener.Start(); Console.WriteLine("Listening..."); waitHandle.Set(); while (true) { TcpClient theClient = listener.AcceptTcpClient(); Task.Factory.StartNew((paramClient) =&gt; { TcpClient client = (TcpClient)paramClient; byte[] buffer = new byte[32768]; MemoryStream memory = new MemoryStream(); using (NetworkStream networkStream = client.GetStream()) { do { int read = networkStream.Read(buffer, 0, buffer.Length); memory.Write(buffer, 0, read); } while (networkStream.DataAvailable); } string text = Encoding.UTF8.GetString(memory.ToArray()); Console.WriteLine("from client: " + text); }, theClient); } } catch (Exception e) { Console.WriteLine(e); } }, TaskCreationOptions.LongRunning); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload