| 294 | |
| 295 | |
| 296 | bool cSocket::ConnectIPv4(const AString & a_HostNameOrAddr, unsigned short a_Port) |
| 297 | { |
| 298 | // First try IP Address string to hostent conversion, because it's faster |
| 299 | unsigned long addr = inet_addr(a_HostNameOrAddr.c_str()); |
| 300 | if (addr == INADDR_NONE) |
| 301 | { |
| 302 | // It is not an IP Address string, but rather a regular hostname, resolve: |
| 303 | hostent * hp = gethostbyname(a_HostNameOrAddr.c_str()); |
| 304 | if (hp == NULL) |
| 305 | { |
| 306 | LOGWARNING("%s: Could not resolve hostname \"%s\"", __FUNCTION__, a_HostNameOrAddr.c_str()); |
| 307 | CloseSocket(); |
| 308 | return false; |
| 309 | } |
| 310 | // Should be optimised to a single word copy |
| 311 | memcpy(&addr, hp->h_addr, hp->h_length); |
| 312 | } |
| 313 | |
| 314 | sockaddr_in server; |
| 315 | server.sin_addr.s_addr = addr; |
| 316 | server.sin_family = AF_INET; |
| 317 | server.sin_port = htons((unsigned short)a_Port); |
| 318 | return (connect(m_Socket, (sockaddr *)&server, sizeof(server)) == 0); |
| 319 | } |
| 320 | |
| 321 | |
| 322 | |