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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 17:22:55  来源:igfitidea点击:

Alternative to gethostbyname

c++windowsgethostbyname

提问by Saustin

I can't use gethostbynameto 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 getaddrinfobut that doesn't seem like what I want.)

我在其他方法上找不到任何足够的资源来使用其他功能查找主机的 IP 地址(推荐使用 MSDN,getaddrinfo但这似乎不是我想要的。)

回答by

Actually, getaddrinfois 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 getaddrinfoactually iswhat you want, here's simple example.

你可以尝试类似的东西WSAConnectByName,但getaddrinfo实际上你想要的,这是一个简单的例子

回答by Baron Leonardo

You can use gethostbyname2which supports both IPV4 and IPV6 resolving.
This is an articlethat shows the difference between gethostbynameand getaddrinfo, it also discusses gethostbyname2.

您可以使用gethostbyname2支持 IPV4 和 IPV6 解析的哪个。
这是一个文章,显示的区别gethostbynamegetaddrinfo,它还讨论gethostbyname2