Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to avoid TCP aggregation in 'C'?

Tags:

c

tcp

I'm sending many TCP packets each of size 50 bytes over the network. Later I found it out TCP aggregates a few 50 byte packets into a single TCP packet. My question is, is there a way to avoid TCP aggregation in 'C' program?

like image 818
user1960286 Avatar asked Sep 15 '25 10:09

user1960286


1 Answers

Packing multiple sent packets into a single TCP packet is handled using an algorithm known as Nagle's algorithm. To disable it, set the TCP_NODELAY option on your socket:

int flag = 1;
setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &flag, sizeof(flag));

Note that this decreases the efficiency of your network, and should be avoided unless you really do need each packet to be sent immediately.