读的NetworkStream字节(挂起) [英] Read bytes from NetworkStream (Hangs)

查看:199
本文介绍了读的NetworkStream字节(挂起)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想学习网络的基础知识,我已经建立了从的这个的教程。我查通过telnet服务器和它的作品完美



现在,当我使用一些互联网上的许多客户端的样本:

  //创建TcpClient的。 
//注意,对于该客户端来工作,你需要有一个TCPSERVER
//连接到相同的地址作为由服务器,端口
//组合指定。
TcpClient的客户端=新的TcpClient(服务器,端口);

//翻译传递的消息转换为ASCII并将其存储为一个字节数组。
字节[]数据= System.Text.Encoding.ASCII.GetBytes(消息);

//获取客户流进行读取和写入。
的NetworkStream流= client.GetStream();

//将消息发送给连接的TCPSERVER。
stream.Write(数据,0,data.Length);

Console.WriteLine(已发送:{0},邮件);

//收到TcpServer.response。

//缓冲器以存储响应字节。
数据=新的字节[256];

//字符串存储响应ASCII码表示。
字符串responseData =的String.Empty;

//读取第一批的TCPSERVER响应字节。
的Int32字节= stream.Read(数据,0,data.Length);
responseData = System.Text.Encoding.ASCII.GetString(数据,0,字节);
Console.WriteLine(收到:{0},responseData);

//关闭一切。
stream.Close();
client.Close();



它不工作得很好。如果我会评论的stream.Read线,一切都运行完美(期望我不能读)。我也试图完成的,在使用的读取异步回调方法类似。然后它只能在我终止程序(服务器处理请求)



我怀疑我是从流造成此块读取的方式,但我M太无能明白我在做什么错。


解决方案

的实施将阻塞,直到数据中的至少一个字节可以
读,在没有数据可用的情况。




从的 MSDN



您的服务器propably未发送任何你。数据



编辑:



我测试你的客户和它的作品完美的罚款。自己尝试并设置以下参数:

 字符串服务器=google.com 
INT端口= 80;
字符串消息=GET / \\\



这绝对是你的服务器有问题。


I'm trying to learn the basics of networking and I've built an echo server from this tutorial. I checked the server with telnet and it works perfect.

Now when I'm using some of the many client samples on the Internet:

// Create a TcpClient.
// Note, for this client to work you need to have a TcpServer 
// connected to the same address as specified by the server, port
// combination.
TcpClient client = new TcpClient(server, port);

// Translate the passed message into ASCII and store it as a Byte array.
Byte[] data = System.Text.Encoding.ASCII.GetBytes(message);

// Get a client stream for reading and writing.
NetworkStream stream = client.GetStream();

// Send the message to the connected TcpServer. 
stream.Write(data, 0, data.Length);

Console.WriteLine("Sent: {0}", message);

// Receive the TcpServer.response.

// Buffer to store the response bytes.
data = new Byte[256];

// String to store the response ASCII representation.
String responseData = String.Empty;

// Read the first batch of the TcpServer response bytes.
Int32 bytes = stream.Read(data, 0, data.Length);
responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);
Console.WriteLine("Received: {0}", responseData);

// Close everything.
stream.Close();
client.Close();

It doesn't work very well. If I will comment the stream.Read line, everything works perfect (expect I can't read). I was also trying to accomplish that in a similar way using asynchronous callback method for the read. and then it only works after I terminate the program (the server handles the request)

I suspect that the way I'm reading from the stream cause this block, but I'm too clueless to understand what I'm doing wrong.

解决方案

The implementation will block until at least one byte of data can be read, in the event that no data is available.

From MSDN

Your server propably isn't sending you any data.

Edit:

I tested your client and it works perfectly fine. Try it yourself and set the following parameters:

  string server = "google.com";
  int port = 80;
  string message = "GET /\n";

It's definitely your server which has the problem.

这篇关于读的NetworkStream字节(挂起)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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