import java.io.IOException;
import java.net.InetAddress;
public class InetAddressDemo {
public static void main(String[] args) throws IOException {
InetAddress locAdd = null; //声明InetAddress对象
InetAddress remAdd = null; //声明InetAddress对象
locAdd = InetAddress.getLocalHost(); //得到本地InetAddress对象
remAdd = InetAddress.getByName("www.baidu.com"); //得到远程InetAddress对象
InetAddress remAdd2 = InetAddress.getByName("192.168.121.200");
System.out.println("本机IP地址:"+locAdd.getHostAddress()); //得到本地IP地址
System.out.println("百度的IP地址"+remAdd.getHostAddress()); //得到远程IP地址
System.out.println("本机是否可达"+remAdd.isReachable(5000));
System.out.println("是否可达远程主机"+remAdd2.isReachable(5000));
}
}