12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589 |
- #include "stdafx.h"
- #ifdef WIN32
- #include <stdlib.h>
- #ifndef UNDER_CE
- #include <crtdbg.h>
- #endif
- #include <strsafe.h>
- #endif
- #include "SocketHandle.h"
- // Jeff.add.
- //#include "Wspiapi.h"
- //#include "Ws2tcpip.h"
- #include <MSTcpIP.h>
- #ifndef BUFFER_SIZE
- #define BUFFER_SIZE 64*1024
- #endif
- #ifndef SOCKHANDLE_TTL
- #define SOCKHANDLE_TTL 5
- #endif
- #ifndef SOCKHANDLE_HOPS
- #define SOCKHANDLE_HOPS 10
- #endif
- #define HOSTNAME_SIZE MAX_PATH
- #define STRING_LENGTH 40
- #if !defined(PLATFORM_HAS_INETFUNC)
- const char *inet_ntop(int af, const void *src, char *dst, socklen_t cnt);
- int inet_pton(int af, const char *src, void *dst);
- #endif
- #ifdef WIN32
- #ifndef UNDER_CE
- #pragma comment(lib, "ws2_32.lib")
- #else
- #pragma comment(lib, "Ws2.lib")
- #endif
- #endif
- #if 1
- typedef struct STcpKeepAlive2
- {
- DWORD onoff;
- DWORD keepalivetime;
- DWORD keepaliveinterval;
- }TCP_KEEP_ALIVE2;
- /************************************************************************/
- /* 函数:SetKeepLive[2/29/2016 IT];
- /* 描述:设置TCP连接鲜活时间;
- /* 参数:;
- /* [IN] Socket:要设置的客户端SOCKET;
- /* 返回:设置成功返回TRUE;
- /* 注意:;
- /* 示例:;
- /*
- /* 修改:;
- /* 日期:;
- /* 内容:;
- /************************************************************************/
- BOOL SetKeepLive(IN SOCKET &Socket)
- {
- const BYTE chOpt = 1; // True;
- // Set KeepAlive 开启保活机制, 防止服务端产生死连接;
- if (setsockopt(Socket, SOL_SOCKET, SO_KEEPALIVE, (char *)&chOpt, sizeof(chOpt)) == SOCKET_ERROR)
- {
- printf("设置客户端鲜活机制失败1\n\n");
- return FALSE;
- }
- // 设置超时详细信息;
- BYTE byRet[5] = {0};
- TCP_KEEP_ALIVE2 klive;
- klive.onoff = 1; // 启用保活;
- klive.keepalivetime = 1000 * 15; // 3分钟超时 Keep Alive;
- klive.keepaliveinterval = 1000 * 5; // 重试间隔为5秒 Resend if No-Reply;
- INT nResult =
- WSAIoctl
- (
- Socket,
- SIO_KEEPALIVE_VALS,
- &klive,
- sizeof(TCP_KEEP_ALIVE2),
- NULL,
- 0,
- (unsigned long *)&byRet,
- 0,
- NULL
- );
- if ( nResult == SOCKET_ERROR )
- {
- printf("设置客户端鲜活机制失败2\n\n");
- return FALSE;
- }
- return TRUE;
- }
- #endif
- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- // SockAddrIn 类实现;
- SockAddrIn SockAddrIn::NULLAddr;
- SockAddrIn::SockAddrIn()
- {
- Clear();
- }
- SockAddrIn::SockAddrIn(const SockAddrIn& sin)
- {
- Copy( sin );
- }
- SockAddrIn::~SockAddrIn(){}
- // 清空结构体;
- void SockAddrIn::Clear()
- {
- memset(this, 0, sizeof(sockaddr_storage));
- }
- /************************************************************************/
- /* 函数:Copy[2/28/2016 IT];
- /* 描述:复制结构体;
- /* 参数:;
- /* [IN] sin:SockAddrIn对象引用;;
- /* 返回:返回this指针;
- /* 注意:;
- /* 示例:;
- /*
- /* 修改:;
- /* 日期:;
- /* 内容:;
- /************************************************************************/
- SockAddrIn& SockAddrIn::Copy(const SockAddrIn& sin)
- {
- ss_family = sin.ss_family;
- memcpy(this, &sin, Size());
- return *this;
- }
- /************************************************************************/
- /* 函数:IsEqual[2/28/2016 IT];
- /* 描述:对比网络地址对象是否相同;
- /* 参数:;
- /* [IN] :;
- /* 返回:相同返回TRUE,否则返回FALSE;
- /* 注意:;
- /* 示例:;
- /*
- /* 修改:;
- /* 日期:;
- /* 内容:;
- /************************************************************************/
- bool SockAddrIn::IsEqual(const SockAddrIn& sin) const
- {
- if ( ss_family == AF_INET )
- {// AF_INET是IPV4协议地址簇专有,则需要忽略sin_zero[8]数组;
- return (memcmp(this, &sin, Size() - 8) == 0);
- }
- // 如果是IPV6地址结构(AF_INET6);
- return (memcmp(this, &sin, Size()) == 0);
- }
- /************************************************************************/
- /* 函数:CreateFrom[2/29/2016 IT];
- /* 描述:创建服务端套接字地址,主要参数以字符串形式传入;
- /* 参数:;
- /* [IN] pszAddr:服务端地址或服务端主机名;
- /* [IN] pszService:字符串形式的IP端口或服务名;
- /* [IN] nFamily:网络地址族类型(IPV4或IPV6);
- /* [OUT] :;
- /* [IN/OUT] :;
- /* 返回:成功创建返回TRUE;
- /* 注意:;
- /* 示例:;
- /*
- /* 修改:;
- /* 日期:;
- /* 内容:;
- /************************************************************************/
- bool SockAddrIn::CreateFrom(IN LPCTSTR pszAddr, IN LPCTSTR pszService, IN int nFamily /*=AF_INET*/)
- {
- Clear();
- CSocketHandle::GetAddressInfo(pszAddr, pszService, nFamily, *this);
- return !IsNull();
- }
- /************************************************************************/
- /* 函数:CreateFrom[2/29/2016 IT];
- /* 描述:创建服务端套接字地址,主要参数以数值形式传入(只支持IPV4);
- /* 参数:;
- /* [IN] lIPAddr:数值形式的IP地址;
- /* [IN] nPort:数值形式的服务端端口号;
- /* [IN] nFamily:网络地址簇类型(默认AF_INET);
- /* [IN] bFmtHost:
- /* 标志IP地址(参数1)和端口(参数2)是主机字节序HBO(Host By Order),即小端模式(little-endian)
- /* 或网络字节序NBO(NetWork By Order),即大端模式(big-endian);
- /* 返回:成功创建返回TRUE;
- /* 注意:该函数只支持IPV4网络地址;
- /* 示例:;
- /*
- /* 修改:;
- /* 日期:;
- /* 内容:;
- /************************************************************************/
- bool SockAddrIn::CreateFrom(IN ULONG lIPAddr, IN USHORT nPort, IN int nFamily /*= AF_INET*/, IN bool bFmtHost /*= true*/)
- {
- Clear();
- _ASSERTE( nFamily == AF_INET ); // supports IPv4 only
- SOCKADDR_IN* psin = reinterpret_cast<SOCKADDR_IN*>(this);
- psin->sin_family = static_cast<short>(nFamily);
- if ( bFmtHost )
- {
- psin->sin_addr.s_addr = htonl( lIPAddr );
- psin->sin_port = htons( nPort );
- }
- else
- {
- psin->sin_addr.s_addr = lIPAddr;
- psin->sin_port = nPort;
- }
- return !IsNull();
- }
- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- // CSocketHandle类的实现;
- CSocketHandle::CSocketHandle(): m_hSocket(INVALID_SOCKET)
- {
- m_npendingSize = 0;
- memset(m_szpendingbuf,0,SOCKET_BUFFSIZE);
- }
- CSocketHandle::~CSocketHandle()
- {
- Close();
- }
- bool CSocketHandle::IsOpen() const
- {
- return ( INVALID_SOCKET != m_hSocket );
- }
- SOCKET CSocketHandle::GetSocket() const
- {
- return m_hSocket;
- }
- /************************************************************************/
- /* 函数:GetSocketType[2/29/2016 IT];
- /* 描述:获取套接字类型;
- /* 参数:;
- /* 返回:返回-1表示不是一个有效的套接字;
- /* 注意:;
- /* 示例:;
- /*
- /* 修改:;
- /* 日期:;
- /* 内容:;
- /************************************************************************/
- int CSocketHandle::GetSocketType() const
- {
- int type = -1;
- if ( INVALID_SOCKET != m_hSocket ) {
- socklen_t optlen = sizeof(int);
- if ( getsockopt(GetSocket(), SOL_SOCKET, SO_TYPE, reinterpret_cast<char*>(&type),&optlen) == SOCKET_ERROR)
- {
- SetLastError( WSAGetLastError() );
- }
- }
- return type;
- }
- /************************************************************************/
- /* 函数:Attach[2/29/2016 IT];
- /* 描述:附加一个SOCKET句柄,使用Detach函数或Close函数来释放句柄;
- /* 参数:;
- /* [IN] sock:要附加到类中的套接字句柄;
- /* 返回:成功返回TRUE;
- /* 注意:函数有可能失败,当要附加的套接字是一个无效值或该值已在使用时;
- /* 示例:;
- /*
- /* 修改:;
- /* 日期:;
- /* 内容:;
- /************************************************************************/
- bool CSocketHandle::Attach(IN SOCKET sock)
- {
- if ( INVALID_SOCKET == m_hSocket )
- {
- m_hSocket = sock;
- return true;
- }
- return false;
- }
- /************************************************************************/
- /* 函数:Detach[2/29/2016 IT];
- /* 描述:把套接字从类对象中分离出来;
- /* 参数:无;
- /* 返回:返回前面附加的套接字句柄或INVALID_SOCKET;
- /* 注意:;
- /* 示例:;
- /*
- /* 修改:;
- /* 日期:;
- /* 内容:;
- /************************************************************************/
- SOCKET CSocketHandle::Detach()
- {
- SOCKET sock = m_hSocket;
- ::InterlockedExchange(reinterpret_cast<long*>(&m_hSocket), INVALID_SOCKET);
- return sock;
- }
- /************************************************************************/
- /* 函数:GetSockName[2/29/2016 IT];
- /* 描述:获取套接字名(本地地址);
- /* 参数:;
- /* [OUT] saddr_in:连接成功时返回的当前本地地址和端口号;
- /* 返回:成功返回TRUE;
- /* 注意:使用前必须调用InitLibrary函数;
- /* 示例:;
- /*
- /* 修改:;
- /* 日期:;
- /* 内容:;
- /************************************************************************/
- bool CSocketHandle::GetSockName(OUT SockAddrIn& saddr_in) const
- {
- _ASSERTE( IsOpen() );
- if (IsOpen()) {
- socklen_t namelen = (socklen_t)saddr_in.Size();
- if (SOCKET_ERROR != getsockname(GetSocket(), saddr_in, &namelen))
- {
- return true;
- }
- SetLastError( WSAGetLastError() );
- }
- return false;
- }
- /************************************************************************/
- /* 函数:GetPeerName[2/29/2016 IT];
- /* 描述:获取点对点的套接接名(获取Peer地址);;
- /* 参数:;
- /* [OUT] saddr_in: peer address and port (use only with TCP or client mode UDP);
- /* 返回:成功返回TRUE;
- /* 注意:使用前必须调用InitLibrary函数;
- /* 示例:;
- /*
- /* 修改:;
- /* 日期:;
- /* 内容:;
- /************************************************************************/
- bool CSocketHandle::GetPeerName(OUT SockAddrIn& saddr_in) const
- {
- _ASSERTE( IsOpen() );
- if (IsOpen()) {
- socklen_t namelen = (socklen_t)saddr_in.Size();
- if (SOCKET_ERROR != getpeername(GetSocket(), saddr_in, &namelen))
- {
- return true;
- }
- SetLastError( WSAGetLastError() );
- }
- return false;
- }
- /************************************************************************/
- /* 函数:Close[2/29/2016 IT];
- /* 描述:关闭套接字;
- /* 参数:;
- /* 返回:void;
- /* 注意:关联函数InitLibrary, CreateSocket, ConnectTo, IsOpen;
- /* 示例:;
- /*
- /* 修改:;
- /* 日期:;
- /* 内容:;
- /************************************************************************/
- void CSocketHandle::Close()
- {
- if ( IsOpen() )
- {
- ShutdownConnection(static_cast<SOCKET>(::InterlockedExchange((LONG*)&m_hSocket, INVALID_SOCKET)));
- }
- }
- /************************************************************************/
- /* 函数:AddMembership[2/29/2016 IT];
- /* 描述:添加多播地址;
- /* 参数:;
- /* [IN] pszIPAddr:多播IP组;
- /* [IN] pszNIC:IP地址接口(多张网卡时,某一网卡序列号);
- /* 返回:成功返回TRUE;
- /* 注意:关联InitLibrary函数;
- /* 示例:;
- /*
- /* 修改:;
- /* 日期:;
- /* 内容:;
- /************************************************************************/
- bool CSocketHandle::AddMembership(IN LPCTSTR pszIPAddr, IN LPCTSTR pszNIC)
- {
- _ASSERTE( IsOpen() );
- if ( IsOpen() )
- {
- int nType = 0;
- socklen_t nOptLen = sizeof(int);
- if ( SOCKET_ERROR != getsockopt(m_hSocket, SOL_SOCKET, SO_TYPE, (char*)&nType, &nOptLen))
- {
- if ( nType == SOCK_DGRAM )
- {
- // Setup interface for multicast traffic
- SockAddrIn mcastAddr;
- if (GetAddressInfo(pszIPAddr, NULL, AF_UNSPEC, mcastAddr))
- {
- SockAddrIn interfAddr;
- GetAddressInfo(pszNIC, NULL, mcastAddr.ss_family, interfAddr);
- if ( mcastAddr.ss_family == AF_INET )
- {
- int nTTL = SOCKHANDLE_TTL;
- if ( SOCKET_ERROR != setsockopt(m_hSocket, IPPROTO_IP, IP_MULTICAST_TTL, (const char*)&nTTL, sizeof(nTTL)))
- {
- ULONG ulNIC = interfAddr.GetIPAddr();
- if ( SOCKET_ERROR != setsockopt(m_hSocket, IPPROTO_IP, IP_MULTICAST_IF,(char *) &ulNIC, sizeof(ulNIC)))
- {
- ip_mreq mreq = { 0 };
- mreq.imr_multiaddr.s_addr = mcastAddr.GetIPAddr();
- mreq.imr_interface.s_addr = ulNIC;
- if ( SOCKET_ERROR != setsockopt(m_hSocket, IPPROTO_IP, IP_ADD_MEMBERSHIP, (const char*)&mreq, sizeof(mreq)))
- {
- return true;
- }
- }
- }
- }
- else if ( mcastAddr.ss_family == AF_INET6 )
- {
- int nTTL = SOCKHANDLE_HOPS;
- if ( SOCKET_ERROR != setsockopt(m_hSocket, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, (const char*)&nTTL, sizeof(nTTL)))
- {
- ipv6_mreq mreq6 = { 0 };
- IN6_ADDR mcin6 = ((sockaddr_in6*)&mcastAddr)->sin6_addr;
- memcpy(&(mreq6.ipv6mr_multiaddr), &mcin6, sizeof(IN6_ADDR));
- if ( SOCKET_ERROR != setsockopt(m_hSocket, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, (const char*)&mreq6, sizeof(mreq6)))
- {
- return true;
- }
- }
- }
- else
- {
- // invalid socket option
- WSASetLastError(WSAENOPROTOOPT);
- }
- }
- }
- else
- {
- // invalid socket option
- WSASetLastError(WSAENOPROTOOPT);
- }
- }
- SetLastError( WSAGetLastError() );
- }
- return false;
- }
- /************************************************************************/
- /* 函数:DropMembership[2/29/2016 IT];
- /* 描述:移除多播地址;
- /* 参数:;
- /* [IN] pszIPAddr:多播IP组;
- /* [IN] pszNIC:IP地址接口(多张网卡时,某一网卡序列号);
- /* 返回:成功返回TRUE;
- /* 注意:关联InitLibrary函数;
- /* 示例:;
- /*
- /* 修改:;
- /* 日期:;
- /* 内容:;
- /************************************************************************/
- bool CSocketHandle::DropMembership(LPCTSTR pszIPAddr, LPCTSTR pszNIC)
- {
- _ASSERTE( IsOpen() );
- if ( IsOpen() )
- {
- int nType = 0;
- socklen_t nOptLen = sizeof(int);
- if ( SOCKET_ERROR != getsockopt(m_hSocket, SOL_SOCKET, SO_TYPE, (char*)&nType, &nOptLen))
- {
- if ( nType == SOCK_DGRAM )
- {
- SockAddrIn mcastAddr;
- if (GetAddressInfo(pszIPAddr, NULL, AF_UNSPEC, mcastAddr))
- {
- SockAddrIn interfAddr;
- GetAddressInfo(pszNIC, NULL, mcastAddr.ss_family, interfAddr);
- if ( mcastAddr.ss_family == AF_INET )
- {
- ip_mreq mreq;
- mreq.imr_multiaddr.s_addr = mcastAddr.GetIPAddr();
- mreq.imr_interface.s_addr = interfAddr.GetIPAddr();;
- if ( SOCKET_ERROR != setsockopt(m_hSocket, IPPROTO_IP, IP_DROP_MEMBERSHIP, (const char*)&mreq, sizeof(mreq)))
- {
- return true;
- }
- }
- else if ( mcastAddr.ss_family == AF_INET6 )
- {
- ipv6_mreq mreq6 = { 0 };
- IN6_ADDR mcin6 = ((sockaddr_in6*)&mcastAddr)->sin6_addr;
- memcpy(&(mreq6.ipv6mr_multiaddr), &mcin6, sizeof(IN6_ADDR));
- if ( SOCKET_ERROR != setsockopt(m_hSocket, IPPROTO_IPV6, IPV6_DROP_MEMBERSHIP, (const char*)&mreq6, sizeof(mreq6)))
- {
- return true;
- }
- }
- else
- {
- // invalid socket option
- WSASetLastError(WSAENOPROTOOPT);
- }
- }
- }
- else
- {
- // invalid socket option
- WSASetLastError(WSAENOPROTOOPT);
- }
- }
- SetLastError( WSAGetLastError() );
- }
- return false;
- }
- /************************************************************************/
- /* 函数:CreateSocket[2/29/2016 IT];
- /* 描述:创建服务端套接字;
- /* 参数:;
- /* [IN] pszHostName:主机名或网卡适配器地址;
- /* [IN] pszServiceName:网络服务名或者IP端口号;
- /* [IN] nFamily:要用的地址协议簇类型(IPV4或IPV6);
- /* [IN] nType:要创建的套接字类型(SOCK_STREAM 或 SOCK_DGRAM);
- /* [IN] uOptions:附加选项(SO_BROADCAST,SO_REUSEADDR) ;
- /* 返回:成功返回TRUE;
- /* 注意:使用WSAGetLastError()函数获取最后的错误,关联函数InitLibrary, ConnectTo, IsOpen;
- /* 示例:;
- /*
- /* 修改:;
- /* 日期:;
- /* 内容:;
- /************************************************************************/
- bool CSocketHandle::CreateSocket(IN LPCTSTR pszHostName, IN LPCTSTR pszServiceName, IN int nFamily, IN int nType, IN UINT uOptions /* = 0 */)
- {
- // Socket is already opened
- if ( IsOpen() ) {
- SetLastError(ERROR_ACCESS_DENIED);
- return false;
- }
- // Create a Socket that is bound to a specific service provider
- // nFamily: (AF_INET, AF_INET6)
- // nType: (SOCK_STREAM, SOCK_DGRAM)
- #ifdef SOCKHANDLE_USE_OVERLAPPED
- SOCKET sock = WSASocket(nFamily, nType, IPPROTO_IP, NULL, 0, WSA_FLAG_OVERLAPPED);
- #else
- SOCKET sock = socket(nFamily, nType, IPPROTO_IP);
- #endif
- if (INVALID_SOCKET != sock)
- {
- if (uOptions & SO_REUSEADDR)
- {
- // Inform Windows Sockets provider that a bind on a socket should not be disallowed
- // because the desired address is already in use by another socket
- BOOL optval = TRUE;
- if ( SOCKET_ERROR == setsockopt( sock, SOL_SOCKET, SO_REUSEADDR, (char *) &optval, sizeof( BOOL ) ) )
- {
- SetLastError( WSAGetLastError() );
- closesocket( sock );
- return false;
- }
- }
- if (nType == SOCK_DGRAM)
- {
- if ((uOptions & SO_BROADCAST) && (nFamily == AF_INET))
- {
- // Inform Windows Sockets provider that broadcast messages are allowed
- BOOL optval = TRUE;
- if ( SOCKET_ERROR == setsockopt( sock, SOL_SOCKET, SO_BROADCAST, (char *) &optval, sizeof( BOOL ) ) )
- {
- SetLastError( WSAGetLastError() );
- closesocket( sock );
- return false;
- }
- }
- #ifdef SOCKHANDLE_CONFIGBUF
- // configure buffer size
- socklen_t rcvbuf = BUFFER_SIZE;
- if ( SOCKET_ERROR == setsockopt( sock, SOL_SOCKET, SO_RCVBUF, (char *) &rcvbuf, sizeof( int ) ) )
- {
- SetLastError( WSAGetLastError() );
- closesocket( sock );
- return false;
- }
- #endif
- }
- // Associate a local address with the socket
- SockAddrIn sockAddr;
- sockAddr.CreateFrom(pszHostName, pszServiceName, nFamily);
- if ( SOCKET_ERROR == bind(sock, sockAddr, (int)sockAddr.Size()))
- {
- SetLastError( WSAGetLastError() );
- closesocket( sock );
- return false;
- }
- // Listen to the socket, only valid for connection socket (TCP)
- if (SOCK_STREAM == nType)
- {
- if ( SOCKET_ERROR == listen(sock, SOMAXCONN))
- {
- SetLastError( WSAGetLastError() );
- closesocket( sock );
- return false;
- }
- }
- // Success, now we may save this socket
- m_hSocket = sock;
- }
- return (INVALID_SOCKET != sock);
- }
- /************************************************************************/
- /* 函数:ConnectTo[2/29/2016 IT];
- /* 描述:客户端连接服务端;
- /* 参数:;
- /* [IN] pszHostName:Hostname or NIC address;
- /* [IN] pszRemote:Remote network address;
- /* [IN] pszServiceName:Network service name or port number;
- /* [IN] nFamily:address family to use (AF_INET, AF_INET6);
- /* [IN] nType:type of socket to create (SOCK_STREAM, SOCK_DGRAM);
- /* 返回:return true if successful, otherwise false (call WSAGetLastError() to retrieve latest error);
- /* 注意:关联函数InitLibrary, CreateSocket, IsOpen;
- /* 示例:;
- /*
- /* 修改:;
- /* 日期:;
- /* 内容:;
- /************************************************************************/
- bool CSocketHandle::ConnectTo(IN LPCTSTR pszHostName, IN LPCTSTR pszRemote, IN LPCTSTR pszServiceName, IN int nFamily, IN int nType)
- {
- // Socket is already opened
- if ( IsOpen() ) {
- SetLastError(ERROR_ACCESS_DENIED);
- return false;
- }
- // Create a Socket that is bound to a specific service provider
- // nFamily: (AF_INET, AF_INET6)
- // nType: (SOCK_STREAM, SOCK_DGRAM)
- #ifdef SOCKHANDLE_USE_OVERLAPPED
- SOCKET sock = WSASocket(nFamily, nType, IPPROTO_IP, NULL, 0, WSA_FLAG_OVERLAPPED);
- #else
- SOCKET sock = socket(nFamily, nType, IPPROTO_IP);
- #endif
- if (INVALID_SOCKET != sock)
- {
- // Associate a local address with the socket but let provider assign a port number
- SockAddrIn sockAddr;
- if (false == sockAddr.CreateFrom(pszHostName, TEXT("0"), nFamily))
- {
- SetLastError( WSAGetLastError() );
- closesocket( sock );
- return false;
- }
- if ( SOCKET_ERROR == bind(sock, sockAddr, (int)sockAddr.Size()))
- {
- SetLastError( WSAGetLastError() );
- closesocket( sock );
- return false;
- }
- #ifdef SOCKHANDLE_CONFIGBUF
- if (nType == SOCK_DGRAM)
- {
- // configure buffer size
- socklen_t rcvbuf = BUFFER_SIZE;
- if ( SOCKET_ERROR == setsockopt( sock, SOL_SOCKET, SO_RCVBUF, (char *) &rcvbuf, sizeof( int ) ) )
- {
- SetLastError( WSAGetLastError() );
- closesocket( sock );
- return false;
- }
- }
- #endif
- // Now get destination address & port
- sockAddr.CreateFrom( pszRemote, pszServiceName, nFamily );
- // try to connect - if fail, server not ready
- if (SOCKET_ERROR == connect( sock, sockAddr, (int)sockAddr.Size()))
- {
- SetLastError( WSAGetLastError() );
- closesocket( sock );
- return false;
- }
- // Success, now we may save this socket
- m_hSocket = sock;
- }
- return (INVALID_SOCKET != sock);
- }
- /************************************************************************/
- /* 函数:Read[2/29/2016 IT];
- /* 描述:从套接字中读取内容;
- /* 参数:;
- /* [OUT] lpBuffer:Buffer to receive data;
- /* [IN] dwSize:Size of buffer in bytes;
- /* [IN] lpAddrIn:Peer address for UDP - this must be NULL for TCP;
- /* [IN] dwTimeout:Read timeout in milliseconds;
- /* 返回:return number of bytes read or (-1L) if fail;
- /* 注意:关联函数InitLibrary, CreateSocket, ConnectTo, IsOpen, ReadEx, Write, WriteEx;
- /* 示例:;
- /*
- /* 修改:;
- /* 日期:;
- /* 内容:;
- /************************************************************************/
- DWORD CSocketHandle::Read(OUT LPBYTE lpBuffer, IN DWORD dwSize, IN LPSOCKADDR lpAddrIn /* = NULL */, IN DWORD dwTimeout /* = INFINITE */)
- {
- _ASSERTE( IsOpen() );
- _ASSERTE( lpBuffer != NULL );
- if (!IsOpen() || lpBuffer == NULL || dwSize < 1L)
- return (DWORD)-1L;
- fd_set fdRead = { 0 };
- TIMEVAL stTime;
- TIMEVAL *pstTime = NULL;
- if ( INFINITE != dwTimeout ) {
- stTime.tv_sec = dwTimeout/1000;
- stTime.tv_usec = (dwTimeout%1000)*1000;
- pstTime = &stTime;
- }
- SOCKET s = GetSocket();
- // Set Descriptor
- FD_SET( s, &fdRead );
- // Select function set read timeout
- DWORD dwBytesRead = 0L;
- int res = 1;
- if ( pstTime != NULL )
- res = select((int)s, &fdRead, NULL, NULL, pstTime );
- if ( res > 0)
- {
- if (lpAddrIn)
- {
- // UDP
- socklen_t fromlen = sizeof(SOCKADDR_STORAGE);
- res = recvfrom(s, reinterpret_cast<LPSTR>(lpBuffer), dwSize, 0, lpAddrIn, &fromlen);
- }
- else
- {
- // TCP
- res = recv(s, reinterpret_cast<LPSTR>(lpBuffer), dwSize, 0);
- }
- if ( res == 0 ) {
- WSASetLastError(WSAECONNRESET);
- res = SOCKET_ERROR;
- }
- }
- if ( res == SOCKET_ERROR )
- {
- SetLastError( WSAGetLastError() );
- }
- dwBytesRead = (DWORD)((res >= 0)?(res) : (-1));
- return dwBytesRead;
- }
- #ifdef WIN32
- /************************************************************************/
- /* 函数:ReadEx[2/29/2016 IT];
- /* 描述:从套接字读取内容,异步模式(asynchronous mode);
- /* 参数:;
- /* [OUT] lpBuffer:Buffer to receive data;
- /* [IN] dwSize:Size of buffer in bytes;
- /* [IN] lpAddrIn:SockAddrIn for UDP - this must be NULL for TCP;
- /* [IN] lpOverlapped:Windows Overlapped structure (required);
- /* [IN] lpCompletionRoutine:Winsock Completion routine (required);
- /* 返回:return number of bytes read, overlapped operation is pending or (-1L) if fail;
- /* 注意:关联函数 InitLibrary, CreateSocket, ConnectTo, IsOpen, Read, Write, WriteEx, IOControl, GetTransferOverlappedResult;
- /* 示例:;
- /*
- /* 修改:;
- /* 日期:;
- /* 内容:;
- /************************************************************************/
- DWORD CSocketHandle::ReadEx(OUT LPBYTE lpBuffer, IN DWORD dwSize, IN LPSOCKADDR lpAddrIn, IN LPWSAOVERLAPPED lpOverlapped, IN LPWSACOMPLETIONROUTINE lpCompletionRoutine)
- {
- _ASSERTE( IsOpen() );
- _ASSERTE( lpBuffer != NULL );
- if (!IsOpen() || lpBuffer == NULL || dwSize < 1L)
- return (DWORD)-1L;
- SOCKET s = GetSocket();
- // Send message to peer
- WSABUF wsabuf;
- wsabuf.buf = (char FAR*)lpBuffer;
- wsabuf.len = dwSize;
- // Select function set read timeout
- DWORD dwBytesRead = 0L;
- DWORD dwFlags = 0L;
- int res = 0;
- if (lpAddrIn)
- {
- // UDP
- socklen_t fromlen = sizeof(SOCKADDR_STORAGE);
- res = WSARecvFrom( s, &wsabuf, 1, &dwBytesRead, &dwFlags, lpAddrIn, &fromlen, lpOverlapped, lpCompletionRoutine);
- }
- else
- {
- // TCP
- res = WSARecv( s, &wsabuf, 1, &dwBytesRead, &dwFlags, lpOverlapped, lpCompletionRoutine);
- }
- if ( res == SOCKET_ERROR )
- {
- res = WSAGetLastError();
- if ( res != WSA_IO_PENDING )
- {
- dwBytesRead = (DWORD)-1L;
- SetLastError( res );
- }
- }
- return dwBytesRead;
- }
- #endif
- /************************************************************************/
- /* 函数:Write[2/29/2016 IT];
- /* 描述:向目标套接字发送内容;
- /* 参数:;
- /* [IN] lpBuffer:Buffer to send;
- /* [IN] dwCount:Number of bytes to send;
- /* [IN] lpAddrIn:Peer address for UDP - this must be NULL for TCP;
- /* [IN] dwTimeout:Write timeout in milliseconds;
- /* 返回:return number of bytes sent or (-1L) if fail;
- /* 注意:关联函数InitLibrary, CreateSocket, ConnectTo, IsOpen, Read, ReadEx, WriteEx;
- /* 示例:;
- /*
- /* 修改:;
- /* 日期:;
- /* 内容:;
- /************************************************************************/
- DWORD CSocketHandle::Write(IN const LPBYTE lpBuffer, IN DWORD dwCount, IN const LPSOCKADDR lpAddrIn /* = NULL */, IN DWORD dwTimeout /* = INFINITE */)
- {
- _ASSERTE( IsOpen() );
- _ASSERTE( NULL != lpBuffer );
- // validate params
- if (!IsOpen() || NULL == lpBuffer)
- return (DWORD)-1L;
- fd_set fdWrite = { 0 };
- TIMEVAL stTime;
- TIMEVAL *pstTime = NULL;
- if ( INFINITE != dwTimeout ) {
- stTime.tv_sec = dwTimeout/1000;
- stTime.tv_usec = (dwTimeout%1000)*1000;
- pstTime = &stTime;
- }
- SOCKET s = GetSocket();
- // Set Descriptor
- FD_SET( s, &fdWrite );
- // Select function set write timeout
- DWORD dwBytesWritten = 0L;
- int res = 1;
- if ( pstTime != NULL )
- {
- res = select((int)s, NULL, &fdWrite, NULL, pstTime );
- }
- if ( res > 0)
- {
- // Send message to peer
- if (lpAddrIn)
- {
- // UDP
- res = sendto( s, reinterpret_cast<LPCSTR>(lpBuffer), dwCount, 0, lpAddrIn, sizeof(SOCKADDR_STORAGE));
- }
- else
- {
- // TCP
- res = send( s, reinterpret_cast<LPCSTR>(lpBuffer), dwCount, 0);
- }
- }
- if ( res == SOCKET_ERROR )
- {
- SetLastError( WSAGetLastError() );
- }
- dwBytesWritten = (DWORD)((res >= 0)?(res) : (-1));
- return dwBytesWritten;
- }
- #ifdef WIN32
- /************************************************************************/
- /* 函数:WriteEx[2/29/2016 IT];
- /* 描述:向目标套接字发送内容,异步模式(asynchronous mode);
- /* 参数:;
- /* [IN] lpBuffer:Buffer to send;
- /* [IN] dwCount:Number of bytes to send;
- /* [IN] lpAddrIn:SockAddrIn for UDP - this must be NULL for TCP;
- /* [IN] lpOverlapped:Windows Overlapped structure (required);
- /* [IN] lpCompletionRoutine:Winsock Completion routine (required);
- /* 返回:return number of bytes read, overlapped operation is pending or (-1L) if fail;
- /* 注意:关联函数InitLibrary, CreateSocket, ConnectTo, IsOpen, Read, ReadEx, Write, IOControl, GetTransferOverlappedResult;
- /* 示例:;
- /*
- /* 修改:;
- /* 日期:;
- /* 内容:;
- /************************************************************************/
- DWORD CSocketHandle::WriteEx(IN const LPBYTE lpBuffer, IN DWORD dwCount, IN const LPSOCKADDR lpAddrIn, IN LPWSAOVERLAPPED lpOverlapped, IN LPWSACOMPLETIONROUTINE lpCompletionRoutine)
- {
- _ASSERTE( IsOpen() );
- _ASSERTE( NULL != lpBuffer );
- // validate params
- if (!IsOpen() || NULL == lpBuffer)
- return (DWORD)-1L;
- SOCKET s = GetSocket();
- // Select function set write timeout
- DWORD dwBytesWritten = 0L;
- int res = 0;
- // Send message to peer
- WSABUF wsabuf;
- wsabuf.buf = (char FAR*) lpBuffer;
- wsabuf.len = dwCount;
- if (lpAddrIn)
- {
- // UDP
- res = WSASendTo( s, &wsabuf, 1, &dwBytesWritten, 0, lpAddrIn, sizeof(SOCKADDR_STORAGE),
- lpOverlapped, lpCompletionRoutine);
- }
- else // TCP
- res = WSASend( s, &wsabuf, 1, &dwBytesWritten, 0, lpOverlapped, lpCompletionRoutine);
- if ( res == SOCKET_ERROR )
- {
- res = WSAGetLastError();
- if ( res != WSA_IO_PENDING )
- {
- dwBytesWritten = (DWORD)-1L;
- SetLastError( res );
- }
- }
- return dwBytesWritten;
- }
- #endif
- #ifdef WIN32
- /************************************************************************/
- /* 函数:IOControl[2/29/2016 IT];
- /* 描述:Control the mode of a socket (asynchronous mode);
- /* 参数:;
- /* [IN] dwIoCode:Control code of operation to perform;
- /* [IN] lpInBuffer:Pointer to the input buffer;
- /* [IN] cbInBuffer:Size of the input buffer, in bytes;
- /* [IN] lpOutBuffer:Pointer to the output buffer;
- /* [IN] cbOutBuffer:Size of the output buffer, in bytes;
- /* [IN] lpcbBytesReturned:Pointer to actual number of bytes of output;
- /* [IN] lpOverlapped:Winsock Overlapped structure;
- /* [IN] lpCompletionRoutine:Winsock Completion routine;
- /* 返回:return true if successful, otherwise false (call WSAGetLastError() to retrieve latest error);
- /* 注意:关联函数InitLibrary, CreateSocket, ConnectTo, IsOpen, ReadEx, WriteEx, GetTransferOverlappedResult;
- /* 示例:;
- /*
- /* 修改:;
- /* 日期:;
- /* 内容:;
- /************************************************************************/
- bool CSocketHandle::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 )
- {
- _ASSERTE( IsOpen() );
- // validate params
- if ( !IsOpen() ) {
- SetLastError(ERROR_INVALID_HANDLE);
- return false;
- }
- int res;
- SOCKET s = GetSocket();
- res = WSAIoctl(s, dwIoCode, lpInBuffer, cbInBuffer, lpOutBuffer, cbOutBuffer,
- lpcbBytesReturned, lpOverlapped, lpCompletionRoutine);
- if ( res == SOCKET_ERROR )
- {
- SetLastError( WSAGetLastError() );
- }
- return ( res != SOCKET_ERROR );
- }
- /************************************************************************/
- /* 函数:GetTransferOverlappedResult[2/29/2016 IT];
- /* 描述:Get Overlapped result (asynchronous mode);
- /* 参数:;
- /* [IN] lpOverlapped:Windows Overlapped structure (required);
- /* [IN] lpcbTransfer:Pointer to get number of bytes transferred;
- /* [IN] bWait:Force wait for overlapped operation to complete;
- /* [IN] lpdwFlags:Optional flags (see MSDN on WSARecv API);
- /* [OUT] :;
- /* [IN/OUT] :;
- /* 返回:成功返回TRUE;
- /* 注意:关联函数InitLibrary, CreateSocket, ConnectTo, IsOpen, ReadEx, WriteEx, IOControl;
- /* 示例:;
- /*
- /* 修改:;
- /* 日期:;
- /* 内容:;
- /************************************************************************/
- bool CSocketHandle::GetTransferOverlappedResult(IN LPWSAOVERLAPPED lpOverlapped, IN LPDWORD lpcbTransfer, IN bool bWait /* = true */, IN LPDWORD lpdwFlags /* = 0 */)
- {
- _ASSERTE( IsOpen() );
- _ASSERTE( NULL != lpOverlapped );
- // validate params
- if (!IsOpen() || NULL == lpOverlapped) {
- SetLastError(ERROR_INVALID_HANDLE);
- return false;
- }
- SOCKET s = GetSocket();
- DWORD dwFlags = 0;
- if ( lpdwFlags == NULL )
- lpdwFlags = &dwFlags;
- BOOL bRet = WSAGetOverlappedResult( s, lpOverlapped, lpcbTransfer, bWait, lpdwFlags );
- if ( !bRet )
- {
- SetLastError( WSAGetLastError() );
- }
- return (bRet != FALSE);
- }
- #endif
- ///////////////////////////////////////////////////////////////////////////////
- // Utility functions
- /************************************************************************/
- /* 函数:InitLibrary[2/29/2016 IT];
- /* 描述:Initialize Winsock library. This function calls WSAStartup;
- /* 参数:;
- /* [IN] wVersion:Winsock version use MAKEWORD macro if possible (e.g.: MAKEWORD(2,2));
- /* 返回:return true if successful;
- /* 注意:关联ReleaseLibrary;
- /* 示例:;
- /*
- /* 修改:;
- /* 日期:;
- /* 内容:;
- /************************************************************************/
- bool CSocketHandle::InitLibrary(IN WORD wVersion)
- {
- #ifdef WIN32
- WSADATA WSAData = { 0 };
- return ( 0 == WSAStartup( wVersion, &WSAData ) );
- #else
- return true;
- #endif
- }
- /************************************************************************/
- /* 函数:ReleaseLibrary[2/29/2016 IT];
- /* 描述:Release Winsock library;
- /* 参数:;
- /* 返回:return true if successful;
- /* 注意:关联InitLibrary;
- /* 示例:;
- /*
- /* 修改:;
- /* 日期:;
- /* 内容:;
- /************************************************************************/
- bool CSocketHandle::ReleaseLibrary()
- {
- #ifdef WIN32
- return ( 0 == WSACleanup() );
- #else
- return true;
- #endif
- }
- /************************************************************************/
- /* 函数:WaitForConnection[2/29/2016 IT];
- /* 描述:Wait for a new connection;
- /* 参数:;
- /* [IN] sock:A TCP socket handle. A new socket is return returned;
- /* 返回:return A new socket when a new client connects;
- /* 注意:关联GetSocket, CreateSocket;
- /* 示例:;
- /*
- /* 修改:;
- /* 日期:;
- /* 内容:;
- /************************************************************************/
- SOCKET CSocketHandle::WaitForConnection(IN SOCKET sock)
- {
- #if 0
- return accept(sock, 0, 0);
- #else
- SOCKET ncs = accept(sock, 0, 0);
- SetKeepLive(ncs);
- return ncs;
- #endif
- }
- /************************************************************************/
- /* 函数:ShutdownConnection[2/29/2016 IT];
- /* 描述:Shutdown a connection;
- /* 参数:;
- /* [IN] sock:Socket to shutdown communication;
- /* 返回:return true if successful;
- /* 注意:;
- /* 示例:;
- /*
- /* 修改:;
- /* 日期:;
- /* 内容:;
- /************************************************************************/
- bool CSocketHandle::ShutdownConnection(IN SOCKET sock)
- {
- shutdown(sock, SD_BOTH);
- return ( 0 == closesocket( sock ));
- }
- static unsigned char chMinClassA_IP [] = { 1, 0, 0, 0 } ;
- static unsigned char chMinClassD_IP [] = { 224, 0, 0, 0 } ;
- static unsigned char chMaxClassD_IP [] = { 239, 255, 255, 255 } ;
- /************************************************************************/
- /* 函数:IsUnicastIP[2/29/2016 IT];
- /* 描述:Check if IP address is unicast (network order);
- /* 参数:;
- /* [IN] ulAddr:IP address (expected valid unicast address);
- /* 返回:return true if successful;
- /* 注意:;
- /* 示例:;
- /*
- /* 修改:;
- /* 日期:;
- /* 内容:;
- /************************************************************************/
- bool CSocketHandle::IsUnicastIP( ULONG ulAddr )
- {
- return (((unsigned char *) & ulAddr) [0] >= chMinClassA_IP [0] &&
- ((unsigned char *) & ulAddr) [0] < chMinClassD_IP [0]) ;
- }
- /************************************************************************/
- /* 函数:IsMulticastIP[2/29/2016 IT];
- /* 描述:Check if IP address is multicast (network order);
- /* 参数:;
- /* [IN] ulAddr:IP address (expected valid multicast address);
- /* 返回:return true if successful;
- /* 注意:;
- /* 示例:;
- /*
- /* 修改:;
- /* 日期:;
- /* 内容:;
- /************************************************************************/
- bool CSocketHandle::IsMulticastIP( ULONG ulAddr )
- {
- return (((unsigned char *) & ulAddr) [0] >= chMinClassD_IP [0] &&
- ((unsigned char *) & ulAddr) [0] <= chMaxClassD_IP [0]) ;
- }
- /************************************************************************/
- /* 函数:FormatIP[2/29/2016 IT];
- /* 描述:Format IP address to string;
- /* 参数:;
- /* [IN] pszIPAddr:Buffer to hold string;
- /* [IN] nSize:Size of buffer in characters;
- /* [IN] ulAddr:IP Address to format;
- /* [IN] bFmtHost:Specify if address (ulAddr) is in host (true) or network format (false);
- /* 返回:return true if successful. Possible error could be INSUFFICIENT_BUFFER;
- /* 注意:;
- /* 示例:;
- /*
- /* 修改:;
- /* 日期:;
- /* 内容:;
- /************************************************************************/
- bool CSocketHandle::FormatIP(LPTSTR pszIPAddr, UINT nSize, ULONG ulAddr, bool bFmtHost)
- {
- if ( pszIPAddr && nSize > 8)
- {
- if ( bFmtHost )
- ulAddr = htonl( ulAddr );
- // Create Address string
- return (SUCCEEDED(StringCchPrintf(pszIPAddr, nSize, TEXT("%u.%u.%u.%u"),
- (UINT)(((PBYTE) &ulAddr)[0]),
- (UINT)(((PBYTE) &ulAddr)[1]),
- (UINT)(((PBYTE) &ulAddr)[2]),
- (UINT)(((PBYTE) &ulAddr)[3]))));
- }
- SetLastError(ERROR_INSUFFICIENT_BUFFER);
- return false;
- }
- /************************************************************************/
- /* 函数:FormatIP[2/29/2016 IT];
- /* 描述:Format IP address to string;
- /* 参数:;
- /* [IN] pszIPAddr:Buffer to hold string;
- /* [IN] nSize:Size of buffer in characters;
- /* [IN] addrIn:IP Address to format;
- /* 返回:return true if successful. Possible error could be INSUFFICIENT_BUFFER;
- /* 注意:;
- /* 示例:;
- /*
- /* 修改:;
- /* 日期:;
- /* 内容:;
- /************************************************************************/
- bool CSocketHandle::FormatIP(LPTSTR pszIPAddr, UINT nSize, const SockAddrIn& addrIn)
- {
- if ( pszIPAddr && nSize > 8)
- {
- const void* addr;
- char szIPAddr[MAX_PATH] = { 0 };
- if (addrIn.ss_family == AF_INET) {
- addr = &((const sockaddr_in*)&addrIn)->sin_addr;
- } else {
- addr = &((const sockaddr_in6*)&addrIn)->sin6_addr;
- }
- if (inet_ntop(addrIn.ss_family, const_cast<void*>(addr), szIPAddr, MAX_PATH) != NULL)
- {
- #ifdef _UNICODE
- return (0 != MultiByteToWideChar(CP_UTF8, 0, szIPAddr, -1, pszIPAddr, nSize ));
- #else
- ::StringCbCopyA(pszIPAddr, nSize, szIPAddr);
- return true;
- #endif
- }
- }
- SetLastError(ERROR_INSUFFICIENT_BUFFER);
- return false;
- }
- /************************************************************************/
- /* 函数:GetPortNumber[2/29/2016 IT];
- /* 描述:Get service port number;
- /* 参数:;
- /* [IN] pszServiceName:Network service name (e.g.: "ftp", "telnet") or port number;
- /* 返回:return port number;
- /* 注意:;
- /* 示例:;
- /*
- /* 修改:;
- /* 日期:;
- /* 内容:;
- /************************************************************************/
- USHORT CSocketHandle::GetPortNumber( LPCTSTR pszServiceName )
- {
- LPSERVENT lpservent;
- USHORT nPort = 0;
- if ( _istdigit( pszServiceName[0] ) ) {
- nPort = (USHORT) _ttoi( pszServiceName );
- }
- else {
- #ifdef _UNICODE
- char pstrService[HOSTNAME_SIZE] = { 0 };
- WideCharToMultiByte(CP_UTF8, 0, pszServiceName, -1, pstrService, sizeof(pstrService), NULL, NULL );
- #else
- LPCTSTR pstrService = pszServiceName;
- #endif
- // Convert network byte order to host byte order
- if ( (lpservent = getservbyname( pstrService, NULL )) != NULL )
- nPort = ntohs( lpservent->s_port );
- }
- return nPort;
- }
- /************************************************************************/
- /* 函数:GetIPAddress[2/29/2016 IT];
- /* 描述:Get IP address of a host;
- /* 参数:;
- /* [IN] pszHostName:host name or IP address;
- /* 返回:return Host IP address in host format;
- /* 注意:关联GetAddressInfo;
- /* 示例:;
- /*
- /* 修改:;
- /* 日期:;
- /* 内容:;
- /************************************************************************/
- ULONG CSocketHandle::GetIPAddress( LPCTSTR pszHostName )
- {
- LPHOSTENT lphostent;
- ULONG uAddr = INADDR_NONE;
- TCHAR szLocal[HOSTNAME_SIZE] = { 0 };
- // if no name specified, get local
- if ( NULL == pszHostName || !pszHostName[0])
- {
- GetLocalName(szLocal, HOSTNAME_SIZE);
- pszHostName = szLocal;
- }
- #ifdef _UNICODE
- char pstrHost[HOSTNAME_SIZE] = { 0 };
- WideCharToMultiByte(CP_UTF8, 0, pszHostName, -1, pstrHost, sizeof(pstrHost), NULL, NULL );
- #else
- LPCTSTR pstrHost = pszHostName;
- #endif
- // Check for an Internet Protocol dotted address string
- uAddr = inet_addr( pstrHost );
- if ( (INADDR_NONE == uAddr) && (strcmp( pstrHost, "255.255.255.255" )) )
- {
- // It's not an address, then try to resolve it as a hostname
- if ( (lphostent = gethostbyname( pstrHost )) != NULL )
- uAddr = *((ULONG *) lphostent->h_addr_list[0]);
- }
- return ntohl( uAddr );
- }
- /************************************************************************/
- /* 函数:GetLocalName[2/29/2016 IT];
- /* 描述:Get current localname for this machine;
- /* 参数:;
- /* [IN] pszName:Buffer to receive host name;
- /* [IN] nSize:Size of this buffer in character;
- /* 返回:return true if successful;
- /* 注意:;
- /* 示例:;
- /*
- /* 修改:;
- /* 日期:;
- /* 内容:;
- /************************************************************************/
- bool CSocketHandle::GetLocalName(LPTSTR pszName, UINT nSize)
- {
- if (pszName != NULL && nSize > 0)
- {
- char szHost[HOSTNAME_SIZE] = { 0 };
- // get host name, if fail, SetLastError is set
- if (SOCKET_ERROR != gethostname(szHost, sizeof(szHost)))
- {
- struct hostent* hp;
- hp = gethostbyname(szHost);
- if (hp != NULL) {
- ::StringCbCopyA(szHost, HOSTNAME_SIZE, hp->h_name);
- }
- // check if user provide enough buffer
- size_t cbLength = 0;
- ::StringCbLengthA(szHost, HOSTNAME_SIZE, &cbLength);
- if ( cbLength > nSize )
- {
- SetLastError(ERROR_INSUFFICIENT_BUFFER);
- return false;
- }
- // Unicode conversion
- #ifdef _UNICODE
- return (0 != MultiByteToWideChar(CP_UTF8, 0, szHost, -1, pszName, nSize ));
- #else
- ::StringCbCopyA(pszName, nSize, szHost);
- return true;
- #endif
- }
- else
- SetLastError( WSAGetLastError() );
- }
- else
- SetLastError(ERROR_INVALID_PARAMETER);
- return false;
- }
- /************************************************************************/
- /* 函数:GetLocalAddress[2/29/2016 IT];
- /* 描述:Get current (default) IP address for this machine;
- /* 参数:;
- /* [IN] pszAddress:Buffer to receive IP address (IPv4, IPv6 format);
- /* [IN] nSize:Size of this buffer in character;
- /* [IN] nFamily:address family to use (AF_INET, AF_INET6);
- /* 返回:return true if successful;
- /* 注意:;
- /* 示例:;
- /*
- /* 修改:;
- /* 日期:;
- /* 内容:;
- /************************************************************************/
- bool CSocketHandle::GetLocalAddress(LPTSTR pszAddress, UINT nSize, int nFamily /*= AF_INET*/)
- {
- if (pszAddress != NULL && nSize > 0)
- {
- TCHAR szHost[HOSTNAME_SIZE] = { 0 };
- // Get computer local address
- // get host name, if fail, SetLastError is set
- if (GetLocalName(szHost, HOSTNAME_SIZE))
- {
- char szAddress[MAX_PATH] = { 0 };
- #ifdef _UNICODE
- char pstrHost[HOSTNAME_SIZE] = { 0 };
- WideCharToMultiByte(CP_UTF8, 0, szHost, -1, pstrHost, sizeof(pstrHost), NULL, NULL );
- #else
- LPCTSTR pstrHost = szHost;
- #endif
- // get address info
- sockaddr_storage addr_store = { 0 };
- addr_store.ss_family = static_cast<short>(nFamily);
- inet_pton(nFamily, pstrHost, &addr_store);
- const void* addr;
- if (addr_store.ss_family == AF_INET) {
- addr = &((const sockaddr_in*)&addr_store)->sin_addr;
- } else {
- addr = &((const sockaddr_in6*)&addr_store)->sin6_addr;
- }
- if (inet_ntop(addr_store.ss_family, const_cast<void*>(addr), szAddress, MAX_PATH) != NULL)
- {
- // Unicode conversion
- #ifdef _UNICODE
- return (0 != MultiByteToWideChar(CP_UTF8, 0, szAddress, -1, pszAddress, nSize ));
- #else
- ::StringCbCopyA(pszAddress, nSize, szAddress);
- return true;
- #endif
- }
- else
- SetLastError( WSAGetLastError() );
- }
- }
- else
- SetLastError(ERROR_INVALID_PARAMETER);
- return false;
- }
- /************************************************************************/
- /* 函数:GetAddressInfo[2/29/2016 IT];
- /* 描述:Get IP address info of a host (Supports: IPv4 and IPv6);
- /* 参数:;
- /* [IN] pszHostName:host name or IP address;
- /* [IN] pszServiceName:pszServiceName Network service name (e.g.: "ftp", "telnet") or port number;
- /* [IN] nFamily:address family to use (AF_INET, AF_INET6);
- /* [IN] sockAddr:Socket address to fill in;
- /* 返回:return true if successful;
- /* 注意:;
- /* 示例:;
- /*
- /* 修改:;
- /* 日期:;
- /* 内容:;
- /************************************************************************/
- bool CSocketHandle::GetAddressInfo(LPCTSTR pszHostName, LPCTSTR pszServiceName,int nFamily, SockAddrIn& sockAddr)
- {
- const TCHAR szZERO[] = TEXT("0");
- ADDRINFO aiHints;
- ADDRINFO *aiList = NULL;
- memset(&aiHints, 0, sizeof(aiHints));
- aiHints.ai_flags = AI_ADDRCONFIG; // Jeff.暂时禁掉;
- aiHints.ai_family = static_cast<short>(nFamily);
- TCHAR szLocal[HOSTNAME_SIZE] = { 0 };
- // if no name specified, get local
- if ( NULL == pszHostName || !pszHostName[0])
- {
- GetLocalName(szLocal, HOSTNAME_SIZE);
- pszHostName = szLocal;
- }
- if ( NULL == pszServiceName || !pszServiceName[0])
- {
- pszServiceName = szZERO;
- }
- #ifdef _UNICODE
- char pstrHost[HOSTNAME_SIZE] = { 0 };
- WideCharToMultiByte(CP_UTF8, 0, pszHostName, -1, pstrHost, sizeof(pstrHost), NULL, NULL );
- char pstrService[HOSTNAME_SIZE] = { 0 };
- WideCharToMultiByte(CP_UTF8, 0, pszServiceName, -1, pstrService, sizeof(pstrService), NULL, NULL );
- #else
- LPCTSTR pstrHost = pszHostName;
- LPCTSTR pstrService = pszServiceName;
- #endif
- if ( SOCKET_ERROR != getaddrinfo(pstrHost, pstrService, &aiHints, &aiList) && ( aiList != 0 ))
- {
- ADDRINFO ai = { 0 };
- ai.ai_addr = sockAddr;
- memcpy(ai.ai_addr, aiList->ai_addr, aiList->ai_addrlen);
- freeaddrinfo( aiList );
- return true;
- }
- SetLastError( WSAGetLastError() );
- return false;
- }
- ///////////////////////////////////////////////////////////////////////////////
- // Globals
- ///////////////////////////////////////////////////////////////////////////////
- #if !defined(PLATFORM_HAS_INETFUNC)
- ///////////////////////////////////////////////////////////////////////////////
- // inet_ntop
- const char *inet_ntop(int af, const void *src, char *dst, socklen_t cnt)
- {
- if ( dst != NULL)
- {
- dst[0] = 0;
- if (af == AF_INET)
- {
- sockaddr_in in;
- memset(&in, 0, sizeof(in));
- in.sin_family = AF_INET;
- memcpy(&in.sin_addr, src, sizeof(in_addr));
- getnameinfo((sockaddr *)&in, sizeof(sockaddr_in), dst, cnt, NULL, 0, NI_NUMERICHOST);
- return dst;
- }
- else if (af == AF_INET6)
- {
- sockaddr_in6 in;
- memset(&in, 0, sizeof(in));
- in.sin6_family = AF_INET6;
- memcpy(&in.sin6_addr, src, sizeof(in6_addr));
- getnameinfo((sockaddr *)&in, sizeof(sockaddr_in6), dst, cnt, NULL, 0, NI_NUMERICHOST);
- return dst;
- }
- }
- WSASetLastError(WSA_INVALID_PARAMETER);
- return dst;
- }
- ///////////////////////////////////////////////////////////////////////////////
- // inet_pton
- int inet_pton(int af, const char *src, void *dst)
- {
- int result = SOCKET_ERROR;
- addrinfo aiHints, *aiList = NULL;
- memset(&aiHints, 0, sizeof(aiHints));
- aiHints.ai_family = af;
- if ( SOCKET_ERROR != getaddrinfo(src, NULL, &aiHints, &aiList) && (aiList != NULL))
- {
- memcpy(dst, aiList->ai_addr, aiList->ai_addrlen);
- freeaddrinfo(aiList);
- result = 0;
- }
- return result;
- }
- #endif
|