2013-03-27 12 views
14

Sto cercando di includere il seguente codice nel mio programma ma apparirà l'errore ('inet_pton': identificatore non trovato).'inet_pton': identificatore non trovato

// IPv4: 

struct sockaddr_in ip4addr; 
int s; 

ip4addr.sin_family = AF_INET; 
ip4addr.sin_port = htons(3490); 
inet_pton(AF_INET, "10.0.0.1", &ip4addr.sin_addr); 

s = socket(PF_INET, SOCK_STREAM, 0); 
bind(s, (struct sockaddr*)&ip4addr, sizeof ip4addr); 

uscita

error C3861: 'inet_pton': identifier not found 

l'intestazione compresa

#include <stdio.h> 
#include <stdlib.h> 
#include "udpDefine.h" 
#include <windows.h> 

qualsiasi porzione può perdere alcune intestazioni o lib.

+2

veloce ricerca su Google si trasforma fino #include Silas

+1

'#include ' –

+0

@ H2CO3 errore irreversibile C1083: Impossibile aprire il file di inclusione: 'ARPA/inet.h': No such file or directory. ho bisogno di installare intestazioni aggiuntive o cosa? –

risposta

21

la funzione

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

è dichiarata nel file di intestazione:

#include <arpa/inet.h> 

se questo è Windows (Vista o versioni successive) v'è Winsock analogico a questa versione ANSI:

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

prova #include <Ws2tcpip.h> aggiungi Ws2_32.lib

+0

ho incluso lo e ho modificato l'istruzione in InetPton (AF_INET, "192.168.10.9" e & address.sin_addr); e l'errore era (errore C3861: 'InetPton': identificatore non trovato) –

+1

@ abdo.eng2006210 aggiungi lib Ws2_32.lib – 4pie0

+0

ok, ma anche errore C3861: 'InetPton': identificatore non trovato. –

13

in Windows XP (e versioni successive) è possibile utilizzare queste funzioni:

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

Collegamento con biblioteca ws2_32.

+1

Il '+ 1' in' INET6_ADDRSTRLEN + 1' (o 'INET_ADDRSTRLEN + 1') non è necessario. I commenti nelle intestazioni Microsoft indicano che la definizione include il NULL di chiusura. – jww

-4

per risolvere questo problema è sufficiente aggiungere il seguente al codice dopo tutti #includes

#pragma comment(lib, "ws2_32.lib") 
0

Nel mio caso la funzione non è stata trovata, perché c'era un "WINVER 0x0502 #define" somewere in un file di intestazione.

Sui sistemi Windows la versione 0x600 (= Vista) è la meno richiesta per questa funzione.

Problemi correlati