TcpServer 服务器优化之后,加了多线程,对心跳包进行优化

ops/2024/12/14 11:08:35/

TcpServer 服务器优化之后,加了多线程,对心跳包进行优化

TcpServer.h

#ifndef TCPSERVER_H
#define TCPSERVER_H#include <iostream>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <vector>
#include <map>
#include <string>
#include <ctime>// 引入静态链接库
#pragma comment(lib, "ws2_32.lib")
#define HEARTBEATTIME  1000class TcpServer {
public:TcpServer();~TcpServer();// 启动服务器,监听指定端口bool start(int port);// 停止服务器void stop();// 发送数据给指定客户端int sendData(SOCKET clientSocket, const char* data, int dataLength);// 处理服务器业务逻辑,通常在循环中调用void handle();//链接static DWORD WINAPI ThreadAccept(LPVOID lpParam);//接收数据static DWORD WINAPI ThreadRecvData(LPVOID lpParam);//心跳包static DWORD WINAPI ThreadHeartBeat(LPVOID lpParam);
public:std::vector<SOCKET> socketsToRemove;BOOL  m_bExit;//程序是否关闭BOOL m_bHeartBeat;//是否启用心跳包int heartbeatInterval;  // 心跳包间隔时间(秒)
private:SOCKET listenSocket;std::vector<SOCKET> clientSockets;std::map<SOCKET, std::time_t> clientLastHeartbeatTime;// 设置套接字为非阻塞模式bool setSocketNonBlocking(SOCKET socket);// 接受新的客户端连接void acceptNewClients();// 接收客户端数据void receiveClientData();// 发送心跳包给客户端,并检测客户端响应void sendHeartbeatsAndCheck();// 移除已断开连接的客户端void removeDisconnectedClients(std::vector<SOCKET> &socketsToRemove);
};#endif

TcpServer.cpp

