Note that there are some explanatory texts on larger screens.

plurals
  1. POStreamWriter won't flush to NetworkStream
    primarykey
    data
    text
    <p>Using a <code>StreamWriter</code> to write to a <code>NetworkStream</code>, and a <code>StreamReader</code> to read the response. The app is sending commands and reading responses to a news server.</p> <p>Simplified code (sans error handling, etc.):</p> <pre><code>tcpClient = new TcpClient(); tcpClient.Connect(Name, Port); networkStream = tcpClient.GetStream(); serverReader = new StreamReader(networkStream, Encoding.Default); serverWriter = new StreamWriter(networkStream, Encoding.ASCII) { AutoFlush = true }; // reads the server's response to the connect: "200 news.newsserver.com" // commenting out these lines doesn't solve the problem while (serverReader.Peek() &gt; -1) { serverReader.ReadLine(); } serverWriter.WriteLine("authinfo user username"); // expect response "381 more authentication required", but code just blocks string response = serverReader.ReadLine(); </code></pre> <p>The code blocks at that last line, presumably waiting for the network stream to send a response. </p> <p>I can avoid hanging the app by setting a timeout loop using <code>serverReader.Peek()</code>, but I will always timeout; I never get a response.</p> <p>If I telnet to the server and port directly and enter the commands, I get an immediate response.</p> <p>If I call <code>serverWriter.Flush()</code> explicitly, instead of using the <code>AutoFlush</code> property, I still block and never get a response.</p> <p>Any ideas why I'm not getting a response to the server using this approach?</p> <p>Thanks!</p> <h1>Resolved:</h1> <p>The above code <em>does</em> work for me, so I went back and built upon that code to the code that wouldn't work. </p> <p>In the code that hangs, I was still using the timeout loop with serverReader.Peek(). Peek() always returns -1, even though there is data in the buffer to read!! Replacing the Peek() loop with a blocking call to ReadLine() solves my problem. </p> <p>I put the timeout loop in originally because the app is multi-threaded, and I didn't want to block. I will have to revisit this issue and see how I can resolve the thread timing without using Peek(). </p> <p>Thanks all, good answers!</p>
    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.
 

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