用于 Telnet 的 TcpClient 流 streamReader.ReadToEnd() [英] TcpClient Stream streamReader.ReadToEnd() for Telnet

查看:35
本文介绍了用于 Telnet 的 TcpClient 流 streamReader.ReadToEnd()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 telnet 连接开发了服务器和设备之间的双向对话.我想在发送下一个命令之前等待并获取读取缓冲区.我试着用这个:

I developing a bidirection dialog between a server and a device using a telnet connection. I would like to wait and get the read buffer before sending the next command. I tryed to use this:

TcpClient tcpClient;
NetworkStream networkStream;
StreamWriter streamWriter;

tcpClient = new TcpClient("10.0.0.51", 23);
networkStream = tcpClient.GetStream();
StreamReader streamReader = new StreamReader(networkStream);
networkStream.ReadTimeout = 500;
while(wline!="exit"){
    Console.Write(streamReader.ReadToEnd());
    Console.Write("next command:");
    wline =Console.ReadLine();
    streamWriter.Write(wline);
}

但似乎 ReadToEnd() 不能正常工作.如果我使用 Read()(单字节),我可以收到一些东西.

but seems that ReadToEnd() doesn't work properly. If I use Read() (single byte) I could receive something.

推荐答案

ReadToEnd 读取所有内容,直到 结束,在网络流的情况下,当流关闭时发生.除非另一端的设备关闭流,否则您的客户端无法知道确实没有更多数据要读取.

ReadToEnd reads everything until the end of the stream, which in the case of a network tream, occurs when that stream closes. Unless the device on the other end closes the stream, there is no way for your client to know that there are indeed no more data to read.

您应该使用 ReadLine 一次读取和处理一行,或者使用 ReadBlock 读取块中的数据.

You should use ReadLine to read and process one line at a time, or use ReadBlock to read the data in blocks.

这篇关于用于 Telnet 的 TcpClient 流 streamReader.ReadToEnd()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