日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

SO_SNDTIMEO和SO_RCVTIMEO

發布時間:2025/6/15 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。