//为了支持移植 沿用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
select + 线程池 回应服务器(windows)
最新推荐文章于 2022-08-17 15:33:56 发布
该程序示例展示了如何在Windows环境下利用select系统调用和线程池来处理TCP服务器的多个并发连接。通过创建线程池,每个新连接被分配到一个工作线程进行读写操作,确保了服务器的高并发性能。
摘要由CSDN通过智能技术生成