I'm using Linux 2.6.38 (fc14). What is the ioctl flag to get the amount of free space on a socket file descriptor (say, a TCP socket)? I found NetBSD has FIONREAD, FIONWRITE and FIONSPACE for such related purposes. But, I could only use FIONREAD in Linux.
2 Answers
SIOCOUTQ
is the Linux equivalent of FIONWRITE
. I don't believe there is a direct FIONSPACE
equivalent: instead, you can subtract the value returned by SIOCOUTQ
from the socket send buffer size, which can be obtained with getsockopt(s, SOL_SOCKET, SO_SNDBUF, ...)
.

- 233,326
- 40
- 323
- 462
-
Please note that there is no guarantee that write operations do not block even if there is room in the socket send buffer according to this calculation. In practice with kernel 3.2.0 I sometimes get EWOULDBLOCK on send() even with ample space in the send buffer. – HHK Feb 03 '13 at 19:18
For information, about what @HKK says, found in man socket(7):
SO_SNDBUF
Sets or gets the maximum socket send buffer in bytes. The kernel doubles this value (to allow space for bookkeeping overhead) when it is set using setsockopt(2), and this doubled value is returned by getsockopt(2). The default value is set by the /proc/sys/net/core/wmem_default file and the maximum allowed value is set by the /proc/sys/net/core/wmem_max file. The minimum (doubled) value for this option is 2048.

- 71
- 6