Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does TCP_NODELAY affect consecutive write() calls?

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?

like image 806
AyCe Avatar asked Aug 06 '13 22:08

AyCe


2 Answers

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.

like image 154
user207421 Avatar answered Sep 19 '22 15:09

user207421


Could manually packing the byte arrays and sending them in one call to write help?

Yes, send them all in a single call to write. This will maximize the chances that your bytes will be sent in a single packet.

Of course, you can never know, since there are too many variables involved - different OSs and lots of different networking gear between you and your peer, but if you give the OS the ability to pack everything together, it will generally try to.

If you disable nagle and make seperate system calls (remember, the OS controls the sockets, not your appliation or java), you're asking the OS to send them individually. The OS has no idea you're about to call write again with some more data.

like image 33
xaxxon Avatar answered Sep 22 '22 15:09

xaxxon