Linux “inet_pton”:未找到标识符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15660203/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
'inet_pton': identifier not found
提问by abdo.eng 2006210
I'm trying to include the following code in my program but the error ('inet_pton': identifier not found) will appeared.
我试图在我的程序中包含以下代码,但会出现错误 ('inet_pton': identifier not found)。
// 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);
Output
输出
error C3861: 'inet_pton': identifier not found
the including header
包含标题
#include <stdio.h>
#include <stdlib.h>
#include "udpDefine.h"
#include <windows.h>
any helping may be missed some headers or lib.
任何帮助都可能会错过一些标题或库。
采纳答案by 4pie0
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:
如果这是 Windows(Vista 或更高版本),则有与此 ANSI 版本类似的 Winsock:
INT WSAAPI InetPton(
_In_ INT Family,
_In_ PCTSTR pszAddrString,
_Out_ PVOID pAddrBuf
);
try #include <Ws2tcpip.h>
add Ws2_32.lib
尝试#include <Ws2tcpip.h>
添加 Ws2_32.lib
回答by Petar Korponai?
In windows XP (and later) you can use these functions:
在 Windows XP(及更高版本)中,您可以使用以下功能:
#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.
与 ws2_32 库链接。
回答by Sid
To fix this problem just add the following to your code after all #includes
要解决此问题,只需在所有 #includes 之后将以下内容添加到您的代码中
#pragma comment(lib, "ws2_32.lib")
回答by David Gausmann
In my case the function couldn't be found, because there was a "#define WINVER 0x0502" somewere in a header file.
在我的情况下,找不到该函数,因为头文件中有“#define WINVER 0x0502”。
On Windows systems the version 0x600 (= Vista) is as least required for this function.
在 Windows 系统上,此功能至少需要版本 0x600 (= Vista)。
回答by Michal M.
Windows users can also find the answer here:
Windows 用户也可以在这里找到答案:
https://docs.microsoft.com/en-us/windows/desktop/api/ws2tcpip/nf-ws2tcpip-inetptonw
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 isinet_pton
.
该
InetPton
函数将标准文本表示形式的 IPv4 或 IPv6 Internet 网络地址转换为其数字二进制形式。此函数的 ANSI 版本是inet_pton
.
The
InetPton
function is supported on Windows Vista and later.
InetPton
Windows Vista 及更高版本支持该功能。
When
UNICODE
or_UNICODE
is not defined,InetPton
is defined toInetPtonA
, the ANSI version of this function. The ANSI version of this function is always defined asinet_pton
.
当
UNICODE
或_UNICODE
未定义时,InetPton
定义为InetPtonA
,此函数的 ANSI 版本。此函数的 ANSI 版本始终定义为inet_pton
.
Header ws2tcpip.h Library Ws2_32.lib DLL Ws2_32.dll
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.
您需要使用ws2tcpip.h
标头并添加Ws2_32.lib
到链接器中。
回答by UFO
In WS2tcpip.h, this api inet_pton is defined:
在 WS2tcpip.h 中,定义了这个 api inet_pton:
#if (NTDDI_VERSION >= NTDDI_VISTA)
WINSOCK_API_LINKAGE
INT
WSAAPI
inet_pton(
_In_ INT Family,
_In_ PCSTR pszAddrString,
_When_(Family == AF_INET, _Out_writes_bytes_(sizeof(IN_ADDR)))
_When_(Family == AF_INET6, _Out_writes_bytes_(sizeof(IN6_ADDR)))
PVOID pAddrBuf
);
回答by Arpit Jain
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;
}