ServerSocket.h
上传用户:swkcbjrc
上传日期:2016-04-02
资源大小:45277k
文件大小:1k
源码类别:

游戏

开发平台:

Visual C++

  1. #pragma once
  2. #include <WinSock2.h>
  3. #pragma comment (lib,"ws2_32.lib")
  4. typedef  unsigned (WINAPI *PBEGINTHREADEX_THREADFUNC)( LPVOID lpThreadParameter);
  5. typedef  unsigned *PBEGINTHREADEX_THREADID;
  6. #define LOGMESSAGE(x) printf(x)
  7. class CServerSocket
  8. {
  9. friend UINT ServerListenThread(LPVOID lParam);
  10. friend UINT OverlappedThread(LPVOID lParam);
  11. public:
  12. CServerSocket(void);
  13. ~CServerSocket(void);
  14. bool StartupAcceptThread();
  15. int  StartListening(const UINT&);
  16. bool StartupWorkThread();
  17. int StopListening();
  18. private:
  19. UINT m_nPort;
  20. int InitWSASocket();
  21. int GetEmptySocket();
  22. HANDLE m_hAcceptThread;
  23. HANDLE m_hWorkThread;
  24. public:
  25. SOCKET sockListen;
  26. SOCKET sockAcceptArray[WSA_MAXIMUM_WAIT_EVENTS]; // 与客户端通信的SOCKET
  27. WSAEVENT EventArray[WSA_MAXIMUM_WAIT_EVENTS];    // 与重叠结构配套的事件组
  28. WSAOVERLAPPED AcceptOverlapped[WSA_MAXIMUM_WAIT_EVENTS]; // 重叠结构,每个SOCKET操作对应一个
  29. WSABUF DataBuf[WSA_MAXIMUM_WAIT_EVENTS];  // 接收缓冲区,WSARecv的参数,每个SCOKET对应一个
  30. DWORD dwEventTotal,dwRecvBytes,Flags;
  31. int  nSockIndex;            // socket数组的编号
  32. BOOL bOverlapped;       // 是否处理重叠请求
  33. };