经历
有次偶然的机会,想通过setsockopt
接口设置socket的收发缓冲区大小,但是在设置后调用getsockopt
验证下设定后的大小,getsockopt
竟然返回大小为零,确实让自己疑惑不已!
后来短暂休息、呼吸了几下,突然想通,原来是因为getsockopt
接口中的optlen参数必须为in & out参数:在调用前需要设置optval的大小为sizeof(optval)
,在函数返回后,optval携带真实的选项大小。
如果不在getsockopt
调用前设置optlen参数大小,则在某些环境中运行时,例如,CentOS7,会得到返回值为零的情况!
延展
通过setsockopt
设定套接字缓冲区的大小,在Linux环境中还会受到内核参数的限制。如果设定比较大的缓冲区大小,且超过了内核*max
参数的限制,则必须事先修改内核参数的值
socket收缓冲区内核参数
- net.core.rmem_max
- net.core.rmem_default
socket发缓冲区内核参数
- net.core.wmem_max
- net.core.wmem_default
显示和修改例子,其它可类比
sysctl net.core.rmem_max
sysctl -w net.core.rmem_max=$((102410241024))
代码
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
int main(void)
{
char szErrno[256];
printf("\nHello World!");
int tSock = socket(PF_INET, SOCK_DGRAM, 0);
if(tSock < 0)
{
strerror_r(errno, szErrno, sizeof(szErrno));
printf("\ncreate sock errno: %d -> %s", errno, szErrno);
return -1;
}
printf("\ncreate sock successfully, got sock fd: %d", tSock);
struct sockaddr_in tSockAddr = {};
tSockAddr.sin_family = AF_INET;
//tSockAddr.sin_addr.s_addr = 0;
tSockAddr.sin_port = htons(12701);
int rc = bind(tSock, (struct sockaddr*)&tSockAddr, sizeof(tSockAddr));
if(rc < 0)
{
strerror_r(errno, szErrno, sizeof(szErrno));
printf("\nbind errno: %d -> %s", errno, szErrno);
return -1;
}
printf("\nbind successfully !");
unsigned int bufSize = 1024*1024*1024;
rc = setsockopt(tSock, SOL_SOCKET, SO_RCVBUF, (const void*)&bufSize, sizeof(bufSize));
printf("\nsetsockopt rc: %d", rc);
unsigned int optval = 0;
/*optlen must be as in & out parameter*/
socklen_t optlen;
rc = getsockopt(tSock, SOL_SOCKET, SO_RCVBUF, (void*)&optval, &optlen);
printf("\ngetsockopt without providing the optlen value. rc: %d, has set bufsize: %u, optlen: %u", rc, optval, optlen);
//compare at CentOS7 env
optlen = sizeof(optval);
rc = getsockopt(tSock, SOL_SOCKET, SO_RCVBUF, (void*)&optval, &optlen);
printf("\ncompare getsockopt rc: %d, has set bufsize: %u, optlen: %u", rc, optval, optlen);
printf("\n");
close(tSock);
return 0;
}