Reprinted from: zxh2075 's Column
In the socket heartbeat mechanism, the heartbeat packet can be sent to the client by the server, or it can be sent to the server by the client, although the cost may be larger compared to the former. This article realizes that the client sends a heartbeat packet to the server, and the server does not have to return the reply packet, but instead detects the heartbeat anomaly by judging the count flag value in the customer's online session record, which determines whether the client has disconnected and deleted its online session record.
Basic ideas:
The ① client sends a heartbeat packet to the server at timed intervals (3 seconds in the case);
The ② server creates a heartbeat-detected thread that adds 1 operations to the counter in the user's online session record every 3 seconds (the initial value is 0);
Each time the ③ server receives a heartbeat packet from the client, it will clear the counter in its online session record by 0;
④ when the heartbeat detection thread detects that a user counter has been added to the value of 5 o'clock (indicating that the user has not received a heartbeat package for 15 seconds), the user is determined to be disconnected and purged from the session record.
(Note: The session record in the case is implemented with a linked list)
C Language Code implementation case:
Client:/** * Function: Client heartbeat packet Send thread function * * parameter: line Cheng (can transmit socket) * * return value: null*/void*send_heart (void*addr) { while(1) {PD->data_type = Heart;//Heart: Packet Type, PD is a packet struct body pointerWrite (CLIENT_SOCKFD,PD,sizeof(Data_pack)); Sleep (3);//Timing 3 seconds } returnNULL;}/***********************************************//***********************************************/Server side: Typefdefstructsession{Charpeerip[ -]; Charname[Ten]; intSOCKFD; intcount; structSession *Next;} s_t;/** * Function: Handle the user Heartbeat packet event, clear the counter in its session record 0 * * Parameters: Socket and Packet pointer * * return value: None*/voidHeart_handler (intSockfd,data_pack *PD) {s_t*cur = shead->next;//shead global variable header pointer for user online session while(NULL! =cur) { if(strcmp (cur->name,pd->name) = =0) {cur->count =0;//Zeroing the counter to indicate that the client named Pd->name is still aliveprintf"Client IP:%s: User%s is connected properly \ n",cur->peerip,pd->name); } cur= cur->Next; }}/** * Function: Heartbeat detection thread function * * Parameter: None * * return value: None*/void*heart_check (void*p) {printf ("heartbeat detection thread is turned on! \ n"); while(1) {Check_handler ();//heartbeat detection processing functionSleep3);//Timing 3 seconds } returnNULL;}/** * Function: heartbeat detection processing function * * Parameter: None * * return value: None*/voidCheck_handler () {s_t*temp = NULL;//used to release nodes.s_t **ppnode = &shead->Next; while(NULL! = (*Ppnode)) { if((*ppnode)->count = =5) {printf ("Client IP:%s: User%s has been dropped!! \ n", (*ppnode)->peerip, (*Ppnode)-name); Close ((*ppnode)->SOCKFD);//Close the peer sockettemp = *ppnode;//Store this node address.*ppnode = (*ppnode)->next;//Move Pointer Free(temp);//release the node.temp =NULL; Continue; } Else if((*ppnode)->count >0) {printf ("Client IP:%s: User%s connection Exception! \ n", (*ppnode)->peerip, (*ppnode),name); (*ppnode)->count++; printf ("count =%d\n", (*ppnode)->count);//View counter ContentsPpnode = & ((*ppnode)->next);//member Pointers Continue; } Else if((*ppnode)->count = =0){ (*ppnode)->count++; printf ("count =%d\n", (*ppnode)->count);//View counter ContentsPpnode = & ((*ppnode)->next);//member Pointers } Else; } }
[GO] socket heartbeat Packet exception detection C language implementation, server and client code case