Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How many packets or bytes are in the socket receive queue?

Tags:

linux

sockets

udp

Calling getsockopt with SO_RCVBUF will return the allocated size of the socket receive buffer.

I am curious to know if it is possible to query for how many datagram packets (or bytes) are actually in the buffer prior to calling recv or recvfrom? If it helps, I can settle for a Linux specific answer. The socket in question is UDP, but I suspect it wouldn't matter for TCP.

The reason why I ask is only for testing and debugging purposes. I'm trying to validate if my call to setsocktop(SO_RCVBUF) is setting a sufficient enough size. Knowing if the receive buffer was ever close to reaching its limit would validate if a sufficient size was set.

like image 319
selbie Avatar asked May 07 '13 20:05

selbie


2 Answers

On Windows, what you are looking for is available via ioctlsocket(FIONREAD) and WSAIoCtl(FIONREAD), which both return the full size of the complete buffered data, even when multiple datagram messages are buffered. However, there is no equivalent on Linux. There is ioctl(FIONREAD), which only returns the size of the next buffered message.

like image 75
Remy Lebeau Avatar answered Sep 21 '22 19:09

Remy Lebeau


use the SIOCINQ ioctl() on the socket to learn the amount of queued incoming bytes.

Similarly there's SIOCOUTQ for querying the send buffer.

like image 28
nos Avatar answered Sep 23 '22 19:09

nos