1. 程式人生 > >java中IP地址、主機名的獲取

java中IP地址、主機名的獲取

import java.net.InetAddress;
import java.net.UnknownHostException;
//public static InetAddress getByName(String host):根據主機名或者IP地址的字串表示得到IP地址物件。
/*根據IP地址或主機名*/
public class InetAddressDemo {

public static void main(String[] args) throws UnknownHostException {
// TODO Auto-generated method stub
//for(int i=0; i<256;i++){
InetAddress address = InetAddress.getByName("wymA");   //根據主機名得到IP地址物件
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.81.205
WYM-PC---169.254.188.97
Dell-PC---219.216.81.168
*/