Browse Source

Improved old code in connect() and bind().

release/0.19
JustinAJ 10 years ago
parent
commit
7e7dca8e27
  1. 84
      Jupiter/Socket.cpp
  2. BIN
      Release/Jupiter.lib

84
Jupiter/Socket.cpp

@ -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)
} {
sockaddr *asock = ainfo->ai_addr; info = info->ai_next;
Jupiter::Socket::data_->rawSock = socket(ainfo->ai_family, Jupiter::Socket::data_->sockType, Jupiter::Socket::data_->sockProto); continue;
if (Jupiter::Socket::data_->rawSock == INVALID_SOCKET) continue; }
if (clientAddress != nullptr) this->bind(clientAddress, clientPort, false);
if (::connect(Jupiter::Socket::data_->rawSock, asock, ainfo->ai_addrlen) == SOCKET_ERROR) if (clientAddress != nullptr)
{ this->bind(clientAddress, clientPort, false);
i++; if (::connect(Jupiter::Socket::data_->rawSock, info->ai_addr, info->ai_addrlen) == SOCKET_ERROR)
continue; {
} info = info->ai_next;
Jupiter::Socket::freeAddrInfo(info); continue;
return true; }
Jupiter::Socket::freeAddrInfo(info_head);
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); info = info->ai_next;
if (Jupiter::Socket::data_->rawSock == INVALID_SOCKET || ::bind(Jupiter::Socket::data_->rawSock, ainfo->ai_addr, ainfo->ai_addrlen) == SOCKET_ERROR) continue;
{ }
i++; Jupiter::Socket::freeAddrInfo(info_head);
continue; if (andListen && Jupiter::Socket::data_->sockType == SOCK_STREAM && ::listen(Jupiter::Socket::data_->rawSock, SOMAXCONN) == SOCKET_ERROR)
} return false;
Jupiter::Socket::freeAddrInfo(info); return true;
if (andListen && Jupiter::Socket::data_->sockType == SOCK_STREAM && listen(Jupiter::Socket::data_->rawSock, SOMAXCONN) == SOCKET_ERROR) return false; } while (info != nullptr);
return true; Jupiter::Socket::freeAddrInfo(info_head);
} }
return false; return false;
} }

BIN
Release/Jupiter.lib

Binary file not shown.
Loading…
Cancel
Save