How does TCP_NODELAY affect consecutive write() calls?

java, networking, sockets, tcp

Solution

My question is: are these 3 byte arrays sent in one packet?

As you have disabled the Nagle algorithm, almost certainly not, but you can't be 100% sure.

I know you don't have much control over how TCP constructs a network packet, but is there any way to tell the socket to (at least try to) pack these byte arrays

Yes. Don't disable the Nagle algorithm.

so network overhead is avoided? Could manually packing the byte arrays and sending them in one call to write help?

Yes, or simpler still just wrap the socket output stream in a `BufferedOutputStream` and call `flush()` when you want the data to be sent, as per your present code. You are correct that `flush()` does nothing on a socket output stream, but it flushes a `BufferedOutputStream.`

Problem

TCP_NODELAY is an option to enable quick sending of TCP packets, regardless of their size. This is very useful option when speed matters, however, I'm curious what it will do to this: ``` Socket socket = [some socket]; socket.setTcpNoDelay(true); OutputStream out = socket.getOutputStream(); out.write(byteArray1); out.write(byteArray2); out.write(byteArray3); out.flush(); ``` I tried to find what flush actually does on a `SocketOutputStream`, but as far as I know now, it doesn't do anything. I had hoped it would tell the socket "send all your buffered data NOW", but, unfortunately, no it doesn't. My question is: are these 3 byte arrays sent in one packet? I know you don't have much control over how TCP constructs a network packet, but is there any way to tell the socket to (at least try to) pack these byte arrays, so network overhead is avoided? Could manually packing the byte arrays and sending them in one call to `write` help?

Original source