SO_SNDTIMEO和SO_RCVTIMEO
2017-08-23 16:24
316 查看
SO_SNDTIMEO和SO_RCVTIMEO这两个套接字选项用来设置超时时间的,看代码吧。
再看看看接收缓冲区和发送缓冲区:
虽然我们没有获取到发送缓冲区的最大的上限值,但是发送缓冲区确实满了,write返回-1,证明write失败了。如果我们没有设置超时时间,write就会一直阻塞在那里。
发送缓冲区为什么会满?你首先要懂得write是干嘛的,它可不是发送数据。write是将用户进程中的数据拷贝到发送缓冲区中,如果发送缓冲区满了,write就会返回-1。发送数据是跟write没有关系的,那是协议做的事。由netstat可以看到对端接收缓冲区有数据,而且已经满了。所以数据堆积在发送缓冲区发不出去,导致发送缓冲区满了。
我们设置超时时间为5秒,write会阻塞在那里5秒,然后返回。
参考资料:unix网络编程卷一
[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 4096 int 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 4096 int 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 client server.o:server.cpp g++ -c server.cpp client.o:client.cpp g++ -c client.cpp server:server.o g++ -o server server.o client:client.o g++ -o client client.o clean: 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 info will 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网络编程卷一
相关文章推荐
- linux和windows下用setsockopt设置SO_SNDTIMEO,SO_RCVTIMEO的参数的一点区别
- winsock下与linux下setsockopt(SO_SNDTIMEO,SO_RCVTIMEO)的区别
- linux和windows下用setsockopt设置SO_SNDTIMEO,SO_RCVTIMEO的参数的一点区别
- linux和windows下用setsockopt设置SO_SNDTIMEO,SO_RCVTIMEO的参数的一点区别
- 带超时时间的sendto和recvfrom(用SO_SNDTIMEO和SO_RCVTIMEO搞起)
- 68-套接字超时(SO_RCVTIMEO 与 SO_SNDTIMEO)
- winsock下与linux下setsockopt(SO_SNDTIMEO,SO_RCVTIMEO)的区别
- socket 请求接收完整的一个http响应(设置recv 接收超时选项SO_RCVTIMEO)
- socket 请求接收完整的一个http响应(设置recv 接收超时选项SO_RCVTIMEO)
- socket 请求接收完整的一个http响应(设置recv 接收超时选项SO_RCVTIMEO)
- setsockopt中SO_RCVTIMEO和SO_SNDTIMEO
- 使用socket option-SO_RCVTIMEO为recvfrom设置超时
- socket 请求接收完整的一个http响应(设置recv 接收超时选项SO_RCVTIMEO)
- linux网络编程二十:socket选项:SO_RCVTIMEO和SO_SNDTIMEO
- 如何在android studio中添加so文件
- SO_REUSEADDR测试及应用
- SO_REUSEADDR 套接字选项作用
- 终极SVN在windows下的配置 (二) -- Apache2.2配置Subversion(SVN)提示cannot load mod_dav_svn.so解决
- android动态加载.so,实现动态库升级
- Linux系统下.ko文件是什么文件?.so文件是什么文件?