这两个选项就是设置和获取缓冲区的大小,看代码吧。
发送缓冲区:
[mapan@localhost unixNetwork]$ cat test.cpp
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define MAXLINE 4096
int main()
{
int listenfd,connfd;
socklen_t clilen;
struct sockaddr_in cliaddr,servaddr;
listenfd=socket(AF_INET,SOCK_STREAM,0);
int buf_len;
socklen_t len=sizeof(buf_len);
getsockopt(listenfd,SOL_SOCKET,SO_SNDBUF,(void *)&buf_len,&len);
printf("buf:%d\n",buf_len);
int setbuf=80000;
socklen_t len1=sizeof(setbuf);
setsockopt(listenfd,SOL_SOCKET,SO_SNDBUF,(void *)&setbuf,len1);
getsockopt(listenfd,SOL_SOCKET,SO_SNDBUF,(void *)&buf_len,&len);
printf("buf:%d\n",buf_len);
getchar();
close(listenfd);
return 0;
}
[mapan@localhost unixNetwork]$ g++ test.cpp
[mapan@localhost unixNetwork]$ ./a.out
buf:16384
buf:160000
接收缓冲区:
[mapan@localhost unixNetwork]$ cat test.cpp
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define MAXLINE 4096
int main()
{
int listenfd,connfd;
socklen_t clilen;
struct sockaddr_in cliaddr,servaddr;
listenfd=socket(AF_INET,SOCK_STREAM,0);
int buf_len;
socklen_t len=sizeof(buf_len);
getsockopt(listenfd,SOL_SOCKET,SO_RCVBUF,(void *)&buf_len,&len);
printf("buf:%d\n",buf_len);
int setbuf=80000;
socklen_t len1=sizeof(setbuf);
setsockopt(listenfd,SOL_SOCKET,SO_RCVBUF,(void *)&setbuf,len1);
getsockopt(listenfd,SOL_SOCKET,SO_RCVBUF,(void *)&buf_len,&len);
printf("buf:%d\n",buf_len);
getchar();
close(listenfd);
return 0;
}
[mapan@localhost unixNetwork]$ g++ test.cpp
[mapan@localhost unixNetwork]$ ./a.out
buf:87380
buf:160000
前言万语不如代码来的实在,如果是服务端setsockopt要在listen函数之前调用,客户端则在connect函数之前调用,注意setsockopt和getsockopt最后一个参数有区别。这里还有一个小细节要说明一下:进程设置的SO_SNDBUF和SO_RCVBUF也并不是真正的上限
,在内核中会把该值翻一倍作为缓存上限使用。