2014-10-16 107 views
-1

嘿傢伙喜歡標題說,即時嘗試連接到遠程登錄時在我的服務器中出現錯誤。即時得到這個錯誤:錯誤在C++設置服務器用於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文件只是櫃面它需要一個解決方案。任何幫助,讚賞球員。提前致謝。

+2

「但即時通訊有問題,將其集成到我的代碼中。」你試過了什麼,你有什麼問題? – 2014-10-17 00:03:22

+0

使用我們添加的inet_pton函數(AF_NET,p_address.c_str()),我不完全確定我的講師放入緩衝區,但p_address發出了一個類似的錯誤,說使用其他的東西 – Pendo826 2014-10-17 00:07:16

+0

_「但是p_address發出了一個類似的錯誤,使用別的東西「_如果你想得到免費的幫助,你必須要比這更精確和科學! – 2014-10-17 00:17:07

回答

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); 
}