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

岁酱吖の 提交于 2019-12-21 21:48:58

问题


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?


回答1:


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.




回答2:


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.




回答3:


  1. To get the host name from an address, you normally use (big surprise here) gethostbyaddr().

  2. Yes, probably.



来源:https://stackoverflow.com/questions/10082787/how-can-i-get-a-hostname-from-an-ipv4-address-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!