1
0

Fixed socket connection startup.

This commit is contained in:
madmaxoft 2013-11-24 14:28:51 +01:00
parent e683c54b49
commit 1f8399fae5
2 changed files with 8 additions and 7 deletions

View File

@ -169,7 +169,7 @@ bool cSocket::SetReuseAddress(void)
int cSocket::WSAStartup() int cSocket::WSAStartup(void)
{ {
#ifdef _WIN32 #ifdef _WIN32
WSADATA wsaData; WSADATA wsaData;
@ -336,23 +336,23 @@ bool cSocket::ConnectIPv4(const AString & a_HostNameOrAddr, unsigned short a_Por
{ {
// First try IP Address string to hostent conversion, because it's faster // First try IP Address string to hostent conversion, because it's faster
unsigned long addr = inet_addr(a_HostNameOrAddr.c_str()); unsigned long addr = inet_addr(a_HostNameOrAddr.c_str());
hostent * hp = gethostbyaddr((char*)&addr, sizeof(addr), AF_INET); if (addr == INADDR_NONE)
if (hp == NULL)
{ {
// It is not an IP Address string, but rather a regular hostname, resolve: // It is not an IP Address string, but rather a regular hostname, resolve:
hp = gethostbyname(a_HostNameOrAddr.c_str()); hostent * hp = gethostbyname(a_HostNameOrAddr.c_str());
if (hp == NULL) if (hp == NULL)
{ {
LOGWARN("cTCPLink: Could not resolve hostname \"%s\"", a_HostNameOrAddr.c_str()); LOGWARNING("%s: Could not resolve hostname \"%s\"", __FUNCTION__, a_HostNameOrAddr.c_str());
CloseSocket(); CloseSocket();
return false; return false;
} }
addr = *((unsigned long*)hp->h_addr);
} }
sockaddr_in server; sockaddr_in server;
server.sin_addr.s_addr = *((unsigned long*)hp->h_addr); server.sin_addr.s_addr = addr;
server.sin_family = AF_INET; server.sin_family = AF_INET;
server.sin_port = htons( (unsigned short)a_Port ); server.sin_port = htons((unsigned short)a_Port);
return (connect(m_Socket, (sockaddr *)&server, sizeof(server)) == 0); return (connect(m_Socket, (sockaddr *)&server, sizeof(server)) == 0);
} }

View File

@ -38,6 +38,7 @@ public:
/// Sets the address-reuse socket flag; returns true on success /// Sets the address-reuse socket flag; returns true on success
bool SetReuseAddress(void); bool SetReuseAddress(void);
/// Initializes the network stack. Returns 0 on success, or another number as an error code.
static int WSAStartup(void); static int WSAStartup(void);
static AString GetErrorString(int a_ErrNo); static AString GetErrorString(int a_ErrNo);