import java.net.InetAddress;
import java.net.UnknownHostException;
public class IpNameConvert {
InetAddress myIPaddress = null;
InetAddress myServer = null;
public static void main(String args[]) {
IpNameConvert mytool;
mytool = new IpNameConvert();
System.out.println("Your host IP is: " + mytool.getMyIP());
System.out.println("The Server IP is :" + mytool.getServerIP());
}
// 取得LOCALHOST的IP地址
public InetAddress getMyIP() {
try {
myIPaddress = InetAddress.getLocalHost();
} catch (UnknownHostException e) {
}
return (myIPaddress);
}
// 取得 www.abc.com 的IP地址
public InetAddress getServerIP() {
try {
myServer = InetAddress.getByName("www.baidu.com");
} catch (UnknownHostException e) {
}
return (myServer);
}
}
System.out.println(myServer.getHostName());
System.out.println(myServer.getHostAddress());
本文提供了一个使用Java实现的简单示例,演示了如何获取本地主机和指定服务器(如www.baidu.com)的IP地址。该示例通过InetAddress类的getLocalHost()方法来获取本地IP,并使用getByName()方法获取远程服务器的IP。
9000

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



