JAVA 中IP地址的获得

阅读更多
import java.net.*;

public class NetTool {

InetAddress myIPaddress = null;

InetAddress myServer = null;

public static void main(String args[]) {

NetTool mytool;
mytool = new NetTool();

System.out.println("Your host IP is: " + mytool.getMyIP());
System.out.println("The Server IP is :" + mytool.getServerIP("www.dsrcom.com"));

}

// 取得LOCALHOST的IP地址
public InetAddress getMyIP() {
try {
myIPaddress = InetAddress.getLocalHost();
} catch (UnknownHostException e) {
}
return (myIPaddress);
}

// 取得IP地址(指定)
public InetAddress getServerIP(String address) {
try {
myServer = InetAddress.getByName(address);
} catch (UnknownHostException e) {
}
return (myServer);
}

}

你可能感兴趣的:(Java,.net)