简体   繁体   English

Java非阻塞套接字的刷新操作

[英]Flush operation for Java non-blocking socket

In normal blocking socket, I can use Socket.getOutputStream().flush() to partly control when to send out a TCP packet. 在普通的阻塞套接字中,我可以使用Socket.getOutputStream()。flush()来部分控制何时发送TCP数据包。 Is there an equivalent operation for SocketChannel? SocketChannel是否有等效的操作?

Edit: My guess is that every time I call SocketChannel.write(ByteBuffer src), at least one individual packet will be sent, even the buffer has only 1 byte(assume Nagle is off), am I right? 编辑:我的猜测是,每次我调用SocketChannel.write(ByteBuffer src)时,至少会发送一个单独的数据包,即使缓冲区只有1个字节(假设Nagle已关闭),对吗?

Edit: package -> packet, sorry for wrong spelling. 编辑:程序包->数据包,抱歉拼写错误。

I guess you could write something like 我想你可以写一些像

while (buf.hasRemaining()) {
    socketChan.write(buf);
}

Perhaps there is a better solution. 也许有更好的解决方案。

As I noted recently Socket.getOutputStream().flush() does nothing in Sun's JDK 6. 正如我最近指出的那样,Socket.getOutputStream()。flush()在Sun的JDK 6中没有任何作用。

However, there is no guarentee that write() will send one packet (not package) it could send more or less. 但是,没有保证write()将发送一个可以发送或多或少的数据包(不是包)。 All you know is it has been passed to the OS to handle it. 您所知道的是,它已经传递给操作系统进行处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM