Shohreh Shohreh - 2 months ago 20
C++ Question

What determines the number of TCP packets in boost::asio?

I am sending TCP packets via

asio::tcp::socket
.

My problem is that although each time the data I send is about 800 bytes and the maximum packet size is 1500 bytes the data is sent in over 5 packets.

(I check the number of packets and the data in sniffsmart software)

why is that?!

how should I solve or track this?

Answer

EDIT : Number of packets in a streaming protocol like TCP depends on your network configurations. However this answer optimizes the use of sockets so that no overhead is added by the program in sense of number of transmitted packets.

If you are trying to get larger packets you should try to put all data on socket at once. You probably are dividing it to multiple packets yourself. Because of Nagle's algorithm of TCP, TCP stack will send available data to peer immediately. So if you put data on socket in multiple occasions the rest of the data will go to the next packet.

Or you can turn off Nagle's algorithm like this :

m_socket.set_option(tcp::no_delay(true));