C++ 相当于 inet_aton 的 windows
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2420663/
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
windows equivalent of inet_aton
提问by SSS
I'm converting some code written for a linux system to a windows system. I'm using C++ for my windows system and wanted to know the equivalent of the function inet_aton.
我正在将一些为 linux 系统编写的代码转换为 windows 系统。我在我的 Windows 系统中使用 C++,并想知道函数 inet_aton 的等价物。
回答by Steve Jessop
It's the Windows equivalent rather than the C++ equivalent, but probably you want inet_addr
, which I believe predates inet_aton
and which Windows supports.
它是 Windows 的等价物,而不是 C++ 的等价物,但可能你想要inet_addr
,我相信它早于inet_aton
Windows 支持。
http://msdn.microsoft.com/en-us/library/ms738563.aspx
http://msdn.microsoft.com/en-us/library/ms738563.aspx
That article also lists, in the "see also" section, the full set of verbosely-named functions to handle IPv6 addresses and so on.
该文章还在“另见”部分列出了处理 IPv6 地址等的完整的详细命名函数集。
回答by caf
Windows supports inet_pton
, which has a similar interface to inet_aton
(but that works with IPV6 addresses too). Just supply AF_INET
as the first parameter, and it will otherwise work like inet_aton
.
Windows 支持inet_pton
,它具有与inet_aton
(但也适用于 IPV6 地址)类似的界面。只需AF_INET
作为第一个参数提供,否则它将像inet_aton
.
(If you can change the Linux source, inet_pton
will also work there).
(如果可以更改Linux 源,inet_pton
也可以在那里工作)。
回答by benziv
To run in windows XP, you can try this checking:
要在 Windows XP 中运行,您可以尝试以下检查:
#pragma comment(lib, "Ws2_32.lib")
sockaddr_in inaddr;
#ifdef _WIN32_WINNT 0x0501
inaddr.sin_addr.s_addr =inet_addr("10.10.10.10"); //for XP
#else
inet_pton(AF_INET, "10.10.10.10", &inaddr.sin_addr.s_addr); //for Vista or higher
#endif