简体   繁体   English

当我尝试使用网络IP对自己的PC进行ping操作时,可能会得到java.net.UnknownHostException的原因是什么?

[英]what could be the reason I am getting java.net.UnknownHostException, when I try to ping my own pc with a network IP?

I try to ping the server which is my own PC, with the following line of code : 我尝试使用以下代码来ping通属于我自己的PC的服务器:

InetAddress.getByName(serverResourceLocator).isReachable(5000)

// where serverResourceLocator is  192.168.43.187/server/ping?ip=Adarsh-PC/192.168.43.187&time=1355482205301

Here 192.168.43.187 is the network IP of my PC, that I came to know from the command ipconfig 192.168.43.187是我的PC的网络IP,我从命令ipconfig得知

Wireless LAN adapter Wireless Network Connection:

Connection-specific DNS Suffix  . :
Link-local IPv6 Address . . . . . : fe80::f5be:cfa7:5c38:efff%14
IPv4 Address. . . . . . . . . . . : 192.168.43.187
Subnet Mask . . . . . . . . . . . : 255.255.255.0
Default Gateway . . . . . . . . . : 192.168.43.1

On my PC i am running tomcat as the server. 在我的PC上,我正在运行tomcat作为服务器。 Why am I getting UnknownHostException ? 为什么我会收到UnknownHostException


java.net.UnknownHostException: 192.168.43.187/server/ping?ip=Adarsh-PC/192.168.43.187&time=1355482205301
at java.net.InetAddress.getAllByName0(InetAddress.java:1140)
at java.net.InetAddress.getAllByName0(InetAddress.java:1109)
at java.net.InetAddress.getAllByName(InetAddress.java:1072)
at java.net.InetAddress.getByName(InetAddress.java:969)
at internet.CommunicationWithServer.PingTheServer.ping(PingTheServer.java:35)
at internet.CommunicationWithServer.PingTheServer.access$000(PingTheServer.java:11)
at internet.CommunicationWithServer.PingTheServer$1.run(PingTheServer.java:21)
at java.lang.Thread.run(Thread.java:619)

The .getByName() method expects a hostname, not a (semi-)URL as you are supplying. .getByName()方法需要一个主机名,而不是您提供的(半)URL。 To quote from the JavaDoc: 引用JavaDoc:

The host name can either be a machine name , such as "java.sun.com", or a textual representation of its IP address . 主机名可以是机器名 ,例如“ java.sun.com”,也可以是其IP地址的文本表示。 If a literal IP address is supplied, only the validity of the address format is checked. 如果提供文字IP地址,则仅检查地址格式的有效性。

(My emphasis). (我的重点)。

Just try InetAddress.getByName( "192.168.43.187" ).isReachable(5000) and you'll be fine. 只需尝试InetAddress.getByName( "192.168.43.187" ).isReachable(5000) ,就可以了。

It occurs to me that you have written a servlet that will ping an IP. 在我看来,您已经编写了一个ping IP的servlet。 If it eg. 如果它例如。 returns the latency via the HTTP response, then you should use eg. 通过HTTP响应返回延迟,则应使用例如。 the HttpClient package to programmatically obtain that response; HttpClient程序包以编程方式获取该响应; there are several threads on SO on accomplishing that. SO上有多个线程可以完成此任务。

Cheers, 干杯,

serverResourceLocator is more like an URL, whereas InetAddress.getByName requires a hostname: serverResourceLocator更像一个URL,而InetAddress.getByName需要一个主机名:

Try 尝试

InetAddress.getByName("192.168.43.187").isReachable(5000)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM