Read the contents of send-q TCP sockets in Linux
I have a TCP client that constantly sends data to the server. After the client and the server successfully connect, the client sends data continuously at intervals of several seconds.
When the client and server send very little data and then disconnect, I learned that TCP retransmits data based on the value in the TCP_retries2, and I configure this value to 8 so that after 100 I write the wrong second.
But there will be some unacknowledged packets in send-q.
Is there a way to read the contents of this unacknowledged packet in send-q in my program before closing this socket, or should I remember to send the data and resend it after connecting again? Are there other ways to achieve this?
You can get the size of sendq using ioctl:
Returns the amount of unsent data in the socket send queue.
The socket must not be in LISTEN state, otherwise an error
(EINVAL) is returned. SIOCOUTQ is defined in
<linux/sockios.h>. Alternatively, you can use the synonymous
TIOCOUTQ, defined in <sys/ioctl.h>.
Note that sendq only tells you what the kernel of the remote system has accepted, it does not guarantee that an application running on that host has processed it. Most failures exist in the network between the communicating parties, but this metric does not serve as definitive evidence of successful transmission.