123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 |
- /************************************************************************/
- /* Copyright (C), 2016-2020, [IT], 保留所有权利;
- /* 模 块 名:SocketHandle;
- /* 描 述:Socket Communication Classes;
- /*
- /* 版 本:[V 1.4] IPv6 support;
- /* 版 本:[V 1.3] Update for Asynchronous mode / Linux port;
- /* 版 本:[V 1.2] Update interface for TCP remote connection;
- /* 版 本:[V 1.1] Added multicast support;
- /* 作 者:[IT];
- /* 日 期:[2/23/2016];
- /*
- /*
- /* 注 意:;
- /*
- /* 修改记录:[IT];
- /* 修改日期:;
- /* 修改版本:;
- /* 修改内容:;
- /************************************************************************/
- #ifndef __SOCKETHANDLE_20160228__
- #define __SOCKETHANDLE_20160228__
- #pragma once
- #ifdef WIN32
- #include <winsock2.h>
- #include <ws2tcpip.h>
- #elif BSD_SOCKET
- #include "platform.h"
- #endif
- #include "SockAddrIn.h"
- #ifdef WIN32
- typedef LPWSAOVERLAPPED_COMPLETION_ROUTINE LPWSACOMPLETIONROUTINE;
- #endif
- // Jeff.add.2014.08.07--------------------------------------------------------
- #define SOCKET_SERVER_PORT 6432
- // Jeff.注意, 这里单个包的大小不能超过该设置数值,一般设置为10*1024即10KB; 不过实际中可根据实际情况处理,如使用TCP来传输数据的话;
- #define SOCKET_BUFFSIZE 1024*64
- // Jeff.add.2014.08.07--------------------------------------------------------
- /************************************************************************/
- /* Copyright (C), 2016-2020, [IT], 保留所有权利;
- /* 模 块 名:CSocketHandle类;
- /* 描 述:套接字通信类;
- /*
- /* 版 本:[V];
- /* 作 者:[IT];
- /* 日 期:[2/29/2016];
- /*
- /*
- /* 注 意:;
- /*
- /* 修改记录:[IT];
- /* 修改日期:;
- /* 修改版本:;
- /* 修改内容:;
- /************************************************************************/
- class CSocketHandle
- {
- public:
- CSocketHandle();
- ~CSocketHandle();
- // 套接字是否打开,打开返回TRUE;
- bool IsOpen() const;
- // 返回套接字句柄(如果对象关闭,句柄值为INVALID_SOCKET);
- SOCKET GetSocket() const;
- // 获取套接字类型(返回-1表示不是一个有效的套接字)
- int GetSocketType() const;
- // 附加套接字;
- bool Attach(IN SOCKET sock);
- // 从类中分离套接字;
- SOCKET Detach();
- // 获取套接字名(本地地址);
- bool GetSockName(OUT SockAddrIn& saddr_in) const;
- // 获取对方地址(如何是客户端调用,那获取的是服务端地址,反之亦然);
- bool GetPeerName(OUT SockAddrIn& saddr_in) const;
- // 关闭套接字;
- void Close();
- // 记录多播地址;
- bool AddMembership(IN LPCTSTR pszIPAddr, IN LPCTSTR pszNIC);
- // 移除多播地址;
- bool DropMembership(IN LPCTSTR pszIPAddr, IN LPCTSTR pszNIC);
- // 创建服务端套接字;
- bool CreateSocket(IN LPCTSTR pszHostName, IN LPCTSTR pszServiceName, IN int nFamily, IN int nType, IN UINT uOptions = 0);
- // 客户端连接服务端;
- bool ConnectTo(IN LPCTSTR pszHostName, IN LPCTSTR pszRemote, IN LPCTSTR pszServiceName, IN int nFamily, IN int nType);
-
- // 从套接字读取内容;
- DWORD Read(OUT LPBYTE lpBuffer, IN DWORD dwSize, IN LPSOCKADDR lpAddrIn = NULL, IN DWORD dwTimeout = INFINITE);
- #ifdef WIN32
- // 异步从套接字读取内容;
- DWORD ReadEx(OUT LPBYTE lpBuffer, IN DWORD dwSize, IN LPSOCKADDR lpAddrIn, IN LPWSAOVERLAPPED lpOverlapped, IN LPWSACOMPLETIONROUTINE lpCompletionRoutine);
- #endif
- // 向目标套接字发送内容;
- DWORD Write(IN const LPBYTE lpBuffer, IN DWORD dwCount, IN const LPSOCKADDR lpAddrIn = NULL, IN DWORD dwTimeout = INFINITE);
- #ifdef WIN32
- // 异步向目标套接字发送内容;
- DWORD WriteEx(IN const LPBYTE lpBuffer, IN DWORD dwCount, IN const LPSOCKADDR lpAddrIn, IN LPWSAOVERLAPPED lpOverlapped, IN LPWSACOMPLETIONROUTINE lpCompletionRoutine);
- #endif
- #ifdef WIN32
- // Control the mode of a socket (asynchronous mode);
- bool IOControl(
- IN DWORD dwIoCode,
- IN LPBYTE lpInBuffer,
- IN DWORD cbInBuffer,
- IN LPBYTE lpOutBuffer,
- IN DWORD cbOutBuffer,
- IN LPDWORD lpcbBytesReturned,
- IN LPWSAOVERLAPPED lpOverlapped,
- IN LPWSACOMPLETIONROUTINE lpCompletionRoutine
- );
- // Get Overlapped result (asynchronous mode);
- bool GetTransferOverlappedResult(IN LPWSAOVERLAPPED lpOverlapped, IN LPDWORD lpcbTransfer, IN bool bWait = true, IN LPDWORD lpdwFlags = 0);
- #endif
- // Initialize Winsock library. This function calls WSAStartup;
- static bool InitLibrary(IN WORD wVersion);
- // Release Winsock library;
- static bool ReleaseLibrary();
- // Wait for a new connection;
- static SOCKET WaitForConnection(IN SOCKET sock);
- // Shutdown a connection;
- static bool ShutdownConnection(IN SOCKET sock);
- // Check if IP address is unicast (network order);
- static bool IsUnicastIP( IN ULONG ulAddr );
- // Check if IP address is multicast (network order);
- static bool IsMulticastIP( IN ULONG ulAddr );
- // Format IP address to string;
- static bool FormatIP(IN LPTSTR pszIPAddr, IN UINT nSize, IN ULONG ulAddr, IN bool bFmtHost);
- // Format IP address to string;
- static bool FormatIP(IN LPTSTR pszIPAddr, IN UINT nSize, IN const SockAddrIn& addrIn);
- // Get service port number;
- static USHORT GetPortNumber( IN LPCTSTR pszServiceName );
- // Get IP address of a host;
- static ULONG GetIPAddress( IN LPCTSTR pszHostName );
- // Get current localname for this machine;
- static bool GetLocalName(IN LPTSTR pszName, IN UINT nSize);
- // Get current (default) IP address for this machine;
- static bool GetLocalAddress(IN LPTSTR pszAddress, IN UINT nSize, IN int nFamily = AF_INET);
- // Get IP address info of a host (Supports: IPv4 and IPv6);
- static bool GetAddressInfo( IN LPCTSTR pszHostName, IN LPCTSTR pszServiceName, IN int nFamily, IN SockAddrIn& sockAddr, bool bIsClient = true );
- // CSocketHandle - data
- protected:
- SOCKET m_hSocket; ///< socket handle
- // Jeff.add.2014.08.07-----------------------------------.
- public:
- unsigned char m_szpendingbuf[SOCKET_BUFFSIZE];
- unsigned int m_npendingSize;
- CRITICAL_SECTION m_hClient2SrvSection;
- // Jeff.add.2014.08.07-----------------------------------.
- };
- #endif // __SOCKETHANDLE_20160228__
|