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);
}
}
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网络工具类实现,展示了如何获取本地主机的IP地址及指定服务器的IP地址。通过两个方法getMyIP()和getServerIP(),可以轻松地获取到相关信息。
281

被折叠的 条评论
为什么被折叠?



