'inet_pton': identifier not found

前端 未结 7 1103
长发绾君心
长发绾君心 2020-12-05 05:04

I\'m trying to include the following code in my program but the error (\'inet_pton\': identifier not found) will appeared.

// IPv4:

struct sockaddr_in ip4a         


        
相关标签:
7条回答
  • 2020-12-05 05:29

    the function

    int inet_pton(int af, const char *src, void *dst);
    

    is declared in header file:

    #include <arpa/inet.h>
    

    if this is Windows (Vista or later) there is Winsock analog to this ANSI version:

    INT WSAAPI InetPton(
      _In_   INT  Family,
      _In_   PCTSTR pszAddrString,
      _Out_  PVOID pAddrBuf
    );
    

    try #include <Ws2tcpip.h> add Ws2_32.lib

    0 讨论(0)
  • 2020-12-05 05:29

    To fix this problem just add the following to your code after all #includes

    #pragma comment(lib, "ws2_32.lib")
    
    0 讨论(0)
  • 2020-12-05 05:30

    In windows XP (and later) you can use these functions:

    #include <stdlib.h>
    #include <string.h>
    #include <stdio.h>    
    
    #include <winsock2.h>
    #include <ws2tcpip.h>
    
    
    int inet_pton(int af, const char *src, void *dst)
    {
      struct sockaddr_storage ss;
      int size = sizeof(ss);
      char src_copy[INET6_ADDRSTRLEN+1];
    
      ZeroMemory(&ss, sizeof(ss));
      /* stupid non-const API */
      strncpy (src_copy, src, INET6_ADDRSTRLEN+1);
      src_copy[INET6_ADDRSTRLEN] = 0;
    
      if (WSAStringToAddress(src_copy, af, NULL, (struct sockaddr *)&ss, &size) == 0) {
        switch(af) {
          case AF_INET:
        *(struct in_addr *)dst = ((struct sockaddr_in *)&ss)->sin_addr;
        return 1;
          case AF_INET6:
        *(struct in6_addr *)dst = ((struct sockaddr_in6 *)&ss)->sin6_addr;
        return 1;
        }
      }
      return 0;
    }
    
    const char *inet_ntop(int af, const void *src, char *dst, socklen_t size)
    {
      struct sockaddr_storage ss;
      unsigned long s = size;
    
      ZeroMemory(&ss, sizeof(ss));
      ss.ss_family = af;
    
      switch(af) {
        case AF_INET:
          ((struct sockaddr_in *)&ss)->sin_addr = *(struct in_addr *)src;
          break;
        case AF_INET6:
          ((struct sockaddr_in6 *)&ss)->sin6_addr = *(struct in6_addr *)src;
          break;
        default:
          return NULL;
      }
      /* cannot direclty use &size because of strict aliasing rules */
      return (WSAAddressToString((struct sockaddr *)&ss, sizeof(ss), NULL, dst, &s) == 0)?
              dst : NULL;
    }
    

    Link with ws2_32 library.

    0 讨论(0)
  • 2020-12-05 05:38

    In my case the function couldn't be found, because there was a "#define WINVER 0x0502" somewere in a header file.

    On Windows systems the version 0x600 (= Vista) is as least required for this function.

    0 讨论(0)
  • 2020-12-05 05:40

    Windows users can also find the answer here:

    https://docs.microsoft.com/en-us/windows/desktop/api/ws2tcpip/nf-ws2tcpip-inetptonw

    The InetPton function converts an IPv4 or IPv6 Internet network address in its standard text presentation form into its numeric binary form. The ANSI version of this function is inet_pton.

    The InetPton function is supported on Windows Vista and later.

    When UNICODE or _UNICODE is not defined, InetPton is defined to InetPtonA, the ANSI version of this function. The ANSI version of this function is always defined as inet_pton.

    Header      ws2tcpip.h
    Library     Ws2_32.lib
    DLL         Ws2_32.dll
    

    You need to use the ws2tcpip.h header and add Ws2_32.lib to your linker.

    0 讨论(0)
  • 2020-12-05 05:40

    Decalare this function Explicitly in your code.

    int inet_pton(int af, const char *src, void *dst)
    {
      struct sockaddr_storage ss;
      int size = sizeof(ss);
      char src_copy[INET6_ADDRSTRLEN+1];
    
      ZeroMemory(&ss, sizeof(ss));
      /* stupid non-const API */
      strncpy (src_copy, src, INET6_ADDRSTRLEN+1);
      src_copy[INET6_ADDRSTRLEN] = 0;
    
      if (WSAStringToAddress(src_copy, af, NULL, (struct sockaddr *)&ss, &size) == 0) {
        switch(af) {
          case AF_INET:
        *(struct in_addr *)dst = ((struct sockaddr_in *)&ss)->sin_addr;
        return 1;
          case AF_INET6:
        *(struct in6_addr *)dst = ((struct sockaddr_in6 *)&ss)->sin6_addr;
        return 1;
        }
      }
      return 0;
    } 
    
    0 讨论(0)
提交回复
热议问题