StreamWriter 不会刷新到 NetworkStream

发布于 2024-08-04 10:57:25 字数 1416 浏览 5 评论 0原文

使用 StreamWriter 写入 NetworkStream,并使用 StreamReader 读取响应。该应用程序正在向新闻服务器发送命令并读取响应。

简化的代码(无错误处理等):

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() > -1) {
    serverReader.ReadLine();
}

serverWriter.WriteLine("authinfo user username");

// expect response "381 more authentication required", but code just blocks
string response = serverReader.ReadLine();

代码在最后一行阻塞,大概是在等待网络流发送响应。

我可以通过使用 serverReader.Peek() 设置超时循环来避免挂起应用程序,但我总是会超时;我从来没有得到回应。

如果我直接远程登录到服务器和端口并输入命令,我会立即得到响应。

如果我显式调用 serverWriter.Flush(),而不是使用 AutoFlush 属性,我仍然会阻塞并且永远不会得到响应。

有什么想法为什么我使用这种方法没有得到服务器的响应吗?

谢谢!

已解决:

上面的代码确实对我有用,所以我回去并在该代码的基础上构建了不起作用的代码。

在挂起的代码中,我仍在使用 serverReader.Peek() 的超时循环。即使缓冲区中有数据可供读取,Peek() 始终返回 -1!用对 ReadLine() 的阻塞调用替换 Peek() 循环解决了我的问题。

我最初放入超时循环是因为该应用程序是多线程的,并且我不想阻塞。我将不得不重新审视这个问题,看看如何在不使用 Peek() 的情况下解决线程计时问题。

谢谢大家,好的答案!

Using a StreamWriter to write to a NetworkStream, and a StreamReader to read the response. The app is sending commands and reading responses to a news server.

Simplified code (sans error handling, etc.):

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() > -1) {
    serverReader.ReadLine();
}

serverWriter.WriteLine("authinfo user username");

// expect response "381 more authentication required", but code just blocks
string response = serverReader.ReadLine();

The code blocks at that last line, presumably waiting for the network stream to send a response.

I can avoid hanging the app by setting a timeout loop using serverReader.Peek(), but I will always timeout; I never get a response.

If I telnet to the server and port directly and enter the commands, I get an immediate response.

If I call serverWriter.Flush() explicitly, instead of using the AutoFlush property, I still block and never get a response.

Any ideas why I'm not getting a response to the server using this approach?

Thanks!

Resolved:

The above code does work for me, so I went back and built upon that code to the code that wouldn't work.

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.

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().

Thanks all, good answers!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

柠北森屋 2024-08-11 10:57:25

我怀疑 StreamWriter 是问题所在......但有一个简单的方法可以找出问题所在。下载 WireShark 并查看网络上实际的情况。这是迄今为止了解正在发生的情况的最简单方法。

I doubt that it's the StreamWriter that's the problem... but there's a simple way to find out. Download WireShark and see what's actually coming and going on the network. That's by far the simplest way of finding out what's going on.

ぶ宁プ宁ぶ 2024-08-11 10:57:25

尝试“authinfo 用户用户名\r\n”。 RFC 规定 NNTP 命令行必须以 CR-LF 终止。

Try "authinfo user username\r\n". The RFC says NNTP command lines must be terminated by a CR-LF.

在梵高的星空下 2024-08-11 10:57:25

上面的代码确实对我有用,所以我回去并在该代码的基础上构建了不起作用的代码。

在挂起的代码中,我仍在使用 serverReader.Peek() 的超时循环。即使缓冲区中有数据可供读取,Peek() 始终返回 -1!用对 ReadLine() 的阻塞调用替换 Peek() 循环解决了我的问题。

我最初放入超时循环是因为该应用程序是多线程的,并且我不想阻塞。我将不得不重新审视这个问题,看看如何在不使用 Peek() 的情况下解决线程计时问题。

谢谢大家,好的答案!

The above code does work for me, so I went back and built upon that code to the code that wouldn't work.

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.

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().

Thanks all, good answers!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文