
疑惑1:通过setsockopt设置SO_SNDBUF、SO_RCVBUF这连个默认缓冲区的,再用getsockopt获取设置的,发现返回是设置的两倍。为什么?
通过网上查找,看到linux的内核代码/usr/src/linux-2.6.13.2/net/core/sock.c,找到sock_setsockopt这个函数的这段代码:
caseSO_SNDBUF: /* Don't error on this BSD doesn't and if you think about it this is right。 Otherwise apps have to play 'guess the biggest size' games。 RCVBUF/SNDBUF are treated in BSD as hints */ if (val > sysctl_wmem_max)//val是我们想设置的缓冲区大小的 val = sysctl_wmem_max;//大于最大,则val设置成最大 sk->sk_userlocks |= SOCK_SNDBUF_LOCK; if ((val * 2) < SOCK_MIN_SNDBUF)//val的两倍小于最小,则设置成最小 sk->sk_sndbuf = SOCK_MIN_SNDBUF; else sk->sk_sndbuf = val * 2;//val的两倍大于最小,则设置成val的两倍 /* * Wake up sending tasks if we * upped the value。
*/ sk->sk_write_space(sk); break; caseSO_RCVBUF: /* Don't error on this BSD doesn't and if you think about it this is right。 Otherwise apps have to play 'guess the biggest size' games。 RCVBUF/SNDBUF are treated in BSD as hints */ if (val > sysctl_rmem_max) val = sysctl_rmem_max; sk->sk_userlocks |= SOCK_RCVBUF_LOCK; /* FIXME: is this lower bound the right one? */ if ((val * 2) < SOCK_MIN_RCVBUF) sk->sk_rcvbuf = SOCK_MIN_RCVBUF; else sk->sk_rcvbuf = val * 2; break;
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/tongxinshuyu/article-60264-1.html
这个鸟毛