views:

1429

answers:

3

How do I discover how many bytes have been sent to a TCP socket but have not yet been put on the wire?

Looking at the diagram here:

I would like to know the total of Categories 2, 3, and 4 or the total of 3 and 4. This is in C(++) and on both Windows and Linux. Ideally there is a ioctl that I could use, but there doesn't seem to be any.

A: 

Some Unix flavors may have an API way to do this, but there is no way to do it that is portable across different variants.

dvorak
+2  A: 

Under Linux, see the man page for tcp(7).

It appears that you can get the number of untransmitted bytes by ioctl(sock,SIOCINQ ...

Other stats might be available from members of the structure given back by the TCP_INFO getsockopt() call.

MarkR
A: 

If you want to determine wheter to add data or not: don't worry, send will block until the data is in the queue. If you don't want it to block, you can tell it to send(2):

send(socket, buf, buflen, MSG_DONTWAIT);

But this only works on Linux.

You can also set the socket to non-blocking:

fcntl(socket, F_SETFD, O_NONBLOCK);

This way write will return an error (EAGAIN) if the data cannot be written to the stream.

terminus