问题描述
使用的StreamWriter
来写入的NetworkStream
和的StreamReader
来读取响应。这款应用程序发送命令和读取响应新闻服务器。
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.
简体code(没有错误处理,等等。):
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();
在最后一行的code块,presumably等待网络数据流发送响应。
The code blocks at that last line, presumably waiting for the network stream to send a response.
我能避免通过设置超时环挂的程序 serverReader.Peek()
,但我会永远超时;我从来没有得到回应。
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.
如果我称之为 serverWriter.Flush()
明确,而不是使用自动刷新
属性,我仍然阻塞,从来没有得到回应。
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?
谢谢!
以上code的确实的工作对我来说,让我回去,并在那code到code这是行不通的建造。
The above code does work for me, so I went back and built upon that code to the code that wouldn't work.
在code,挂,我还是用的超时环路serverReader.Peek()。皮克()总是返回-1,即使有缓冲区中的数据读取!与阻塞调用的ReadLine()更换皮克()循环解决了我的问题。
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!
推荐答案
以上code的确实的工作对我来说,让我回去,并在那code内置到code表示是行不通的。
The above code does work for me, so I went back and built upon that code to the code that wouldn't work.
在code,挂,我还是用的超时环路serverReader.Peek()。皮克()总是返回-1,即使有缓冲区中的数据读取!与阻塞调用的ReadLine()更换皮克()循环解决了我的问题。
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!
这篇关于的StreamWriter将不刷新到的NetworkStream的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!