当前位置:
首页 >
SO_SNDTIMEO和SO_RCVTIMEO
发布时间:2025/6/15
32
豆豆
生活随笔
收集整理的这篇文章主要介绍了
SO_SNDTIMEO和SO_RCVTIMEO
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
SO_SNDTIMEO和SO_RCVTIMEO这两个套接字选项用来设置超时时间的,看代码吧。
[mapan@localhost sockOption]$ ls client.cpp makefile server.cpp [mapan@localhost sockOption]$ cat client.cpp #include <unistd.h> #include <sys/types.h> #include <sys/socket.h> #include <netdb.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> #include <errno.h> #include <malloc.h> #include <netinet/in.h> #include <arpa/inet.h> #include <sys/ioctl.h> #include <stdarg.h> #include <fcntl.h> #include <sys/types.h> #include <sys/wait.h> #include <netinet/in.h> #include <arpa/inet.h> #include <signal.h> #define MAXLINE 4096int main(int argc,char **argv) {int connfd,ret;char sendbuf[400000]={0};struct sockaddr_in servaddr;if(argc != 2){printf("error\n");}connfd=socket(AF_INET,SOCK_STREAM,0);memset(&servaddr,0,sizeof(servaddr));servaddr.sin_family=AF_INET;servaddr.sin_port=htons(6666);inet_pton(AF_INET,argv[1],&servaddr.sin_addr);connect(connfd,(struct sockaddr *)&servaddr,sizeof(servaddr));struct timeval stTimeValStruct;stTimeValStruct.tv_sec=5;stTimeValStruct.tv_usec=0;setsockopt(connfd,SOL_SOCKET,SO_SNDTIMEO,&stTimeValStruct,sizeof(stTimeValStruct));while(1){ret= write(connfd,sendbuf,sizeof(sendbuf));printf("ret=%d\n",ret);}close(connfd);return 0; }[mapan@localhost sockOption]$ cat server.cpp #include <unistd.h> #include <sys/types.h> #include <sys/socket.h> #include <netdb.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> #include <errno.h> #include <malloc.h> #include <netinet/in.h> #include <arpa/inet.h> #include <sys/ioctl.h> #include <stdarg.h> #include <fcntl.h> #include <sys/types.h> #include <sys/wait.h> #include <netinet/in.h> #include <arpa/inet.h> #include <signal.h> #define MAXLINE 4096int main() {int listenfd,acceptfd;struct sockaddr_in servaddr;listenfd=socket(AF_INET,SOCK_STREAM,0);memset(&servaddr,0,sizeof(servaddr));servaddr.sin_family=AF_INET;servaddr.sin_port=htons(6666);servaddr.sin_addr.s_addr=htonl(INADDR_ANY);bind(listenfd,(struct sockaddr *)&servaddr,sizeof(servaddr));listen(listenfd,10);acceptfd=accept(listenfd,(struct sockaddr *)NULL,NULL); getchar();close(acceptfd);close(listenfd); return 0; }[mapan@localhost sockOption]$ cat makefile all:server clientserver.o:server.cppg++ -c server.cpp client.o:client.cppg++ -c client.cpp server:server.og++ -o server server.o client:client.og++ -o client client.oclean:rm -f server client *.o [mapan@localhost sockOption]$
编译并运行,客户端需要新打开一个窗口执行。
[mapan@localhost sockOption]$ make g++ -c server.cpp g++ -o server server.o g++ -c client.cpp g++ -o client client.o [mapan@localhost sockOption]$ ./server [mapan@localhost sockOption]$ ./client 127.0.0.1 ret=400000 ret=400000 ret=400000 ret=254012 ret=-1 ret=-1 ^C [mapan@localhost sockOption]$
再看看看接收缓冲区和发送缓冲区:
[mapan@localhost ~]$ netstat -nap | grep 6666 (Not all processes could be identified, non-owned process infowill not be shown, you would have to be root to see it all.) tcp 0 0 0.0.0.0:6666 0.0.0.0:* LISTEN 6151/./server tcp 138900 0 127.0.0.1:6666 127.0.0.1:59104 ESTABLISHED 6151/./server tcp 0 1315113 127.0.0.1:59104 127.0.0.1:6666 FIN_WAIT1 - [mapan@localhost ~]$
虽然我们没有获取到发送缓冲区的最大的上限值,但是发送缓冲区确实满了,write返回-1,证明write失败了。如果我们没有设置超时时间,write就会一直阻塞在那里。
发送缓冲区为什么会满?你首先要懂得write是干嘛的,它可不是发送数据。write是将用户进程中的数据拷贝到发送缓冲区中,如果发送缓冲区满了,write就会返回-1。发送数据是跟write没有关系的,那是协议做的事。由netstat可以看到对端接收缓冲区有数据,而且已经满了。所以数据堆积在发送缓冲区发不出去,导致发送缓冲区满了。
我们设置超时时间为5秒,write会阻塞在那里5秒,然后返回。
参考资料:unix网络编程卷一
总结
以上是生活随笔为你收集整理的SO_SNDTIMEO和SO_RCVTIMEO的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: 拼接JSON字符串
- 下一篇: close和SO_LINGER