G
Gordon Beaton
We are writing data to a socket and in most cases, a TCP segment is
then passed down to the IP layer and a separate packet is sent
across the network. However, occasionally we get two or three lots
of data being sent in the same TCP segment, which the receiving
station cannot handle. We have enabled TCP_NODELAY, but this hasn't
resolved the problem.
Is there a way of forcing each piece of data to be sent in its own
packet?
No there isn't. TCP sends a continuous stream of bytes. It knows
nothing about messages, and doesn't attempt to respect any message
boundaries you might have defined in your application. Depending on
the network MTU, the rate of transmission, the size of your messages
and the phase of the moon, it might send several messages at once or
break a single message into several transmissions.
If you want to use TCP as if it were a message based protocol, you
need to delimit your messages in the application. A simple and common
solution involves sending each message as a short header with the
length of the message body that follows. The recipient then reads the
length, followed by the specified number of bytes.
TCP_NODELAY is not a solution to your problem, however by turning it
on you turn off some other optimizations (i.e. Nagle) that the
protocol normally does.
/gordon