即使客户端将数据写入网络流,但在服务器上不可用为什么
客户端编码
TcpClient tcpclnt = new TcpClient("192.157.1.1", 8001);
Stream stm = tcpclnt.GetStream();
byte[] bites = new byte[dataLength];
// assigning values for bites
stm.Write(clientdata, 0, clientdata.Length);
服务器端编码
TcpListener listener = new TcpListener(IPAddress.Any, 8001);
listener.Start(10);
Socket soc = listener.AcceptSocket();
byte[] bites = new byte[1000];
int avail = soc.Available;
int receivedBytesLen = soc.Receive(bites);
将客户端数据写入流后,在服务器端 soc.Available 也为零。所以服务器无法读取数据。问题是什么?
client side coding
TcpClient tcpclnt = new TcpClient("192.157.1.1", 8001);
Stream stm = tcpclnt.GetStream();
byte[] bites = new byte[dataLength];
// assigning values for bites
stm.Write(clientdata, 0, clientdata.Length);
Server side Coding
TcpListener listener = new TcpListener(IPAddress.Any, 8001);
listener.Start(10);
Socket soc = listener.AcceptSocket();
byte[] bites = new byte[1000];
int avail = soc.Available;
int receivedBytesLen = soc.Receive(bites);
After writting the clientdata on stream also In server side soc.Available is zero. So server can't read the data. What is the problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
数据被缓冲。客户必须写更多。或者关闭流。
The data is buffered. The client hast to Write more. Or close the stream.
根据 msdn,NetworkStream.Flush() 实际上尚未在 .net 4.0 中实现:
您的问题可能是由 nagle 算法 引起的,防止少量数据发送到减少拥堵。您可以尝试通过在 tcpClient 对象上设置以下属性来禁用它:
另请查看 networkComms.net,它已准备好如果您仍然遇到问题,请使用解决方案。
NetworkStream.Flush() is actually not implemented 'yet' in .net 4.0 as per msdn:
Your problem is probably being caused by the nagle algorithm, prevent small amounts of data being sent to reduce congestion. You could try disabling it by setting the following properties on your tcpClient object:
Also checkout networkComms.net which is a ready to use solution if you keep having problems.
在
stm.Write(...)
之后添加一个调用stm.Flush()
以便数据将刷新到网络。after
stm.Write(...)
add a callstm.Flush()
so the data will be flushed to the network.