简体   繁体   中英

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 :

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

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. Why am I getting 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. To quote from the JavaDoc:

The host name can either be a machine name , such as "java.sun.com", or a textual representation of its IP address . If a literal IP address is supplied, only the validity of the address format is checked.

(My emphasis).

Just try InetAddress.getByName( "192.168.43.187" ).isReachable(5000) and you'll be fine.

It occurs to me that you have written a servlet that will ping an IP. If it eg. returns the latency via the HTTP response, then you should use eg. the HttpClient package to programmatically obtain that response; there are several threads on SO on accomplishing that.

Cheers,

serverResourceLocator is more like an URL, whereas InetAddress.getByName requires a hostname:

Try

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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