可以参考TCP连接保活机制来设计应用层的连接状态监测,同时需要注意到有两个关键点:
1.需要使用定时器,这可以通过使用 I/O 复用自身的机制来实现,这点可以先看一下《使用select实现定时任务》;
2.需要设计一个 PING-PONG 的协议。
设计一个PING-PONG协议的的话,首先就需要一个客户端和服务器都认的固定结构体:
typedef struct {
u_int32_t type;
char data[1024];
} messageObject;
#define MSG_PING 1
#define MSG_PONG 2
#define MSG_TYPE1 11
#define MSG_TYPE2 21
这里把前 4 个字节标识了消息类型,为了简单,这里设计了MSG_PING、MSG_PONG、MSG_TYPE 1和MSG_TYPE 2四种消息类型。
服务器端的代码pingserver.c
如下:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/select.h>
typedef struct {
u_int32_t type;
char data[1024];
} messageObject;
#define MSG_PING 1
#define MSG_PONG 2
#define MSG_TYPE1 11
#define MSG_TYPE2 21
#define MAXLINE 4096
static int count;
int main(int argc, char **argv) {
if (argc != 3) {
printf("usage: tcpsever <port> or <sleepingtime>");
exit(1);
}
int sleepingTime = atoi(argv[2]);
int listenfd;
listenfd = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in server_addr;
bzero(&server_addr, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
server_addr.sin_port = htons(atoi(argv[1]));
int rt1 = bind(listenfd, (struct sockaddr *) &server_addr, sizeof(server_addr));
if (rt1 < 0) {
printf("bind failed ");
exit(1);
}
int rt2 = listen(listenfd, 1024);
if (rt2 < 0) {
printf("listen failed ");
exit(1);
}
int connfd;
struct sockaddr_in client_addr;
socklen_t client_len = sizeof(client_addr);
if ((connfd = accept(listenfd, (struct sockaddr *) &client_addr, &client_len)) < 0) {
printf("bind failed ");
exit(1);
}
messageObject message;
count = 0;
for (;;) {
int n = read(connfd, (char *) &message, sizeof(messageObject));
if (n < 0) {
printf("error read");
exit(1);
} else if (n == 0) {
printf("client closed \n");
continue;
}
printf("received %d bytes\n", n);
count++;
switch (ntohl(message.type)) {
case MSG_TYPE1 :
printf("process MSG_TYPE1 \n");
break;
case MSG_TYPE2 :
printf("process MSG_TYPE2 \n");
break;
case MSG_PING: {
messageObject pong_message;
pong_message.type = MSG_PONG;
sleep(sleepingTime);
ssize_t rc = send(connfd, (char *) &pong_message, sizeof(pong_message), 0);
if (rc < 0)
printf("send failure");
break;
}
default :
printf("unknown message type (%d)\n", ntohl(message.type));
}
}
}
gcc pingserver.c -o pingserver
进行编译。
客户端的代码pingclient.c
如下:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/select.h>
typedef struct {
u_int32_t type;
char data[1024];
} messageObject;
#define MSG_PING 1
#define MSG_PONG 2
#define MSG_TYPE1 11
#define MSG_TYPE2 21
#define MAXLINE 4096
#define KEEP_ALIVE_TIME 10
#define KEEP_ALIVE_INTERVAL 3
#define KEEP_ALIVE_PROBETIMES 3
int main(int argc, char **argv) {
if (argc != 3) {
printf("usage: select01 <IPaddress> or <Port>");
}
int socket_fd;
socket_fd = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in server_addr;
bzero(&server_addr, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(atoi(argv[2]));
inet_pton(AF_INET, argv[1], &server_addr.sin_addr);
socklen_t server_len = sizeof(server_addr);
int connect_rt = connect(socket_fd, (struct sockaddr *) &server_addr, server_len);
if (connect_rt < 0) {
printf("connect failed ");
exit(1);
}
char recv_line[MAXLINE + 1];
int n;
fd_set readmask;
fd_set allreads;
struct timeval tv;
int heartbeats = 0;
tv.tv_sec = KEEP_ALIVE_TIME;
tv.tv_usec = 0;
messageObject messageObject;
FD_ZERO(&allreads);
FD_SET(socket_fd, &allreads);
for (;;) {
readmask = allreads;
int rc = select(socket_fd + 1, &readmask, NULL, NULL, &tv);
if (rc < 0) {
printf("select failed");
exit(1);
}
if (rc == 0) {
if (++heartbeats > KEEP_ALIVE_PROBETIMES) {
printf("connection dead\n");
exit(1);
}
printf("sending heartbeat #%d\n", heartbeats);
messageObject.type = htonl(MSG_PING);
rc = send(socket_fd, (char *) &messageObject, sizeof(messageObject), 0);
if (rc < 0) {
printf("send failure");
exit(1);
}
tv.tv_sec = KEEP_ALIVE_INTERVAL;
continue;
}
if (FD_ISSET(socket_fd, &readmask)) {
n = read(socket_fd, recv_line, MAXLINE);
if (n < 0) {
printf("read error");
exit(1);
} else if (n == 0) {
printf("server terminated \n");
exit(1);
}
printf("received heartbeat, make heartbeats to 0 \n");
heartbeats = 0;
tv.tv_sec = KEEP_ALIVE_TIME;
}
}
}
gcc pingclient.c -o pingclient
进行编译。
第一种情况:服务器状态不正常
模拟服务器端崩溃的的现象:./pingserver 8080 60
监听在8080
端口,然后设置60
秒的监听间隔,也就是服务器端接收到PING
报文之后,在六十秒内不做任何回应,相当于模拟服务器端出现崩溃的现象。
客户端执行的是./pingclient 127.0.0.1 8080
。
在服务器上按下 ctrl + c可以结束程序。
第二种情况:服务器状态正常
./pingserver 8080 5
监听在8080
端口,然后设置5
秒的监听间隔,也就是服务器端接收到PING
报文之后,在五秒内不做任何回应,这里只是模拟服务器正常处理业务请求。
客户端执行的是./pingclient 127.0.0.1 8080
。
发现只要服务器端不停止,那么客户端就没有停止监测。
可以看到服务器一停止,那么客户端就停止了。
此文章为11月Day 16学习笔记,内容来源于极客时间《网络编程实战》。