windows gethostbyname 的替代方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6638580/
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
Alternative to gethostbyname
提问by Saustin
I can't use gethostbyname
to grab a host's IP address, it is a deprecated function that only works 10% of the fricken' time on Windows!
我不能gethostbyname
用来获取主机的 IP 地址,这是一个已弃用的功能,在 Windows 上只能运行 10% 的时间!
I can't find any adequate resources on other ways to find a host's IP address using other functions (MSDN recommended getaddrinfo
but that doesn't seem like what I want.)
我在其他方法上找不到任何足够的资源来使用其他功能查找主机的 IP 地址(推荐使用 MSDN,getaddrinfo
但这似乎不是我想要的。)
回答by
Actually, getaddrinfo
is what you want. It's just a long-winded way to do it, not to mention the fact that sometimes multiple IP addresses are found (e.g. one IPv4 address andone IPv6 address). I'd recommend looking at Beej's Guide to Network Programming, specifically getaddrinfo
.
其实,getaddrinfo
就是你想要的。这只是一种冗长的方法,更不用说有时会发现多个 IP 地址(例如一个 IPv4 地址和一个 IPv6 地址)这一事实。我建议查看Beej 的网络编程指南,特别是getaddrinfo
.
回答by Nikolai Fetissov
You can try things like WSAConnectByName
, but getaddrinfo
actually iswhat you want, here's simple example.
你可以尝试类似的东西WSAConnectByName
,但getaddrinfo
实际上是你想要的,这是一个简单的例子。