[英]Unable to read character from a networkStream object using StreamReader in C#
I want to read an array of character from a networkStream object in this way : 我想以这种方式从networkStream对象中读取字符数组:
public String readLine(NetworkStream networkStream)
{
using (StreamReader reader = new StreamReader(networkStream))//This line throws an exception
{
char[] buffer = new char[128];
int offset = 0;
int ch;
while(true)
{
ch = reader.Read();
if (ch == -1 || ch == '\n')
{
break;
}
else if (ch == '\r')
{
//int tempch = ;
if (reader.Peek()== '\n')
{
break;
}
}
}
When i run the program i get an exception error "Stream unreadable" . 当我运行程序时,出现异常错误“流不可读” 。 Is it because the StreamReader constructor expects a stream Object not a networkStream as a parameter ?
是否因为StreamReader构造函数希望将流对象而不是networkStream作为参数? If yes, is there a workaround or an alternative way of reading characters from a networkStream object?
如果是,是否存在解决方法或从networkStream对象读取字符的替代方法?
You'll get this exception when the stream's CanRead property is false. 当流的CanRead属性为false时,您将获得此异常。 We can't tell how you created the NetworkStream, but a logical explanation is that you're trying to read from a network stream that you opened for writing.
我们无法告诉您如何创建NetworkStream,但是合乎逻辑的解释是,您正在尝试从为写入打开的网络流中读取数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.