Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send data in separate TCP segments without being merged by the TCP stack

On Linux (Kernel 3.0.0.12; C++ with GCC 4.6.1) I want to send shortly after each other a few TCP-packets via (POSIX) send-call.

With Wireshark I can see that the packets weren't sent in separate TCP-frames, but two or more were sent together in one packet.

Is there any way to tell the system to send data from one send()-buffer in own TCP-packet? The programmflow shouldn't block at the send-call.

like image 468
Bastian Born Avatar asked Dec 03 '22 06:12

Bastian Born


1 Answers

Your TCP stack is implementing Nagle's algorithm trying to increase efficiency by buffering data. Its a common optimization where the intent is amortize the cost of the 40+ byte (TCP + IP) header. Instead of sending multiple small packets, the stack buffers the data and combines them into a larger packet thereby reducing the header overhead.

TCP stacks don't buffer indefinitely though. As long as there is some un-acknowledged sent data on the connection, the sender continues to buffer. The next packet is sent out when:

  • A full packet worth of data is available to be sent OR
  • All previously sent data is acknowledged by the receiver

You can usually disable this by setting the TCP_NODELAY socket option.

setsockopt(sock_fd, IPPROTO_TCP, TCP_NODELAY, (char *) &flag, sizeof(int));
like image 140
jman Avatar answered Dec 05 '22 21:12

jman