嘿,喜欢标题的人说我在尝试连接到telnet时在我的服务器中收到错误。我收到这个错误:
Error 1 error C4996: 'inet_addr': Use inet_pton() or InetPton() instead or define _WINSOCK_DEPRECATED_NO_WARNINGS to disable deprecated API warnings
现在我明白这个错误告诉我我需要使用inet_pton,因为它比我在网上发现的更新。不幸的是我无法找到我所在的网页。但我有问题将这个集成到我的代码中。这甚至让讲师正在研究解决方案。
这是我的代码人员:
// MUD Programming
// Ron Penton
// (C)2003
// SocketLibSystem.cpp - This header contains all of the socket functions
// that aren't related to any classes.
#include "SocketLibSystem.h"
#include "SocketLibErrors.h"
// this is a simple object-oriented socket library wrapper around
// winsock/BSDsockets. It will only use TCP/IP as it's method of communication,
// and will have no UDP support at all.
namespace SocketLib
{
// ========================================================================
// This class is designed to be a global singleton that initializes
// and shuts down Winsock.
// ========================================================================
#ifdef WIN32 // windows 95 and above
class System
{
public:
// ================================================================
// This initializes winsock
// ================================================================
System()
{
// attempt to start up the winsock lib
WSAStartup( MAKEWORD( 2, 2 ), &m_WSAData );
}
// ========================================================================
// This shuts down winsock
// ========================================================================
~System()
{
// attempt to close down the winsock lib
WSACleanup();
}
protected:
// holds information about winsock
WSADATA m_WSAData;
};
System g_system;
#endif
// ========================================================================
// Function: GetIPAddress
// Purpose: To get the IP address of the string as an ipaddress
// structure. Throws an exception if the address cannot be
// converted.
// ========================================================================
ipaddress GetIPAddress( const std::string p_address )
{
if( IsIPAddress( p_address ) )
{
// if the address is just a regular IP address, there's no need
// to do a DNS lookup, so just convert the string directly into
// its binary format.
ipaddress addr = inet_addr( p_address.c_str() );
//inet_pton(AF_INET, addr, buf);
// if the address is invalid, throw a HOST_NOT_FOUND exception.
if( addr == INADDR_NONE )
{
throw Exception( EDNSNotFound );
}
// by this point, the address is valid, so return it.
return addr;
}
else
{
// the address isn't an IP address, so we need to look it up using
// DNS.
struct hostent* host = gethostbyname( p_address.c_str() );
// if there was an error, throw an exception.
if( host == 0 )
{
// get the error from h_errno.
throw Exception( GetError( false ) );
}
// now perform some really wierd casting tricks to get the value.
// h_addr is a char*, so cast it into an ipaddress*, and
// dereference it to get the value.
return *((ipaddress*)host->h_addr);
}
}
// ========================================================================
// Function: GetIPString
// Purpose: Converts an ipaddress structure to a string in numerical
// format.
// ========================================================================
std::string GetIPString( ipaddress p_address )
{
// return a new string containing the address.
// (god that is some ugly casting going on... stupid language)
char* str = inet_ntoa( *((in_addr*)&p_address) );
if( str == 0 )
{
return std::string( "Invalid IP Address" );
}
return std::string( str );
}
// ========================================================================
// Function: GetHostNameString
// Purpose: Converts an ipaddress structure to a string using
// reverse-DNS lookup. This may block.
// ========================================================================
std::string GetHostNameString( ipaddress p_address )
{
// get the host info.
struct hostent* host = gethostbyaddr( (char*)&p_address, 4, AF_INET );
// if there was an error, throw an exception.
if( host == 0 )
{
// get the error from h_errno.
throw Exception( GetError( false ) );
}
return std::string( host->h_name );
}
// ========================================================================
// Function: IsIPAddress
// Purpose: determines if a string contains a pure numerical IP address
// (returns true) or a DNS'able address (returns false)
// ========================================================================
bool IsIPAddress( const std::string p_address )
{
// scan through the string to see if it's a pure IP address or not.
// basically, we assume that any string with characters other than
// numerics and periods needs to be DNS'ed.
for( size_t i = 0; i < p_address.length(); i++ )
{
if( ( p_address[i] < '0' || p_address[i] > '9' ) &&
p_address[i] != '.' )
return false;
}
return true;
}
}
我已经粘贴了整个cpp文件,只是需要一个解决方案。任何帮助是值得赞赏的人。提前谢谢。
答案 0 :(得分:1)
试试这个:
ipaddress GetIPAddress( const std::string p_address )
{
if( IsIPAddress( p_address ) )
{
// if the address is just a regular IP address, there's no need
// to do a DNS lookup, so just convert the string directly into
// its binary format.
// if the address is invalid, throw a HOST_NOT_FOUND exception.
IN_ADDR addr;
if( inet_pton(AF_INET, p_address.c_str(), &addr) != 1 )
{
throw Exception( EDNSNotFound );
}
// by this point, the address is valid, so return it.
return addr.s_addr;
}
else
{
// the address isn't an IP address, so we need to look it up using DNS.
struct hostent* host = gethostbyname( p_address.c_str() );
// if there was an error, throw an exception.
if( !host )
{
// get the error from h_errno.
throw Exception( GetError( false ) );
}
// make sure it is IPv4
if( host->h_addrtype != AF_INET )
{
throw Exception( EDNSNotFound );
}
// now perform some really wierd casting tricks to get the value.
// h_addr is a char*, so cast it into an in_addr*, and
// dereference it to get the value.
return *((in_addr*)(host->h_addr));
}
}
话虽如此,你真的应该使用getaddrinfo()
代替inet_pton()
和gethostbyname()
:
ipaddress GetIPAddress( const std::string p_address )
{
// if the address is just a regular IP address, there's no need
// to do a DNS lookup, so just convert the string directly into
// its binary format.
addrinfo hints = {0};
hints.ai_flags = IsIPAddress( p_address ) ? AI_NUMERICHOST : 0;
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
// if the address is invalid, throw a HOST_NOT_FOUND exception.
addrinfo *addrs = NULL;
if( getaddrinfo( p_address.c_str(), NULL, &hints, &addrs ) != 0 )
{
throw Exception( EDNSNotFound );
}
// by this point, the address is valid, so return it.
// now perform some really wierd casting tricks to get the value.
// ai_addr is a sockaddr*, so cast it to an sockaddr_in*, and
// dereference it to get the value.
ipaddress addr = ((sockaddr_in*)(addrs->ai_addr))->sin_addr.s_addr;
freeaddrinfo(addrs);
return addr;
}
在getnameinfo()
gethostbyaddr()
中使用GetHostNameString()
代替inet_ntoa()
(GetIPString()
中可选择代替std::string GetHostNameString( ipaddress p_address )
{
char host[NI_MAXHOST+1] = {0};
struct sockaddr_in addr = {0};
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = p_address;
// get the host info.
// if there was an error, throw an exception.
if( getnameinfo( (struct sockaddr*)&addr, sizeof(addr), host, NI_MAXHOST, NULL, 0, 0 ) != 0 )
{
throw Exception( EDNSNotFound );
}
return std::string( host );
}
):
{{1}}