获取特定主机的 IP 无法通过具有 Java 的网络工作
Get IP of a specific host is not working over a network with Java
我无法通过网络获取主机名的 IP。
我可以获得 public IP,但似乎无法通过网络工作,因为缺少协议:
public static void main(String[] args) throws UnknownHostException {
String url = "host22.my.network";
getIp(url);
}
public static void getIp(String url) throws UnknownHostException{
try {
InetAddress ip = InetAddress.getByName(new URL(url).getHost());
System.err.println(ip);
}
catch (MalformedURLException e) {
System.err.println(e.getMessage());
}
}
可能缺少协议前缀
由于@ejp 不想再实际回答问题,所以他是这样说的:
new URL(url).getHost()
是错误的。相反,使用
InetAddress ip = InetAddress.getByName(url)
并且由于您实际上并未传递 URL,因此将参数重命名为 hostname
。
我无法通过网络获取主机名的 IP。 我可以获得 public IP,但似乎无法通过网络工作,因为缺少协议:
public static void main(String[] args) throws UnknownHostException {
String url = "host22.my.network";
getIp(url);
}
public static void getIp(String url) throws UnknownHostException{
try {
InetAddress ip = InetAddress.getByName(new URL(url).getHost());
System.err.println(ip);
}
catch (MalformedURLException e) {
System.err.println(e.getMessage());
}
}
可能缺少协议前缀
由于@ejp 不想再实际回答问题,所以他是这样说的:
new URL(url).getHost()
是错误的。相反,使用
InetAddress ip = InetAddress.getByName(url)
并且由于您实际上并未传递 URL,因此将参数重命名为 hostname
。