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!
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
Always have a look what the
man
page says: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).