简体   繁体   English

WinSock2 getaddrinfo解析主机名

[英]WinSock2 getaddrinfo to resolve a hostname

I want to use getaddrinfo to resolve a hostname and obtain both IPv4 and IPv6 addresses. 我想使用getaddrinfo解析主机名并获取IPv4和IPv6地址。 The documentation says that to do that the 'hints' parameter can be NULL. 该文档说,要做到这一点,“提示”参数可以为NULL。 However, what should I do with the 'servicename' parameter? 但是,我应该如何使用'servicename'参数? I just want the addresses and not associate it with any service/port. 我只需要地址,而不将其与任何服务/端口关联。 Can I leave that parameter NULL or an empty string? 我可以将该参数保留为NULL还是空字符串吗? It works, but is it right to do so? 它有效,但是这样做正确吗?

The gethostbyname function seems to be just for that task only, but according to the documentation it is deprecated and getaddrinfo should be used instead. gethostbyname函数似乎仅用于该任务,但是根据文档,不建议使用它,而应使用getaddrinfo。

The documentation also says that: 文档还说:

One or both of the pNodeName or pServiceName parameters must point to a NULL-terminated ANSI string; pNodeName或pServiceName参数中的一个或两个必须指向以NULL结尾的ANSI字符串。 generally both are provided. 通常都提供。

This is a bit fuzzy but it seems that you can specify NULL for pServiceName (works for me). 这有点模糊,但似乎可以为pServiceName指定NULL(对我有用)。

The documentation also has a nice example program which shows you how to use getaddrinfo with both IPv4 and IPv6. 文档还有一个不错的示例程序,向您展示了如何将getaddrinfo与IPv4和IPv6结合使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM