BOOL CUdpCast::CreateClientSocket(LPCTSTR lpszRemoteAddress, USHORT usPort, LPCTSTR lpszBindAddress, HP_SOCKADDR& bindAddr) { if(m_enCastMode == CM_MULTICAST) { if(!::GetSockAddrByHostName(lpszRemoteAddress, usPort, m_castAddr)) return FALSE; } else { m_castAddr.family = AF_INET; m_castAddr.addr4.sin_addr.s_addr = INADDR_BROADCAST; m_castAddr.SetPort(usPort); } if(!lpszBindAddress || lpszBindAddress[0] == 0) { bindAddr.family = m_castAddr.family; bindAddr.SetPort(usPort); } else { if(!::sockaddr_A_2_IN(lpszBindAddress, usPort, bindAddr)) return FALSE; if(m_enCastMode == CM_BROADCAST && bindAddr.IsIPv6()) { ::WSASetLastError(WSAEPROTONOSUPPORT); return FALSE; } if(m_castAddr.family != bindAddr.family) { ::WSASetLastError(WSAEAFNOSUPPORT); return FALSE; } } m_soClient = socket(m_castAddr.family, SOCK_DGRAM, IPPROTO_UDP); if(m_soClient == INVALID_SOCKET) return FALSE; VERIFY(::SSO_UDP_ConnReset(m_soClient, FALSE) == NO_ERROR); VERIFY(::SSO_ReuseAddress(m_soClient, m_bReuseAddress) != SOCKET_ERROR); m_evSocket = ::WSACreateEvent(); ASSERT(m_evSocket != WSA_INVALID_EVENT); SetRemoteHost(lpszRemoteAddress, usPort); return TRUE; }
BOOL CTcpClient::BindClientSocket(const HP_SOCKADDR& addrBind, const HP_SOCKADDR& addrRemote, USHORT usLocalPort) { if(addrBind.IsSpecified() && usLocalPort == 0) { if(::bind(m_soClient, addrBind.Addr(), addrBind.AddrSize()) == SOCKET_ERROR) return FALSE; } else if(usLocalPort != 0) { HP_SOCKADDR realBindAddr = addrBind.IsSpecified() ? addrBind : HP_SOCKADDR::AnyAddr(addrRemote.family); realBindAddr.SetPort(usLocalPort); if(::bind(m_soClient, realBindAddr.Addr(), realBindAddr.AddrSize()) == SOCKET_ERROR) return FALSE; } m_dwConnID = ::GenerateConnectionID(); return TRUE; }