Ai
1 Star 0 Fork 0

周乃青/LinuxLearning

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
CalServer.cpp 2.00 KB
一键复制 编辑 原始数据 按行查看 历史
周乃青 提交于 2024-02-21 20:01 +08:00 . TcpServer done
#pragma once
#include "ThreadPool.cpp"
#include "TcpServer.cpp"
#include "CalTask.cpp"
#include "log.hpp"
#include <signal.h>
//构建计算器服务器
class CalServer{
const int size = 2048;
private:
Thread_Pool<CalTask> * Pool_ptr_;
MySocket::TcpServer Socket_;
int Socket_fd_ = -1;
public:
CalServer(const std::string& de_ip = "172.19.29.44",uint16_t de_port = 8081)
: Socket_(de_ip,de_port)
{
Pool_ptr_ = Thread_Pool<CalTask>::Getinstance();
if(Pool_ptr_ == nullptr){
lg(Fatal,"Pool_ptr_ is nullptr\n");
return;
}
Pool_ptr_->Create_thread();
}
~CalServer(){}
public:
//建立Tcp连接条件
bool Init(){
Socket_.BuildSocket();
Socket_fd_ = Socket_.Get_Server_fd();
if(Socket_fd_ < 0){
lg(Fatal,"BuildSocket failed\n");
return true;
}
Socket_.SocketBind();
Socket_.Socklisten();
lg(Info, "init server .... done");
return true;
}
//启动服务器
void Start(){
signal(SIGCHLD, SIG_IGN);
signal(SIGPIPE, SIG_IGN);
char ReadBuffer[size];
while(true){
//接受用户请求
std::string client_ip;
uint16_t client_port;
int client_fd = Socket_.SockAccept(client_ip,client_port);
if(client_fd < 0){
lg(Warning,"SockAccept error\n");
continue;
}
lg(Info, "accept a new link, sockfd: %d, clientip: %s, clientport: %d", client_fd, client_ip.c_str(), client_port);
int n = read(client_fd,ReadBuffer,sizeof(ReadBuffer));
ReadBuffer[n] = 0;
std::string TaskStr(ReadBuffer);
printf("receives mess from client : %s",ReadBuffer);
if(n < 0){
lg(Warning,"read error\n");
break;
}
CalTask task(client_fd,client_ip,client_port,TaskStr);
Pool_ptr_->Push(task);
}
}
};
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/c-learning--c_0/LinuxLearning.git
git@gitee.com:c-learning--c_0/LinuxLearning.git
c-learning--c_0
LinuxLearning
LinuxLearning
master

搜索帮助