Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java code to retrieve response from IP mentioned../

 
prabhu pandurangan
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
prabhu pandurangan
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@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
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 147
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@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
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic