How to clear SocketChannel in Java NIO?

In Java IO, it OutputStreamcan use a method flush()to ensure that data is sent immediately.

Is there a corresponding feature in Java NIO for SocketChannel? As far as I know, there is a method force()for FileChanneldata reset.

Maybe a very naive question ...

+5
source share
3 answers

Use .force()or.getFD.sync()

Update. Since you clarified your question to be more specific in the SocketChannel (it was generally NIO), please see my answer.

+1
source

OutputStream.flush() , BufferedOutputStream ObjectOutputStream PrintStream, .

, , FileChannel.force() getFD().sync(). JVM ( ) .

SocketChannel.write() โ€‹โ€‹: . , , .

+7

() , , . TCP- , . , ( 64 ) , ( GB)

, , - .

If you want the data to be sent as quickly as possible, you can try to disable nagle, but most OSs are pretty smart at optimizing and disabling does not make as much difference as used.

+4
source

All Articles