根据主机名获取IP地址

import java.net.InetAddress;
import java.net.UnknownHostException;


/*根据主机名获取IP地址*/
public class InetAddressDemo {


public static void main(String[] args) throws UnknownHostException {
// TODO Auto-generated method stub


InetAddress address = InetAddress.getByName("wymA");
String name = address.getHostName();
String ip = address.getHostAddress();
System.out.println(name+"---"+ip);

InetAddress address1 = InetAddress.getByName("WYM-PC");
String name1 = address1.getHostName();
String ip1 = address1.getHostAddress();
System.out.println(name1+"---"+ip1);

InetAddress address2 = InetAddress.getByName("Dell-PC");
String name2 = address2.getHostName();
String ip2 = address2.getHostAddress();
System.out.println(name2+"---"+ip2);
}


}




/*
运行结果:
wymA---219.216.102.182
WYM-PC---169.254.188.97
Dell-PC---219.216.102.101
*/
相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页