hanvari hanvari - 4 months ago 15
Linux Question

Linux Sockets, how to get number of bytes/packets in sending buffer?

I am working on a simple network application under Linux, where I need to read the following two properties:


  1. The number of bytes in receive buffer, which are ready to be read.

  2. The number of bytes in socket send buffer, which has not been sent yet.



The receive buffer (1st property) could be obtained using FIONREAD option of ioctl() function. But for the second property (bytes# in send buffer) I am not sure how can I read that. I have tried the SO_SNDBUF option in getsockopt() function, but it turn out to be the maximum size of sending buffer rather than current size of data in send buffer.

Any thoughts or suggestions?

Answer

Here is how to obtain,

  1. The length of data in Receive Buffer which is not read yet:

    ioctl( socket_descriptor, FIONREAD, &size );  // alternative 1
    ioctl( socket_descriptor, SIOCINQ, &size );   // alternative 2
    
  2. The length of data in Send Buffer which is not drained yet (either not sent yet or send but not acknowledged by receiver):

    ioctl( socket_descriptor, TIOCOUTQ, &size );  // alternative 1
    ioctl( socket_descriptor, SIOCOUTQ, &size );  // alternative 2
    

Reference: http://linux.die.net/man/7/tcp

Comments