TCP通信的两方,其中一方正常关闭(调用close函数或程序正常退出)正处于连接状态的TCP套接字,这时双方的TCP将完成协商关闭连接的四次握手,握手结束以后,另一方的应用层将检测到TCP连接对应的套接字可读,执行read函数的时候倘若返回0,则说明对方已经主动断开了TCP连接,这边只需要调用close函数关闭对应套接字,回收相关资源即可。整个过程就是正常网络连接断开的处理过程。
监控这种正常网络连接断开的情况,使用的最普遍的办法是采用select机制,将所有建立好TCP连接的套接字交给select函数去维护他们的可读状态,select函数维护一组套接字的可读属性,一旦该组集合中有套接字可读,select将返回当前可读的套接字数量,然后调用FD_ISSET宏判断找出当前可读的所有套接字,调用read函数读取这些套接字中的数据,倘若read返回0,即没有读到任何数据,我们则可以断定该连接已经被对端关闭。
下面两个C语言程序分别模拟了服务端跟客户端采用select监控正常网络断开的实际处理方式,先启动server,监听1987端口,再启动client,指定服务器IP,建立连接。可以同时启动多个client连接server,分别结束(Ctrl+C或kill)服务端或客户端,观察客户端或服务端是否能够检测到连接断开。
服务端源代码:server.c
#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h> #include <sys/types.h> #include <sys/socket.h> #include <sys/select.h> #include <netinet/in.h> #include <errno.h> #define MAX_SESSION 10 #define MAX_BUF_LEN 1024 #define PORT 1987 #define SLEEP_TIME 10 int main(void) { int listen_fd; int accept_fd[MAX_SESSION] = {0}; struct sockaddr_in addr; struct timeval select_timeval; fd_set readfds; int max_fd; int fd_num; int new_fd; int i; int recv_len; char recvBuf[MAX_BUF_LEN] = {0}; memset(&addr, 0, sizeof(addr)); addr.sin_family = AF_INET; addr.sin_port = htons(PORT); addr.sin_addr.s_addr = htonl(INADDR_ANY); listen_fd = socket(AF_INET, SOCK_STREAM, 0); if (listen_fd < 0) { perror("socket"); return -1; } if (bind(listen_fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) { perror("bind"); return -1; } if (listen(listen_fd, 3) < 0) { perror("listen"); return -1; } select_timeval.tv_sec = 0; select_timeval.tv_usec = 0; while (1) { FD_ZERO(&readfds); max_fd = listen_fd; FD_SET(listen_fd, &readfds); for (i = 0; i < MAX_SESSION; i++) { if (accept_fd[i] > max_fd) max_fd = accept_fd[i]; if (accept_fd[i] > 0) FD_SET(accept_fd[i], &readfds); } fd_num = select(max_fd+1, &readfds, NULL, NULL, &select_timeval); if (fd_num < 0) { perror("select"); return -1; } else if (0 == fd_num) { select_timeval.tv_sec = SLEEP_TIME; select_timeval.tv_usec = 0; printf("select timeout...\n"); continue; } if (FD_ISSET(listen_fd, &readfds)) { new_fd = accept(listen_fd, NULL, NULL); printf("new accept sock: %d\n", new_fd); if (new_fd < 0) { perror("accept"); continue; } for (i=0; i<MAX_SESSION; i++) { if (0 == accept_fd[i]) { accept_fd[i] = new_fd; break; } } if (MAX_SESSION == i) { close(new_fd); printf("beyoned the MAX_SESSION, so closed the new accept sock: %d\n", new_fd); } continue; } for (i=0; i<MAX_SESSION; i++) { if (FD_ISSET(accept_fd[i], &readfds)) { memset(recvBuf, 0, sizeof(recvBuf)); recv_len = read(accept_fd[i], recvBuf, sizeof(recvBuf)); if (recv_len > 0) { printf("Recv msg from sock %d : %s\n", accept_fd[i], recvBuf); } else if (0 == recv_len) { printf("Disconnected with the sock %d\n", accept_fd[i]); close(accept_fd[i]); accept_fd[i] = 0; } else { perror("read"); close(accept_fd[i]); accept_fd[i] = 0; } } } } return 0; }
客户端源代码:client.c
#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h> #include <sys/types.h> #include <sys/socket.h> #include <sys/select.h> #include <netinet/in.h> #include <arpa/inet.h> #include <errno.h> #define MAX_BUF_LEN 1024 #define PORT 1987 #define SLEEP_TIME 10 int main(int argc, char *argv[]) { int connect_fd; struct sockaddr_in addr; struct timeval select_timeval; fd_set readfds; int fd_num; int recv_len; char recvBuf[MAX_BUF_LEN] = {0}; if (argc != 2) { printf("Please set the arguments as follow:\n"); printf("%s <serverIP>\n", argv[0]); return -1; } memset(&addr, 0, sizeof(addr)); addr.sin_family = AF_INET; addr.sin_port = htons(PORT); addr.sin_addr.s_addr = inet_addr(argv[1]); connect_fd = socket(AF_INET, SOCK_STREAM, 0); if (connect_fd < 0) { perror("socket"); return -1; } if (connect(connect_fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) { perror("connect"); close(connect_fd); return -1; } select_timeval.tv_sec = 0; select_timeval.tv_usec = 0; while (1) { FD_ZERO(&readfds); FD_SET(connect_fd, &readfds); fd_num = select(connect_fd+1, &readfds, NULL, NULL, &select_timeval); if (fd_num < 0) { perror("select"); return -1; } else if (0 == fd_num) { select_timeval.tv_sec = SLEEP_TIME; select_timeval.tv_usec = 0; printf("select timeout...\n"); continue; } if (FD_ISSET(connect_fd, &readfds)) { memset(recvBuf, 0, sizeof(recvBuf)); recv_len = read(connect_fd, recvBuf, sizeof(recvBuf)); if (recv_len > 0) { printf("Recv msg from sock %d : %s\n", connect_fd, recvBuf); } else if (0 == recv_len) { printf("Disconnected with the sock %d\n", connect_fd); close(connect_fd); break; } else { perror("read"); close(connect_fd); break; } } } return 0; }
除非注明,文章均为CppLive 编程在线原创,转载请注明出处,谢谢。