简体   繁体   中英

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. Is there an equivalent operation for 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?

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.

However, there is no guarentee that write() will send one packet (not package) it could send more or less. All you know is it has been passed to the OS to handle it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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