Socket close, socket
#include <iostream>#include <sys/types.h>#include <sys/socket.h>#include <netinet/in.h>#include <arpa/inet.h>#include <unistd.h>#include <errno.h>#include <stdio.h>#include <string.h>#include <stdlib.h>bool server(char *ip,int port){ int socketfd = 0; if(-1 ==(socketfd = socket(AF_INET,SOCK_STREAM,0))){ printf("create socket faile,errno:%d\n",errno); return false; } sockaddr_in serverAddr; memset(&serverAddr,0,sizeof(serverAddr)); serverAddr.sin_family = AF_INET; serverAddr.sin_port = htons(port); serverAddr.sin_addr.s_addr = inet_addr(ip); if(-1==bind(socketfd,(struct sockaddr *)&serverAddr,sizeof(serverAddr))){ printf("bind socket fail,errno:%d\n",errno); return false; } if(-1==listen(socketfd,10)){ printf("listen socket fail,errno:%d\n",errno); return false; } printf("server has started...\n"); int confd = 0; sockaddr_in clientAddr; memset(&clientAddr,0,sizeof(clientAddr)); int sockLen = 0; if(-1==(confd = accept(socketfd,(struct sockaddr *)&clientAddr,(socklen_t*)&sockLen))){ printf("accept socket fail,errno:%d\n",errno); return false; } int len = 0; while(1){ char recvBuf[64]={0}; len = recv(confd,recvBuf,sizeof(recvBuf),0); printf("len:%d,str:%s\n",len,recvBuf); if(len ==0){ printf("client shutdown socket...\n"); break; } } while(1){ sleep(3); } return true;}bool client(char *ip,int port){ int socketfd = 0; if(-1 == (socketfd = socket(AF_INET,SOCK_STREAM,0))){ printf("create socket fail,errno:%d\n",errno); return false; } sockaddr_in serverAddr; memset(&serverAddr,0,sizeof(serverAddr)); serverAddr.sin_family = AF_INET; serverAddr.sin_port = htons(port); serverAddr.sin_addr.s_addr = inet_addr(ip); if(-1==connect(socketfd,(struct sockaddr*)&serverAddr,sizeof(serverAddr))){ printf("connect socket fail,errno:%d\n",errno); return false; } char * sendBuf = "hello world"; send(socketfd,sendBuf,strlen(sendBuf)+1,0); while(1){ sleep(3); } return true;}int main(int argc, char *argv[]){ if(argc!=4){ printf("usage:CloseWaitTest [0|1] ip port. 0:server,1:client\n"); return 1; } if(atoi(argv[1])==0){ server(argv[2],atoi(argv[3])); } if(atoi(argv[1])==1){ client(argv[2],atoi(argv[3])); } return 0;}
Test 1: the connection is closed when the process exits. If the client process is shut down normally, the server will receive a request from the client. The connection socket status of the server enters CLOSE_WAIT. Because the server does not actively send a request to close the client, the client socket status is in FIN_WAIT2.
Normal Operation
The listening socket on the server side is in the LISTEN state, and the direct connection socket between the server and client is in the ESTABLISHED state.
Ctrl + c Exit the client
The server socket is in the CLOSE_WAIT state, and the client process socket is in the FIN_WAIT2 state. After the FIN_WAIT2 times out, the system recycles the socket resource. The default timeout value is 60 seconds, you can view it through/sbin/sysctl-a | grep fin_timeout.
The server has not been active, so it is always in the CLOSE_WAIT status.
Test 2: