如何根据IP地址获取主机名?

use*_*345 6 java network-programming

我想根据程序中给定的IP地址找到主机名.是否有可能得到它,如果是的话,请提供代码.谢谢.

小智 11

是的,它可能.

import java.net.*;
public class HostName
{
  public static void main(String args[])
  {
    InetAddress inetAddress =InetAddress.getByName("127.64.84.2");//get the host Inet using ip
    System.out.println ("Host Name: "+ inetAddress.getHostName());//display the host
  }
}
Run Code Online (Sandbox Code Playgroud)