SocketChannel.write()写入问题
这里的问题是我可以看到数据正在被写入套接字,但并不总是被发送。
这是一个代码片段,
ByteBuffer writeBuffer = ByteBuffer.allocate(8192);
writeBuffer.clear();
writeBuffer.put("heartbeat".getBytes());
writeBuffer.flip();
LOG.debug("is connected: " + socketChannel.isConnected());
int bytesWritten = 0;
if (key.isWritable()) {
while (writeBuffer.hasRemaining()) {
bytesWritten += socketChannel.write(writeBuffer);
}
}
我使用 TCPMon 来查看实际数据是否被写入套接字 - 确实如此。
但使用 WireShark(另一种网络监控工具)我无法看到数据包通过网卡。
任何帮助将不胜感激
The issue here is I can see that the data is being written out to the socket but it is not ALWAYS being sent.
Here's a code sniplet
ByteBuffer writeBuffer = ByteBuffer.allocate(8192);
writeBuffer.clear();
writeBuffer.put("heartbeat".getBytes());
writeBuffer.flip();
LOG.debug("is connected: " + socketChannel.isConnected());
int bytesWritten = 0;
if (key.isWritable()) {
while (writeBuffer.hasRemaining()) {
bytesWritten += socketChannel.write(writeBuffer);
}
}
I use TCPMon to see if the actual data gets written out to the socket - WHICH it does.
But using WireShark (another network monitoring tool) I cannot see the packet going through the NIC.
Any help would be appreciated
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
无论如何你的代码都是错误的。如果写入返回零,则套接字发送缓冲区已满,因此您应该注册 OP_WRITE 并返回到选择循环,而不是浪费时间旋转直到再次有空间。您目前的技术会导致其他服务通道匮乏并浪费 CPU 周期。
另外,此时测试 isConnected() 是徒劳的。这是。你连接了它。该方法告诉您有关套接字的状态,而不是连接的状态。
Your code is wrong anyway. If the write returns zero the socket send buffer is full, so you should register OP_WRITE and return to the select loop, rather than waste time spinning until there is room again. Your present technique starves the other channels of service and wastes CPU cycles.
Also, testing
isConnected()
at this point is futile. It is. You connected it. That method tells you about the state of the socket, not the connection.尝试如下
Try it as follows