0

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)
saplingPro
  • 20,769
  • 53
  • 137
  • 195

2 Answers2

3

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,

Anders R. Bystrup
  • 15,729
  • 10
  • 59
  • 55
1

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

Try

InetAddress.getByName("192.168.43.187").isReachable(5000)
beny23
  • 34,390
  • 5
  • 82
  • 85
  • but i have to ping to a servlet that will handle everything. what can i do about this – saplingPro Dec 14 '12 at 11:17
  • 1
    That would be a HTTP request to that servlet, not a network ping. Try something like [Apache HTTP Client](http://hc.apache.org/httpcomponents-client-ga/index.html) to simplify making the HTTP request, but that would be a different question. – beny23 Dec 14 '12 at 11:21