TCP socket心跳包示例程序_xqhrs232的專欄-CSDN博客_setsockopt 心跳包
原文地址::TCP socket心跳包示例程序_神奕的專欄-CSDN博客_tcp心跳包
相關文章
1、Linux網絡編程--服務端判斷客戶端斷開的經驗方法?----Linux網絡編程--服務端判斷客戶端斷開的經驗方法_志存高遠-CSDN博客_info.tcpi_state
2、Socket心跳包機制及SO_KEEPALIVE套接口選項----Socket心跳包機制及SO_KEEPALIVE套接口選項_u012252959的博客-CSDN博客
3、TCP Keepalive HOWTO----TCP Keepalive HOWTO
在做游戲開發時,經常需要在應用層實現自己的心跳機制,即定時發送一個自定義的結構體(心跳包),讓對方知道自己還活著,以確保連接的有效性。
在TCP?socket心跳機制中,心跳包可以由服務器發送給客戶端,也可以由客戶端發送給服務器,不過比較起來,前者開銷可能更大。—— 這里實現的是由客戶端給服務器發送心跳包,基本思路是:
1) 服務器為每個客戶端保存了IP和計數器count,即map<fd, pair<ip, count>>
。服務端主線程采用 select 實現多路IO復用,監聽新連接以及接受數據包(心跳包),子線程用于檢測心跳:
- 如果主線程接收到的是心跳包,將該客戶端對應的計數器 count 清零;
- 在子線程中,每隔3秒遍歷一次所有客戶端的計數器 count:?
- 若 count 小于 5,將 count 計數器加 1;
- 若 count 等于 5,說明已經15秒未收到該用戶心跳包,判定該用戶已經掉線;
2) 客戶端則只是開辟子線程,定時給服務器發送心跳包(本示例中定時時間為3秒)。
下面是Linux下一個socket心跳包的簡單實現:
/*************************************************************************> File Name: Server.cpp> Author: SongLee> E-mail: lisong.shine@qq.com> Created Time: 2016年05月05日 星期四 22時50分23秒> Personal Blog: http://songlee24.github.io/************************************************************************/
#include<netinet/in.h> // sockaddr_in
#include<sys/types.h> // socket
#include<sys/socket.h> // socket
#include<arpa/inet.h>
#include<unistd.h>
#include<sys/select.h> // select
#include<sys/ioctl.h>
#include<sys/time.h>
#include<iostream>
#include<vector>
#include<map>
#include<string>
#include<cstdlib>
#include<cstdio>
#include<cstring>
using namespace std;
#define BUFFER_SIZE 1024enum Type {HEART, OTHER};struct PACKET_HEAD
{Type type;int length;
};void* heart_handler(void* arg);class Server
{
private:struct sockaddr_in server_addr;socklen_t server_addr_len;int listen_fd; // 監聽的fdint max_fd; // 最大的fdfd_set master_set; // 所有fd集合,包括監聽fd和客戶端fd fd_set working_set; // 工作集合struct timeval timeout;map<int, pair<string, int> > mmap; // 記錄連接的客戶端fd--><ip, count>
public:Server(int port);~Server();void Bind();void Listen(int queue_len = 20);void Accept();void Run();void Recv(int nums);friend void* heart_handler(void* arg);
};Server::Server(int port)
{bzero(&server_addr, sizeof(server_addr));server_addr.sin_family = AF_INET;server_addr.sin_addr.s_addr = htons(INADDR_ANY);server_addr.sin_port = htons(port);// create socket to listenlisten_fd = socket(PF_INET, SOCK_STREAM, 0);if(listen_fd < 0){cout << "Create Socket Failed!";exit(1);}int opt = 1;setsockopt(listen_fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
}Server::~Server()
{for(int fd=0; fd<=max_fd; ++fd){if(FD_ISSET(fd, &master_set)){close(fd);}}
}void Server::Bind()
{if(-1 == (bind(listen_fd, (struct sockaddr*)&server_addr, sizeof(server_addr)))){cout << "Server Bind Failed!";exit(1);}cout << "Bind Successfully.\n";
}void Server::Listen(int queue_len)
{if(-1 == listen(listen_fd, queue_len)){cout << "Server Listen Failed!";exit(1);}cout << "Listen Successfully.\n";
}void Server::Accept()
{struct sockaddr_in client_addr;socklen_t client_addr_len = sizeof(client_addr);int new_fd = accept(listen_fd, (struct sockaddr*)&client_addr, &client_addr_len);if(new_fd < 0){cout << "Server Accept Failed!";exit(1);}string ip(inet_ntoa(client_addr.sin_addr)); // 獲取客戶端IPcout << ip << " new connection was accepted.\n";mmap.insert(make_pair(new_fd, make_pair(ip, 0)));// 將新建立的連接的fd加入master_setFD_SET(new_fd, &master_set);if(new_fd > max_fd){max_fd = new_fd;}
} void Server::Recv(int nums)
{for(int fd=0; fd<=max_fd; ++fd){if(FD_ISSET(fd, &working_set)){bool close_conn = false; // 標記當前連接是否斷開了PACKET_HEAD head;recv(fd, &head, sizeof(head), 0); // 先接受包頭if(head.type == HEART){mmap[fd].second = 0; // 每次收到心跳包,count置0cout << "Received heart-beat from client.\n";}else{// 數據包,通過head.length確認數據包長度 } if(close_conn) // 當前這個連接有問題,關閉它{close(fd);FD_CLR(fd, &master_set);if(fd == max_fd) // 需要更新max_fd;{while(FD_ISSET(max_fd, &master_set) == false)--max_fd;}}}}
}void Server::Run()
{pthread_t id; // 創建心跳檢測線程int ret = pthread_create(&id, NULL, heart_handler, (void*)this);if(ret != 0){cout << "Can not create heart-beat checking thread.\n";}max_fd = listen_fd; // 初始化max_fdFD_ZERO(&master_set);FD_SET(listen_fd, &master_set); // 添加監聽fdwhile(1){FD_ZERO(&working_set);memcpy(&working_set, &master_set, sizeof(master_set));timeout.tv_sec = 30;timeout.tv_usec = 0;int nums = select(max_fd+1, &working_set, NULL, NULL, &timeout);if(nums < 0){cout << "select() error!";exit(1);}if(nums == 0){//cout << "select() is timeout!";continue;}if(FD_ISSET(listen_fd, &working_set))Accept(); // 有新的客戶端請求elseRecv(nums); // 接收客戶端的消息}
}// thread function
void* heart_handler(void* arg)
{cout << "The heartbeat checking thread started.\n";Server* s = (Server*)arg;while(1){map<int, pair<string, int> >::iterator it = s->mmap.begin();for( ; it!=s->mmap.end(); ){ if(it->second.second == 5) // 3s*5沒有收到心跳包,判定客戶端掉線{cout << "The client " << it->second.first << " has be offline.\n";int fd = it->first;close(fd); // 關閉該連接FD_CLR(fd, &s->master_set);if(fd == s->max_fd) // 需要更新max_fd;{while(FD_ISSET(s->max_fd, &s->master_set) == false)s->max_fd--;}s->mmap.erase(it++); // 從map中移除該記錄}else if(it->second.second < 5 && it->second.second >= 0){it->second.second += 1;++it;}else{++it;}}sleep(3); // 定時三秒}
}int main()
{Server server(15000);server.Bind();server.Listen();server.Run();return 0;
}
/*************************************************************************> File Name: Client.cpp> Author: SongLee> E-mail: lisong.shine@qq.com> Created Time: 2016年05月05日 星期四 23時41分56秒> Personal Blog: http://songlee24.github.io/************************************************************************/
#include<netinet/in.h> // sockaddr_in
#include<sys/types.h> // socket
#include<sys/socket.h> // socket
#include<arpa/inet.h>
#include<sys/ioctl.h>
#include<unistd.h>
#include<iostream>
#include<string>
#include<cstdlib>
#include<cstdio>
#include<cstring>
using namespace std;
#define BUFFER_SIZE 1024enum Type {HEART, OTHER};struct PACKET_HEAD
{Type type;int length;
};void* send_heart(void* arg); class Client
{
private:struct sockaddr_in server_addr;socklen_t server_addr_len;int fd;
public:Client(string ip, int port);~Client();void Connect();void Run();friend void* send_heart(void* arg);
};Client::Client(string ip, int port)
{bzero(&server_addr, sizeof(server_addr));server_addr.sin_family = AF_INET;if(inet_pton(AF_INET, ip.c_str(), &server_addr.sin_addr) == 0){cout << "Server IP Address Error!";exit(1);}server_addr.sin_port = htons(port);server_addr_len = sizeof(server_addr);// create socketfd = socket(AF_INET, SOCK_STREAM, 0);if(fd < 0){cout << "Create Socket Failed!";exit(1);}
}Client::~Client()
{close(fd);
}void Client::Connect()
{cout << "Connecting......" << endl;if(connect(fd, (struct sockaddr*)&server_addr, server_addr_len) < 0){cout << "Can not Connect to Server IP!";exit(1);}cout << "Connect to Server successfully." << endl;
}void Client::Run()
{pthread_t id;int ret = pthread_create(&id, NULL, send_heart, (void*)this);if(ret != 0){cout << "Can not create thread!";exit(1);}
}// thread function
void* send_heart(void* arg)
{cout << "The heartbeat sending thread started.\n";Client* c = (Client*)arg;int count = 0; // 測試while(1) {PACKET_HEAD head;head.type = HEART;head.length = 0; send(c->fd, &head, sizeof(head), 0);sleep(3); // 定時3秒++count; // 測試:發送15次心跳包就停止發送if(count > 15)break;}
}int main()
{Client client("127.0.0.1", 15000);client.Connect();client.Run();while(1){string msg;getline(cin, msg);if(msg == "exit")break;cout << "msg\n";}return 0;
}
可以看出,客戶端啟動以后發送了15次心跳包,然后停止發送心跳包。在經過一段時間后(3s*5),服務器就判斷該客戶端掉線,并斷開了連接。?