select + 线程池 回应服务器(windows)

该程序示例展示了如何在Windows环境下利用select系统调用和线程池来处理TCP服务器的多个并发连接。通过创建线程池,每个新连接被分配到一个工作线程进行读写操作,确保了服务器的高并发性能。
摘要由CSDN通过智能技术生成
//为了支持移植 沿用Ptypes的头文件
#define WIN32 1
#include "ptime.h"
#include "pinet.h"
#include "ptypes.h"
#include "pasync.h"

#ifdef WIN32
#pragma comment(lib, "ptypes.lib")
#pragma comment(lib, "ws2_32.lib")
#endif

#ifdef WIN32
#  include <winsock2.h>
#else
#  include <sys/time.h>
#  include <sys/types.h>
#  include <sys/socket.h>
#  include <netinet/in.h>
#  include <arpa/inet.h>
#  include <netdb.h>
#  include <unistd.h>
#  include <time.h>
#endif

USING_PTYPES

const int testport = 8101;
const int maxtoken = 4096;
const int timeout = -1;
const int maxsetsize = 1024*10;
const int maxthreads = 30;

const int MSG_MYJOB = MSG_USER + 1;

fd_set g_allset;  //保存所有要检测的socket;
int g_arrClientSocket[maxsetsize];
struct SocketInfo
{
	int nSocket;
	int nIndex;
};

class myjobthread: public thread
{
protected:
	int id;
	jobqueue* jq;
	virtual void execute();
public:
	myjobthread(int iid, jobqueue* ijq)
		: thread(false), id(iid), jq(ijq)  {}
	~myjobthread()  { waitfor(); }
};

class myjob: public message
{
public:
	SocketInfo* m_pSocketInfo;
	myjob(SocketInfo* pSocketInfo)
		: message(MSG_MYJOB), m_pSocketInfo(pSocketInfo)  {}
	~myjob()  {delete m_pSocketInfo; }
};


void myjobthread::execute()
{
	bool quit = false;
	while (!quit)
	{
		// get the next message from the queue
		message* msg = jq->getmessage();

		try
		{
			switch (msg->id)
			{
			case MSG_MYJOB:
				{
					pout.putf("%t,线程:%d\n", now(), i
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值