SocketHandle.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /************************************************************************/
  2. /* Copyright (C), 2016-2020, [IT], 保留所有权利;
  3. /* 模 块 名:SocketHandle;
  4. /* 描 述:Socket Communication Classes;
  5. /*
  6. /* 版 本:[V 1.4] IPv6 support;
  7. /* 版 本:[V 1.3] Update for Asynchronous mode / Linux port;
  8. /* 版 本:[V 1.2] Update interface for TCP remote connection;
  9. /* 版 本:[V 1.1] Added multicast support;
  10. /* 作 者:[IT];
  11. /* 日 期:[2/23/2016];
  12. /*
  13. /*
  14. /* 注 意:;
  15. /*
  16. /* 修改记录:[IT];
  17. /* 修改日期:;
  18. /* 修改版本:;
  19. /* 修改内容:;
  20. /************************************************************************/
  21. #ifndef __SOCKETHANDLE_20160228__
  22. #define __SOCKETHANDLE_20160228__
  23. #pragma once
  24. #ifdef WIN32
  25. #include <winsock2.h>
  26. #include <ws2tcpip.h>
  27. #elif BSD_SOCKET
  28. #include "platform.h"
  29. #endif
  30. #include "SockAddrIn.h"
  31. #ifdef WIN32
  32. typedef LPWSAOVERLAPPED_COMPLETION_ROUTINE LPWSACOMPLETIONROUTINE;
  33. #endif
  34. // Jeff.add.2014.08.07--------------------------------------------------------
  35. #define SOCKET_SERVER_PORT 6432
  36. // Jeff.注意, 这里单个包的大小不能超过该设置数值,一般设置为10*1024即10KB; 不过实际中可根据实际情况处理,如使用TCP来传输数据的话;
  37. #define SOCKET_BUFFSIZE 1024*64
  38. // Jeff.add.2014.08.07--------------------------------------------------------
  39. /************************************************************************/
  40. /* Copyright (C), 2016-2020, [IT], 保留所有权利;
  41. /* 模 块 名:CSocketHandle类;
  42. /* 描 述:套接字通信类;
  43. /*
  44. /* 版 本:[V];
  45. /* 作 者:[IT];
  46. /* 日 期:[2/29/2016];
  47. /*
  48. /*
  49. /* 注 意:;
  50. /*
  51. /* 修改记录:[IT];
  52. /* 修改日期:;
  53. /* 修改版本:;
  54. /* 修改内容:;
  55. /************************************************************************/
  56. class CSocketHandle
  57. {
  58. public:
  59. CSocketHandle();
  60. ~CSocketHandle();
  61. // 套接字是否打开,打开返回TRUE;
  62. bool IsOpen() const;
  63. // 返回套接字句柄(如果对象关闭,句柄值为INVALID_SOCKET);
  64. SOCKET GetSocket() const;
  65. // 获取套接字类型(返回-1表示不是一个有效的套接字)
  66. int GetSocketType() const;
  67. // 附加套接字;
  68. bool Attach(IN SOCKET sock);
  69. // 从类中分离套接字;
  70. SOCKET Detach();
  71. // 获取套接字名(本地地址);
  72. bool GetSockName(OUT SockAddrIn& saddr_in) const;
  73. // 获取对方地址(如何是客户端调用,那获取的是服务端地址,反之亦然);
  74. bool GetPeerName(OUT SockAddrIn& saddr_in) const;
  75. // 关闭套接字;
  76. void Close();
  77. // 记录多播地址;
  78. bool AddMembership(IN LPCTSTR pszIPAddr, IN LPCTSTR pszNIC);
  79. // 移除多播地址;
  80. bool DropMembership(IN LPCTSTR pszIPAddr, IN LPCTSTR pszNIC);
  81. // 创建服务端套接字;
  82. bool CreateSocket(IN LPCTSTR pszHostName, IN LPCTSTR pszServiceName, IN int nFamily, IN int nType, IN UINT uOptions = 0);
  83. // 客户端连接服务端;
  84. bool ConnectTo(IN LPCTSTR pszHostName, IN LPCTSTR pszRemote, IN LPCTSTR pszServiceName, IN int nFamily, IN int nType);
  85. // 从套接字读取内容;
  86. DWORD Read(OUT LPBYTE lpBuffer, IN DWORD dwSize, IN LPSOCKADDR lpAddrIn = NULL, IN DWORD dwTimeout = INFINITE);
  87. #ifdef WIN32
  88. // 异步从套接字读取内容;
  89. DWORD ReadEx(OUT LPBYTE lpBuffer, IN DWORD dwSize, IN LPSOCKADDR lpAddrIn, IN LPWSAOVERLAPPED lpOverlapped, IN LPWSACOMPLETIONROUTINE lpCompletionRoutine);
  90. #endif
  91. // 向目标套接字发送内容;
  92. DWORD Write(IN const LPBYTE lpBuffer, IN DWORD dwCount, IN const LPSOCKADDR lpAddrIn = NULL, IN DWORD dwTimeout = INFINITE);
  93. #ifdef WIN32
  94. // 异步向目标套接字发送内容;
  95. DWORD WriteEx(IN const LPBYTE lpBuffer, IN DWORD dwCount, IN const LPSOCKADDR lpAddrIn, IN LPWSAOVERLAPPED lpOverlapped, IN LPWSACOMPLETIONROUTINE lpCompletionRoutine);
  96. #endif
  97. #ifdef WIN32
  98. // Control the mode of a socket (asynchronous mode);
  99. bool IOControl(
  100. IN DWORD dwIoCode,
  101. IN LPBYTE lpInBuffer,
  102. IN DWORD cbInBuffer,
  103. IN LPBYTE lpOutBuffer,
  104. IN DWORD cbOutBuffer,
  105. IN LPDWORD lpcbBytesReturned,
  106. IN LPWSAOVERLAPPED lpOverlapped,
  107. IN LPWSACOMPLETIONROUTINE lpCompletionRoutine
  108. );
  109. // Get Overlapped result (asynchronous mode);
  110. bool GetTransferOverlappedResult(IN LPWSAOVERLAPPED lpOverlapped, IN LPDWORD lpcbTransfer, IN bool bWait = true, IN LPDWORD lpdwFlags = 0);
  111. #endif
  112. // Initialize Winsock library. This function calls WSAStartup;
  113. static bool InitLibrary(IN WORD wVersion);
  114. // Release Winsock library;
  115. static bool ReleaseLibrary();
  116. // Wait for a new connection;
  117. static SOCKET WaitForConnection(IN SOCKET sock);
  118. // Shutdown a connection;
  119. static bool ShutdownConnection(IN SOCKET sock);
  120. // Check if IP address is unicast (network order);
  121. static bool IsUnicastIP( IN ULONG ulAddr );
  122. // Check if IP address is multicast (network order);
  123. static bool IsMulticastIP( IN ULONG ulAddr );
  124. // Format IP address to string;
  125. static bool FormatIP(IN LPTSTR pszIPAddr, IN UINT nSize, IN ULONG ulAddr, IN bool bFmtHost);
  126. // Format IP address to string;
  127. static bool FormatIP(IN LPTSTR pszIPAddr, IN UINT nSize, IN const SockAddrIn& addrIn);
  128. // Get service port number;
  129. static USHORT GetPortNumber( IN LPCTSTR pszServiceName );
  130. // Get IP address of a host;
  131. static ULONG GetIPAddress( IN LPCTSTR pszHostName );
  132. // Get current localname for this machine;
  133. static bool GetLocalName(IN LPTSTR pszName, IN UINT nSize);
  134. // Get current (default) IP address for this machine;
  135. static bool GetLocalAddress(IN LPTSTR pszAddress, IN UINT nSize, IN int nFamily = AF_INET);
  136. // Get IP address info of a host (Supports: IPv4 and IPv6);
  137. static bool GetAddressInfo( IN LPCTSTR pszHostName, IN LPCTSTR pszServiceName, IN int nFamily, IN SockAddrIn& sockAddr, bool bIsClient = true );
  138. // CSocketHandle - data
  139. protected:
  140. SOCKET m_hSocket; ///< socket handle
  141. // Jeff.add.2014.08.07-----------------------------------.
  142. public:
  143. unsigned char m_szpendingbuf[SOCKET_BUFFSIZE];
  144. unsigned int m_npendingSize;
  145. CRITICAL_SECTION m_hClient2SrvSection;
  146. // Jeff.add.2014.08.07-----------------------------------.
  147. };
  148. #endif // __SOCKETHANDLE_20160228__