Reputation: 868
int n = 0;
if ( 0 != getsockopt(iSockFd,SOL_SOCKET,SO_RCVBUF, &n, sizeof(n)))
{
printf("Get socket option failed, errno: %d\n",errno);
}
else
{
printf("Current socket buff len = %d\n", n);
}
n = 225280;
if(0 != setsockopt(iSockFd, SOL_SOCKET, SO_RCVBUF, (const void *)&n, sizeof(n)))
{
printf("setsock err errno %d\n", errno);
}
else
{
printf("setsock opt success\n");
}
n = 0;
if ( 0 != getsockopt(iSockFd,SOL_SOCKET,SO_RCVBUF, &n, sizeof(n)))
{
printf("Get socket option failed, errno: %d\n",errno);
}
else
{
printf("After setting socket buff len = %d\n", n);
}
Output is -
Current socket buff len = 41600
setsock opt success
After setting socket buff len = 41600.
Looks like receive buffer size is not increasing, any idea why this happens?
Thanks in advance!
Upvotes: 5
Views: 5674
Reputation: 133189
Always have a look what the man
page says:
SO_RCVBUF
Sets or gets the maximum socket receive buffer in bytes. The kernel doubles this value (to allow space for bookkeeping overhead) when it is set usingsetsockopt(2)
, and this doubled value is returned bygetsockopt(2)
. The default value is set by the/proc/sys/net/core/rmem_default
file, and the maximum allowed value is set by the/proc/sys/net/core/rmem_max
file. The minimum (doubled) value for this option is 256.
http://man7.org/linux/man-pages/man7/socket.7.html
So there is an upper limit and any attempt to set a larger value will silently fail, which means there will be no error, the size just isn't raised. Such a limit exists on pretty much all existing systems, not just Linux. Also note that even if your setsockopt()
was successful, getsockopt()
would return a larger value because this value is internally doubled (this is Linux exclusive, other systems don't do that).
Upvotes: 0
Reputation: 7842
If the kernel is of newer version (2.6.17 or higher), checkout whether autotuning is enabled by verifying the file /proc/sys/net/ipv4/tcp_moderate_rcvbuf . If the value of tcp_moderate_rcvbuf is 1, then autotuning is enabled. In such a scenario, the receive buffer will be dynamically updated by the kernel and is bound to the values in /proc/sys/net/ipv4/tcp_rmem. Check whether this limit is hit.
If the kernel is of older version, check whether the SO_RCVBUF is limited by the values in /proc/sys/net/core/rmem_default and /proc/sys/net/core/rmem_max. Incase of TCP, also check the value of /proc/sys/net/ipv4/tcp_rmem
Also note that 'Manually adjusting socket buffer sizes with setsockopt() disables autotuning' . Here is good link on tuning for linux http://www.psc.edu/index.php/networking/641-tcp-tune
Upvotes: 4