How can I get a hostname from an IPv4 address in C

2019-04-12 15:36发布

I need a simple function that outputs a char* hostname, given a char* IPAddress. Any ideas or hints?

Also, is easiest way to get the IP address from a hostname is gethostbyname() and inet_ntoa() is that correct?

3条回答
趁早两清
2楼-- · 2019-04-12 15:46
  1. To get the host name from an address, you normally use (big surprise here) gethostbyaddr().

  2. Yes, probably.

查看更多
Lonely孤独者°
3楼-- · 2019-04-12 15:55

Use gethostbyaddr() or getnameinfo() to convert an IP into a hostname.

Use gethostbyname() or getaddrinfo() to convert a hostname into an IP.

gethostby...() only support IPv4. get...info() support multiple protocols including both IPv4 and IPv6, and are the preferred functions for new development.

查看更多
神经病院院长
4楼-- · 2019-04-12 15:56

All the information you need on this is in the excellent Beej's Guide to Network Programming. The page relevant to your question is here.

If after reading that, you are still having problems, post here with a question about a specific issue.

查看更多
登录 后发表回答