Socket thread response in Java

I have a Java socket server and the connection socket is working fine. I need help streaming a response to a client.

I get the output stream from socket.getOutputStream(). How can I make it so that when I write to the output stream, it is sent immediately, but in the future I can send another piece of data over the same connection.

I tried just using writeand writecombined with flush, but I don’t know what I'm doing ...

+3
source share
3 answers

flush () will force data to be sent to the OS. The OS can buffer data, as well as the OS on the client. If you want the OS to send data earlier, I suggest you try disabling the Nagle algorithm. socket.setTcpNoDelay (true); However, you will find that the OS / driver options may still introduce some buffering / batch coelesing.

If you look at the Sun JDK 6 java.net.SocketOutputStream, you will see that the flush () method does nothing. This is not guaranteed on all platforms, and may require cleaning ().

+2
source

, , write(). flush(), , . , ( ). ( MTU). java, BufferedOutputStream.

+3

Another solution might be DataOutputStream
DataOutputStream dataOut = new DataOutputStream(socket.getOutputStream());
dataOut.writeInt(1)

+1
source

Source: https://habr.com/ru/post/1776631/


All Articles