#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#ifdef WIN32 //windows系统下的socket
#include <Windows.h>
#define socklen_t int
#else //linux系统下的socket
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <arpa/inet.h>
#define closesocket close
#endif
#include <thread>
using namespace std;
class TcpThread
{
public:
void Main()
{
//接收用户数据,并回信息
char buf[1024] = { 0 };
for (;;)
{
int recv_len = recv(client, buf, sizeof(buf) - 1, 0);
if (recv_len < 0)
{
break;
}
buf[recv_len] = '\0';
if (NULL != (strstr(buf, "quit")))
{
send(client, "quit success\n", 13, 0);
break;
}
int send_len = send(client, "ok\n", 3, 0);
printf("recv [%s \n", buf);
memset(buf, 0, sizeof(buf));
}
closesocket(client); //linux系统下 宏定义为close()函数
delete this; //自己清理自己
简单多线程服务器,支持windows和linux
最新推荐文章于 2023-09-25 22:51:53 发布
本文介绍了一个简单的多线程服务器实现,该服务器能够同时运行在Windows和Linux操作系统上,通过多线程技术提高了服务处理能力,适用于基础的并发服务需求。
摘要由CSDN通过智能技术生成