TCP_DEFER_ACCEPT
生活随笔
收集整理的這篇文章主要介紹了
TCP_DEFER_ACCEPT
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
? ? ? ? ? ? 該選項的意思是:當接收到第一個數據之后,才會創建連接,這是為防止空連接的攻擊,直接看代碼吧。
?
[mapan@localhost sockOption]$ ls client.cpp makefile server.cpp [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> #include <netinet/tcp.h> #define MAXLINE 4096int main() {int listenfd,acceptfd;int val=10;struct sockaddr_in servaddr;listenfd=socket(AF_INET,SOCK_STREAM,0);int ret= setsockopt(listenfd,SOL_TCP,TCP_DEFER_ACCEPT,&val,sizeof(val));printf("ret=%d\n",ret);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 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));getchar();write(connfd,sendbuf,sizeof(sendbuf));getchar(); close(connfd);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 ret=0 [mapan@localhost sockOption]$ ./client 127.0.0.1看看此時的網絡狀態:
?
[mapan@localhost net_04S]$ netstat -na | grep 6666 tcp 0 0 0.0.0.0:6666 0.0.0.0:* LISTEN tcp 0 0 127.0.0.1:6666 127.0.0.1:59226 SYN_RECV tcp 0 0 127.0.0.1:59226 127.0.0.1:6666 ESTABLISHED [mapan@localhost net_04S]$
服務端并沒有建立連接,其實服務端沒有調用accept。服務端忽略了客戶端最后發送過來了一個ACK,僅僅把這個socket標記位acked,然后丟棄它。然后就是服務端啟動超時重傳機制,重傳SYN/ACK,達到一定次數之后,然后開始計時到val秒。我這邊linux內核是2.6.32的,超時時間過之后,客戶端還沒有數據發送過來,服務端就會丟掉這個請求,2.6.18內核會刪除SYN_RECV這個狀態的連接。
?
如果重傳SYN/ACK時,客戶端及時響應ACK但是沒有數據到來,那么服務端會定時重傳SYN/ACK,保持連接狀態SYN/ACK不變。
?
?
?
?
?
參考地址:http://blog.sina.com.cn/s/blog_3fde82520101fmxw.html
?
?
?
?
?
?
?
總結
以上是生活随笔為你收集整理的TCP_DEFER_ACCEPT的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: close和SO_LINGER
- 下一篇: memset的妙用