|
@ -161,33 +161,35 @@ bool Jupiter::Socket::connect(addrinfo *info) |
|
|
bool Jupiter::Socket::connect(const char *hostname, unsigned short iPort, const char *clientAddress, unsigned short clientPort) |
|
|
bool Jupiter::Socket::connect(const char *hostname, unsigned short iPort, const char *clientAddress, unsigned short clientPort) |
|
|
{ |
|
|
{ |
|
|
#if defined _WIN32 |
|
|
#if defined _WIN32 |
|
|
if (!socketInit && !Jupiter::Socket::init()) return false; |
|
|
if (!socketInit && !Jupiter::Socket::init()) |
|
|
|
|
|
return false; |
|
|
#endif // _WIN32
|
|
|
#endif // _WIN32
|
|
|
Jupiter::Socket::data_->host.set(hostname); |
|
|
Jupiter::Socket::data_->host.set(hostname); |
|
|
Jupiter::Socket::data_->port = iPort; |
|
|
Jupiter::Socket::data_->port = iPort; |
|
|
int i = 0; |
|
|
addrinfo *info_head = Jupiter::Socket::getAddrInfo(Jupiter::Socket::data_->host.c_str(), Jupiter::CStringS::Format("%hu", Jupiter::Socket::data_->port).c_str()); |
|
|
static char portString[256]; |
|
|
if (info_head != nullptr) |
|
|
sprintf(portString, "%hu", Jupiter::Socket::data_->port); |
|
|
|
|
|
addrinfo *info = Jupiter::Socket::getAddrInfo(Jupiter::Socket::data_->host.c_str(), portString); |
|
|
|
|
|
while (info != nullptr) |
|
|
|
|
|
{ |
|
|
{ |
|
|
addrinfo *ainfo = Jupiter::Socket::getAddrInfo(info, i); |
|
|
addrinfo *info = info_head; |
|
|
if (ainfo == nullptr) |
|
|
do |
|
|
{ |
|
|
{ |
|
|
Jupiter::Socket::freeAddrInfo(info); |
|
|
Jupiter::Socket::data_->rawSock = socket(info->ai_family, Jupiter::Socket::data_->sockType, Jupiter::Socket::data_->sockProto); |
|
|
return false; |
|
|
if (Jupiter::Socket::data_->rawSock == INVALID_SOCKET) |
|
|
|
|
|
{ |
|
|
|
|
|
info = info->ai_next; |
|
|
|
|
|
continue; |
|
|
} |
|
|
} |
|
|
sockaddr *asock = ainfo->ai_addr; |
|
|
|
|
|
Jupiter::Socket::data_->rawSock = socket(ainfo->ai_family, Jupiter::Socket::data_->sockType, Jupiter::Socket::data_->sockProto); |
|
|
if (clientAddress != nullptr) |
|
|
if (Jupiter::Socket::data_->rawSock == INVALID_SOCKET) continue; |
|
|
this->bind(clientAddress, clientPort, false); |
|
|
if (clientAddress != nullptr) this->bind(clientAddress, clientPort, false); |
|
|
if (::connect(Jupiter::Socket::data_->rawSock, info->ai_addr, info->ai_addrlen) == SOCKET_ERROR) |
|
|
if (::connect(Jupiter::Socket::data_->rawSock, asock, ainfo->ai_addrlen) == SOCKET_ERROR) |
|
|
|
|
|
{ |
|
|
{ |
|
|
i++; |
|
|
info = info->ai_next; |
|
|
continue; |
|
|
continue; |
|
|
} |
|
|
} |
|
|
Jupiter::Socket::freeAddrInfo(info); |
|
|
Jupiter::Socket::freeAddrInfo(info_head); |
|
|
return true; |
|
|
return true; |
|
|
|
|
|
} while (info != nullptr); |
|
|
|
|
|
Jupiter::Socket::freeAddrInfo(info_head); |
|
|
} |
|
|
} |
|
|
return false; |
|
|
return false; |
|
|
} |
|
|
} |
|
@ -195,31 +197,29 @@ bool Jupiter::Socket::connect(const char *hostname, unsigned short iPort, const |
|
|
bool Jupiter::Socket::bind(const char *hostname, unsigned short iPort, bool andListen) |
|
|
bool Jupiter::Socket::bind(const char *hostname, unsigned short iPort, bool andListen) |
|
|
{ |
|
|
{ |
|
|
#if defined _WIN32 |
|
|
#if defined _WIN32 |
|
|
if (!socketInit && !Jupiter::Socket::init()) return false; |
|
|
if (!socketInit && !Jupiter::Socket::init()) |
|
|
|
|
|
return false; |
|
|
#endif // _WIN32
|
|
|
#endif // _WIN32
|
|
|
Jupiter::Socket::data_->host.set(hostname); |
|
|
Jupiter::Socket::data_->host.set(hostname); |
|
|
Jupiter::Socket::data_->port = iPort; |
|
|
Jupiter::Socket::data_->port = iPort; |
|
|
int i = 0; |
|
|
addrinfo *info_head = Jupiter::Socket::getAddrInfo(Jupiter::Socket::data_->host.c_str(), Jupiter::CStringS::Format("%hu", Jupiter::Socket::data_->port).c_str()); |
|
|
static char portString[256]; |
|
|
if (info_head != nullptr) |
|
|
sprintf(portString, "%hu", Jupiter::Socket::data_->port); |
|
|
|
|
|
addrinfo *info = Jupiter::Socket::getAddrInfo(Jupiter::Socket::data_->host.c_str(), portString); |
|
|
|
|
|
while (info != nullptr) |
|
|
|
|
|
{ |
|
|
{ |
|
|
addrinfo *ainfo = Jupiter::Socket::getAddrInfo(info, i); |
|
|
addrinfo *info = info_head; |
|
|
if (ainfo == nullptr) |
|
|
do |
|
|
{ |
|
|
{ |
|
|
Jupiter::Socket::freeAddrInfo(info); |
|
|
Jupiter::Socket::data_->rawSock = socket(info->ai_family, Jupiter::Socket::data_->sockType, Jupiter::Socket::data_->sockProto); |
|
|
return false; |
|
|
if (Jupiter::Socket::data_->rawSock == INVALID_SOCKET || ::bind(Jupiter::Socket::data_->rawSock, info->ai_addr, info->ai_addrlen) == SOCKET_ERROR) |
|
|
} |
|
|
|
|
|
Jupiter::Socket::data_->rawSock = socket(ainfo->ai_family, Jupiter::Socket::data_->sockType, Jupiter::Socket::data_->sockProto); |
|
|
|
|
|
if (Jupiter::Socket::data_->rawSock == INVALID_SOCKET || ::bind(Jupiter::Socket::data_->rawSock, ainfo->ai_addr, ainfo->ai_addrlen) == SOCKET_ERROR) |
|
|
|
|
|
{ |
|
|
{ |
|
|
i++; |
|
|
info = info->ai_next; |
|
|
continue; |
|
|
continue; |
|
|
} |
|
|
} |
|
|
Jupiter::Socket::freeAddrInfo(info); |
|
|
Jupiter::Socket::freeAddrInfo(info_head); |
|
|
if (andListen && Jupiter::Socket::data_->sockType == SOCK_STREAM && listen(Jupiter::Socket::data_->rawSock, SOMAXCONN) == SOCKET_ERROR) return false; |
|
|
if (andListen && Jupiter::Socket::data_->sockType == SOCK_STREAM && ::listen(Jupiter::Socket::data_->rawSock, SOMAXCONN) == SOCKET_ERROR) |
|
|
|
|
|
return false; |
|
|
return true; |
|
|
return true; |
|
|
|
|
|
} while (info != nullptr); |
|
|
|
|
|
Jupiter::Socket::freeAddrInfo(info_head); |
|
|
} |
|
|
} |
|
|
return false; |
|
|
return false; |
|
|
} |
|
|
} |
|
|