简单多线程服务器,支持windows和linux

本文介绍了一个简单的多线程服务器实现,该服务器能够同时运行在Windows和Linux操作系统上,通过多线程技术提高了服务处理能力,适用于基础的并发服务需求。
摘要由CSDN通过智能技术生成
#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; //自己清理自己
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值