代码如下:
package com.sxt.ip;
import java.net.InetAddress;
import java.net.UnknownHostException;
public class IPtest {
public static void main(String args[]) throws UnknownHostException {
InetAddress addr=InetAddress.getLocalHost();
System.out.println(addr.getHostAddress());
System.out.println(addr.getHostName());
addr=InetAddress.getByName("www.baidu.com");
System.out.println(addr.getHostAddress());
System.out.println(addr.getHostName());
addr=InetAddress.getByName("202.193.80.124");
System.out.println(addr.getHostAddress());
System.out.println(addr.getHostName());
}
}
运行截图: