简体   繁体   中英

WinSock2 getaddrinfo to resolve a hostname

I want to use getaddrinfo to resolve a hostname and obtain both IPv4 and IPv6 addresses. The documentation says that to do that the 'hints' parameter can be NULL. However, what should I do with the 'servicename' parameter? I just want the addresses and not associate it with any service/port. Can I leave that parameter NULL or an empty string? 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.

The documentation also says that:

One or both of the pNodeName or pServiceName parameters must point to a NULL-terminated ANSI string; generally both are provided.

This is a bit fuzzy but it seems that you can specify NULL for pServiceName (works for me).

The documentation also has a nice example program which shows you how to use getaddrinfo with both IPv4 and IPv6.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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