2007年7月25日 星期三

取得網卡的IP

利用NetworkInterfacegetByName函式來選擇網路卡介面,接著便可以取得設定於此網卡上的所有IP。

try {
NetworkInterface iface = NetworkInterface.getByName("eth1");
Enumeration e = iface.getInetAddresses();
while(e.hasMoreElements()) {
InetAddress ip = (InetAddress)e.nextElement();
System.out.println(ip);
}
}
catch(Exception ex) {
ex.printStackTrace();
}

0 意見: