0
votes

The general function for sending data over TCP socket is below:

ssize_t send(int sockfd, const void *buffer, size_t length, int flags);

Generally, the TCP will fragment the data if data size is more that MSS. But it is said that send() API will send the 'length' amount of data and returns whatever amount of data could be sent to outgoing buffer and its the responsibility of the developer to check the return value and resend the remaining data.

So my doubt it, then if the 'length' value is more than MSS, will not TCP automatically fragment the data? If no, then how can we make the TCP to fragment the data internally with one send() call?

2

2 Answers

3
votes

Generally, the TCP will fragment the data if data size is more that MSS.

Not generally. Always. Otherwise MSS has no ascertainable meaning.

But it is said that send() API will send the 'length' amount of data and returns whatever amount of data could be sent to outgoing buffer and its the responsibility of the developer to check the return value and resend the remaining data.

Correct.

These two statements are not in conflict. send() transfers data to the socket send buffer; TCP transfers it from there to the network, taking due note of MSS, congestion, receive window, etc.

So my doubt it, then if the 'length' value is more than MSS, will not TCP automatically fragment the data?

See above. TCP deals with what is in the socket send buffer, not with the parameter values provided to send().

If no, then how can we make the TCP to fragment the data internally with one send() call?

You can't. You have no control over TCP segmentation.

0
votes

For a stream socket if the length is more than can be sent in a single packet the network layer will indeed automatically split it into multiple packets. You have very little control over how the OS chooses to split streamed data, there are socket options that have some impact (for example TCP_NODELAY) but if you are talking about mostly sending large packets it won't make much of a difference.