Socket buffer size not increasing

回眸只為那壹抹淺笑 提交于 2019-12-05 02:31:37

问题


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!


回答1:


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




回答2:


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 using setsockopt(2), and this doubled value is returned by getsockopt(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).



来源:https://stackoverflow.com/questions/33775740/socket-buffer-size-not-increasing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!