#include "TcpServer.h"// 构造函数,初始化相关成员变量
TcpServer::TcpServer() : listenSocket(INVALID_SOCKET),
heartbeatInterval(5), m_bExit(false), m_bHeartBeat(false)
{WSADATA wsaData;int result = WSAStartup(MAKEWORD(2, 2), &wsaData);if (result != 0) {std::cerr << "WSAStartup failed: " << result << std::endl;}
}// 析构函数,关闭套接字并清理WinSock环境
TcpServer::~TcpServer() 
{stop();WSACleanup();
}// 启动服务器,监听指定端口
bool TcpServer::start(int port)
{listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);if (listenSocket == INVALID_SOCKET){std::cerr << "Socket creation failed: " << WSAGetLastError() << std::endl;return false;}if (!setSocketNonBlocking(listenSocket)) {std::cerr << "Failed to set listen socket non-blocking" << std::endl;closesocket(listenSocket);return false;}sockaddr_in serverAddr;serverAddr.sin_family = AF_INET;serverAddr.sin_addr.s_addr = INADDR_ANY;serverAddr.sin_port = htons(port);int result = bind(listenSocket, (sockaddr*)&serverAddr, sizeof(serverAddr));if (result == SOCKET_ERROR){std::cerr << "Bind failed: " << WSAGetLastError() << std::endl;closesocket(listenSocket);return false;}result = listen(listenSocket, SOMAXCONN);if (result == SOCKET_ERROR){std::cerr << "Listen failed: " << WSAGetLastError() << std::endl;closesocket(listenSocket);return false;}return true;
}// 停止服务器
void TcpServer::stop()
{if (listenSocket != INVALID_SOCKET) {closesocket(listenSocket);listenSocket = INVALID_SOCKET;}for (SOCKET clientSocket : clientSockets) {closesocket(clientSocket);}clientSockets.clear();clientLastHeartbeatTime.clear();
}// 设置套接字为非阻塞模式
bool TcpServer::setSocketNonBlocking(SOCKET socket) 
{u_long iMode = 1;int result = ioctlsocket(socket, FIONBIO, &iMode);if (result == SOCKET_ERROR){std::cerr << "ioctlsocket failed: " << WSAGetLastError() << std::endl;return false;}return true;
}// 发送数据给指定客户端
int TcpServer::sendData(SOCKET clientSocket, const char* data, int dataLength) 
{if (clientSocket == INVALID_SOCKET) {std::cerr << "Invalid client socket, cannot send data" << std::endl;return SOCKET_ERROR;}int totalBytesSent = 0;while (totalBytesSent < dataLength) {int bytesSent = ::send(clientSocket, data + totalBytesSent, dataLength - totalBytesSent, 0);if (bytesSent == SOCKET_ERROR){if (WSAGetLastError() == WSAEWOULDBLOCK) {// 暂时无法发送,等待下次尝试continue;}return SOCKET_ERROR;}totalBytesSent += bytesSent;}return totalBytesSent;
}// 接受新的客户端连接
void TcpServer::acceptNewClients()
{SOCKET newClientSocket = accept(listenSocket, NULL, NULL);if (newClientSocket == INVALID_SOCKET){if (WSAGetLastError() != WSAEWOULDBLOCK) {std::cerr << "Accept failed: " << WSAGetLastError() << std::endl;}return;}else{std::cout << "Accept success: " << newClientSocket << std::endl;}if (!setSocketNonBlocking(newClientSocket)) {std::cerr << "Failed to set client socket non-blocking" << std::endl;closesocket(newClientSocket);return;}clientSockets.push_back(newClientSocket);clientLastHeartbeatTime[newClientSocket] = std::time(nullptr);
}// 接收客户端数据
void TcpServer::receiveClientData()
{for (size_t i = 0; i < clientSockets.size(); ++i){SOCKET clientSocket = clientSockets[i];char buffer[1024];int bytesReceived = ::recv(clientSocket, buffer, sizeof(buffer), 0);if (bytesReceived == SOCKET_ERROR) {if (WSAGetLastError() == WSAEWOULDBLOCK) {// 暂时无数据可读,继续检查下一个客户端continue;}}else{buffer[bytesReceived] = '\0';std::string receivedData(buffer);// 在这里可以根据接收到的数据进行具体业务逻辑处理,比如解析命令等std::cout << "Received from client " << clientSocket << ": " << receivedData << std::endl;clientLastHeartbeatTime[clientSocket] = std::time(nullptr);std::string heartbeatData = buffer;heartbeatData+="    recvok:";int sentBytes = sendData(clientSocket, heartbeatData.c_str(), heartbeatData.length());}}
}// 发送心跳包给客户端,并检测客户端响应
void TcpServer::sendHeartbeatsAndCheck()
{const char* heartbeatData = "HEARTBEAT";  // 简单的心跳包内容,可自定义int dataLength = strlen(heartbeatData);for (auto& clientPair : clientLastHeartbeatTime){SOCKET clientSocket = clientPair.first;std::time_t& lastHeartbeatTime = clientPair.second;std::time_t currentTime = std::time(nullptr);if (currentTime - lastHeartbeatTime > heartbeatInterval) {// 超过心跳间隔时间没收到心跳响应,认为客户端连接异常socketsToRemove.push_back(clientSocket);continue;}int sentBytes = sendData(clientSocket, heartbeatData, dataLength);if (sentBytes == SOCKET_ERROR){// 发送心跳包失败,认为客户端连接可能有问题socketsToRemove.push_back(clientSocket);continue;}}
}// 移除已断开连接的客户端(更新函数定义,无参数)
void TcpServer::removeDisconnectedClients(std::vector<SOCKET>&socketsToRemove)
{for (SOCKET socketToRemove : socketsToRemove){auto it = std::find(clientSockets.begin(), clientSockets.end(), socketToRemove);if (it != clientSockets.end()) {std::cout << "Remove :"<< * it << std::endl;clientSockets.erase(it);clientLastHeartbeatTime.erase(socketToRemove);}}
}//接收链接线程
DWORD WINAPI TcpServer::ThreadAccept(LPVOID lpParam)
{TcpServer* t_Server = static_cast<TcpServer*>(lpParam);while (t_Server->m_bExit==false){t_Server->acceptNewClients();}return 0;
}
//接收数据
DWORD WINAPI TcpServer::ThreadRecvData(LPVOID lpParam)
{TcpServer* t_Server = static_cast<TcpServer*>(lpParam);while (t_Server->m_bExit == false){t_Server->receiveClientData();}return 0;
}//心跳包
DWORD WINAPI TcpServer::ThreadHeartBeat(LPVOID lpParam)
{TcpServer* t_Server = static_cast<TcpServer*>(lpParam);while (t_Server->m_bExit == false){Sleep(HEARTBEATTIME);t_Server->sendHeartbeatsAndCheck();if (t_Server->heartbeatInterval > 0){t_Server->removeDisconnectedClients(t_Server->socketsToRemove);}}return 0;
}
// 处理服务器业务逻辑,通常在循环中调用
void TcpServer::handle() 
{//创建4个线程,分别进行接收链接  接收数据 发送数据 发送心跳包// 创建线程,传入当前对象指针作为参数,线程启动函数为 SendHeartbeatHANDLE acceptThreadHandle = CreateThread(NULL, 0, ThreadAccept, this, 0, NULL);if (acceptThreadHandle == NULL){std::cerr << "Create accept thread failed: " << GetLastError() << std::endl;}else{std::cerr << "Create accept thread success: " << acceptThreadHandle << std::endl;}HANDLE recvDatatThreadHandle = CreateThread(NULL, 0, ThreadRecvData, this, 0, NULL);if (acceptThreadHandle == NULL){std::cerr << "Create recvData thread failed: " << GetLastError() << std::endl;}else{std::cerr << "Create recvData thread success: " << recvDatatThreadHandle << std::endl;}if (m_bHeartBeat == true){HANDLE heartBeatThreadHandle = CreateThread(NULL, 0, ThreadHeartBeat, this, 0, NULL);if (acceptThreadHandle == NULL){std::cerr << "Create heartBeat thread failed: " << GetLastError() << std::endl;}else{std::cerr << "Create heartBeat thread success: " << heartBeatThreadHandle << std::endl;}}}

main.cpp

#include "TcpServer.h"int main()
{TcpServer server;server.heartbeatInterval = 30;server.m_bHeartBeat = true;if (server.start(8080)) {while (true) {server.handle();// 可以在这里添加适当的延时,避免过于频繁地循环处理,消耗过多CPU资源Sleep(100);break;}}else{std::cout << "server initiatefail" << std::endl;}Sleep(1000000);return 0;
}

在这里插入图片描述
在这里插入图片描述


http://www.ppmy.cn/ops/141797.html

相关文章

C语言(指针基础练习)

删除数组中的元素 数组的元素在内存地址中是连续的&#xff0c;不能单独删除数组中的某个元素&#xff0c;只能覆盖。 #include <stdio.h> #include <stdbool.h>// 函数声明 int deleteElement(int arr[], int size, int element);int main() {int arr[] {1, 2, 3…

图形学笔记 - 5. 光线追踪 - RayTracing

Whitted-Style Ray tracing 为什么要光线追踪 光栅化不能很好地处理全局效果 软阴影尤其是当光线反射不止一次的时候 栅格化速度很快&#xff0c;但质量相对较低 光线追踪是准确的&#xff0c;但速度很慢 光栅化&#xff1a;实时&#xff0c;光线追踪&#xff1a;离线~10K …

ios上架构建版本没苹果电脑怎么上传

在app store上架的时候&#xff0c;遇到下图的问题&#xff1a; 点击蓝色加号的时候&#xff0c;并没有构建版本可以选择 从图中可以看出&#xff0c;它给我们推荐了很多上传工具&#xff0c;比如xcode、transporter或命令行工具之类的&#xff0c;但是这些工具都是只能在苹果…

Debedium如何忽略Oracle的purge命令

报错 截至目前3.0版本&#xff0c;Debezium的Oracle Connector并不支持purge table这个指令。 所以&#xff0c;在使用Debezium解析Oracle变更的时候&#xff0c;如果在源端执行了类似 purge table "$BIN… 的语句&#xff0c;就会导致Debezium罢工&#xff0c;日志里显…

双城联动 | 桥田智能获汽车装备卓越供应商奖

12月10日&#xff0c;2024年扶轮奖颁奖典礼在上海金陵紫金山大酒店举行&#xff0c;桥田智能设备有限公司获2024扶轮奖“汽车装备卓越供应商奖 ”荣誉称号。桥田智能市场部负责人张苏娜出席本次颁奖仪式。 桥田智能市场部负责人张苏娜 (右三&#xff09; 本次获得“汽车装备卓…

EDA - Spring Boot构建基于事件驱动的消息系统

文章目录 概述事件驱动架构的基本概念工程结构Code创建事件和事件处理器创建事件总线创建消息通道和发送逻辑创建事件处理器消息持久化创建消息发送事件配置 Spring Boot 启动类测试消息消费运行项目 概述 在微服务架构和大规模分布式系统中&#xff0c;事件驱动架构&#xff…

avue-crud 同时使用 column 与 group 的问题

场景一&#xff1a;在使用option 中的column 和 group 进行表单数据新增操作时&#xff0c;进行里面的控件操作时&#xff0c;点击后卡死问题&#xff0c;文本没问题 其它比如下拉&#xff0c;单选框操作&#xff0c;当删除 column 中的字段后&#xff0c; group 中的可以操作 …

CNN时间序列预测Matlab实现

代码&#xff1a; %% 清空环境变量 warning off % 关闭报警信息 close all % 关闭开启的图窗 clear % 清空变量 clc % 清空命令行%% 导入数据&#xff08;时间序列的单列数据&#xff09; result xlsread…