Replaced the deprecated gethostbyname with getaddrinfo (#47)

This commit is contained in:
Laurent Gomila 2013-06-21 19:25:06 +02:00
parent 96d0204f30
commit 34d866d4bc
2 changed files with 15 additions and 3 deletions

View File

@ -28,6 +28,7 @@
#include <SFML/Network/IpAddress.hpp> #include <SFML/Network/IpAddress.hpp>
#include <SFML/Network/Http.hpp> #include <SFML/Network/Http.hpp>
#include <SFML/Network/SocketImpl.hpp> #include <SFML/Network/SocketImpl.hpp>
#include <cstring>
namespace namespace
@ -48,9 +49,19 @@ namespace
return ip; return ip;
// Not a valid address, try to convert it as a host name // Not a valid address, try to convert it as a host name
hostent* host = gethostbyname(address.c_str()); addrinfo hints;
if (host) std::memset(&hints, 0, sizeof(hints));
return reinterpret_cast<in_addr*>(host->h_addr)->s_addr; hints.ai_family = AF_INET;
addrinfo* result = NULL;
if (getaddrinfo(address.c_str(), NULL, &hints, &result) == 0)
{
if (result)
{
ip = reinterpret_cast<sockaddr_in*>(result->ai_addr)->sin_addr.s_addr;
freeaddrinfo(result);
return ip;
}
}
// Not a valid address nor a host name // Not a valid address nor a host name
return 0; return 0;

View File

@ -30,6 +30,7 @@
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
#include <SFML/Network/Socket.hpp> #include <SFML/Network/Socket.hpp>
#include <winsock2.h> #include <winsock2.h>
#include <ws2tcpip.h>
namespace sf namespace sf