streamsocket.h
上传用户:garry_shen
上传日期:2015-04-15
资源大小:45647k
文件大小:2k
源码类别:

游戏引擎

开发平台:

Visual C++

  1. /*---------------------------------------------------------
  2.    StreamSocket.h -- Handling stream socket's APIs
  3.                (c) 砛κ秤Cary Hsu, 2000.3.3
  4. ---------------------------------------------------------*/
  5. #ifndef __STREAMSOCKET
  6. #define __STREAMSOCKET
  7. #include <windows.h>
  8. #include "Wsa_xtra.h"
  9. #define CALLWGS "0.0.0.0"
  10. #define MTU_SIZE 1460
  11. #define INPUT_SIZE 8192
  12. #define BUF_SIZE 1024
  13. #define MAXHOSTNAME 128
  14. enum{
  15. WS_ERROR=0,
  16. WS_OK =1,
  17. WS_WAIT =2
  18. };
  19. enum
  20. {
  21. NO_USED=0,
  22. CONNECTED,
  23. LISTENING
  24. };
  25. class StreamSocket
  26. {
  27. private:
  28. HWND hwnd;
  29. SOCKET hSock;
  30. SOCKADDR_IN
  31. stName;
  32. char szRecvBuf[INPUT_SIZE];
  33. int nRecvBufSize;
  34. BOOL bNoDelay;
  35. static BOOL bInitStartup;
  36. static WSADATA stWSAData;
  37. static int nUseObjects;
  38. protected:
  39. DWORD state;
  40. private:
  41. public:
  42. StreamSocket():nRecvBufSize(INPUT_SIZE),state(NO_USED),hSock(INVALID_SOCKET),bNoDelay(TRUE){InitialSocket();}
  43. StreamSocket(BOOL NoDelay):nRecvBufSize(INPUT_SIZE),state(NO_USED),hSock(INVALID_SOCKET){
  44. bNoDelay=NoDelay;
  45. InitialSocket();}
  46. ~StreamSocket(){ReleaseSocket();}
  47. void InitialSocket();
  48. void ReleaseSocket();
  49. DWORD GetHostIP(LPSTR szHost);
  50. DWORD GetLocalIP();
  51. WORD GetPort(LPSTR szService);
  52. int SetLargestBuf(int nBigBufSize,int nOptval);
  53. DWORD listening(WORD wPort,HWND hWnd,DWORD dwAsyncMsg);
  54. DWORD accepting(StreamSocket &stmSock,HWND hWnd,DWORD dwAsyncMsg);
  55. DWORD accepted(SOCKET sock,HWND hWnd,DWORD dwAsyncMsg);
  56. DWORD connecting(char *lpstrHostName,WORD wPort,HWND hWnd,DWORD dwAsyncMsg);
  57. void connected();
  58. void closeing();
  59. void closed(){
  60. closesocket(hSock);
  61. hSock=INVALID_SOCKET;
  62. state=NO_USED;}
  63. LPSOCKADDR
  64. GetSOCKADDR(){return (LPSOCKADDR)&stName;}
  65. BOOL equal(SOCKET sock){
  66. return hSock==sock;}
  67. BOOL IsNotUsed(){return (NO_USED==state && INVALID_SOCKET==hSock);}
  68. };
  69. #endif