wood burning stoves 2.0*
The moose likes Java in General and the fly likes Java code to retrieve response from IP mentioned../ Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Java code to retrieve response from IP mentioned../" Watch "Java code to retrieve response from IP mentioned../" New topic
Author

Java code to retrieve response from IP mentioned../

prabhu pandurangan
Ranch Hand

Joined: May 23, 2008
Posts: 120
Dear Members,

I have to get the reponse of the machine using the IP address specified.

For example:

by using 'ping <Ip address>' we can retrieve the response of the particular machine's reponse if it is up or if it is shut down.

I need some java code to retrieve this response.

As it should return '1' for machine is up and '0' for machine is down.

Regards,
Prabhu.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41068
    
  43
Check out the java.net.InetAddress.isReachable method; it's pretty close to a ping.

What are you planning to use this information for? I'm asking because -generally- the more interesting information is whether there's a process listening on a particular port of some machine, than to know whether the machine itself is up.


Ping & DNS - my free Android networking tools app
prabhu pandurangan
Ranch Hand

Joined: May 23, 2008
Posts: 120
@Ulf

In my web application i have to retrieve some files from remote machine:

support.jsp:
-------------

<head>
<title></title>
<script type='text/javascript' src='https://192.xx.xx.xxx/engine.js'></script>
<script type='text/javascript' src='https://192.xx.xx.xxx/util.js'></script>
</head>

<body>

// my code comes here
</body>

This is my sample code. Here im retrieving javascript files from remote machine.
And if the specified site is down at the time, it takes more time to fetch the specified file. So to avoid this.

I need to check whether the site is up or down before trying to fetch the files.

As you said i tried using inetAddress.isReachable() it supports only for hostNames.

However it is useful for me. But i have to check using the URL, for ex: 'https://192.xx.xx.xxx/util.js'

Hope you could understand my issue.


Regards,
Prabhu.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41068
    
  43
So it is indeed like what I said - you're interested in whether the HTTP server is running, not whether the machine is up?

Judging by the "192.xxx" address this is a machine on the same network, or a network close by, so the connection should be pretty fast. That means you can set the timeout for failure pretty short (maybe 10 seconds, or even 5).
prabhu pandurangan
Ranch Hand

Joined: May 23, 2008
Posts: 120
@Ulf

Absolutely right Ulf. I need to know whether the HTTP server is running or not, not the machine.

This is on different network.

How to set the timeOut. If suppose the HTTP server responds slowly due to heavy load or slow network connectivity. How to handle this situation.

how to set the timeOut function in this case.


Do not hesitate if my questions are like baby step.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41068
    
  43
How to set the timeOut.

Check out the HTTPUrlConnection.setConnectTimeout and setReadTimeout methods.

If suppose the HTTP server responds slowly due to heavy load or slow network connectivity. How to handle this situation.

It's impossible to differentiate between a really slow server or an unreachable server in general. You could play around with different connection timeouts vs. read timeouts. I think if the server is just slow, you'd time out on the read, while if the server is not up at all, you'd time out on the connection.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java code to retrieve response from IP mentioned../
 
Similar Threads
Returning the users IP address
JBoss 5 IP Binding Automatic Detection
Read Client IP Address behind Proxy
how to check if a service is running on a port in a remote machine
Need To have show MAC address and IP Address In Web Application