• Post Reply Bookmark Topic Watch Topic
  • New Topic

Dead or alive?  RSS feed

 
Tien Shan
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!
I am trying to find if a server is alive or not using java:



It works ok but takes a long time (about 15 seconds).

Is there a better way or is there a way to make it faster? 4 or 5 seconds would be ideal.

Thanks.

PS: I removed "urlcon.connect();" part, but the time it took didn't change.
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could try InetAddress and its isReachable method. It's not guaranteed to work though. Alternatively, URLConnection itself has methods setConnectTimeout and setReadTimeout that you can use. Finally, you can specify the connect timeout for sockets, but you must create an unbound socket for that:
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tien Shan wrote:Hi!
I am trying to find if a server is alive or not using java:


1. Why? If you're going to check if it's "alive" before attempting to connect, that's pointless. Don't bother.

2. What, exactly does "alive" mean to you in the context of your answer to #1? There are many different possible answers, including, but not limited to: a) There exists a network path to that host address that is usable by me. b) There exists a network path to that host address and port that is usable by me. c) There is some service listening on a particular port on that address. d) There is a particular service listening on a particular port on that address. e) The host is powered on.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote:You could try InetAddress and its isReachable method. It's not guaranteed to work though.


It goes a little bit beyond "not guaranteed to work." It's more like "almost universally useless."
 
Tien Shan
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think I already have been helped with your comments. Thank you both.

Rob: I need an almost foolproof method to handle this, so I will stick with the URLConnection and use setConnectTimeOut, if that helps in my case (I am mentioning my requirements below.)

Jeff: Come on, don't be so hard on me
What I mean:
I have a service running at a server on a certain port.
(1) the host machine is powered off
(2) I can reach the server, and the port is open, but the service is not running
(3) it takes too much time to connect. this could be network traffic issues ( server and service may have been available if I were local, but I am always checking it from far away, different domain)

I think that covers my requirements.

I am designing a system whose task is to check if a service is available on a particular server. And if it takes a long time (and times out on me, if not up and running or says 'ok I am alive' after a long, long time), it is not acceptable.
I cannot wait more than *some commonly accepted value* seconds.

I don't know how to settle on this "some commonly accepted value". If you were to design Google's login system, for example, how much time would you set before you display a message like "server is not available now.. please try again later" kind of message? I wrote 4 seconds, but that may have been impractical because the software piece will run in south America, and it will check the availability of two servers; one in the north America, and the other in Europe.

So is Rob's second option still a safe bet?

Thanks again, and cheers!

 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you should only use URLConnection if the service is supported by java.net.URL. If it can be any kind of service using the Socket is better, as it has no dependencies - it can connect to any port on any machine, as long as the network allows this.
 
Tien Shan
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote:You should only use URLConnection if the service is supported by java.net.URL. If it can be any kind of service using the Socket is better, as it has no dependencies - it can connect to any port on any machine, as long as the network allows this.


Rob, I don't know if the service in question is supported. I will check (=google) the documentation. The service I will be using is called CAS, the Central Authentication Service. You may have heard about it or may have even used it.

Thanks.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tien Shan wrote:
Jeff: Come on, don't be so hard on me


I wasn't hard on you at all. Just pointing out a few things in the face of what looked like a very common beginner mistake.


What I mean:
I have a service running at a server on a certain port.
(1) the host machine is powered off
(2) I can reach the server, and the port is open, but the service is not running
(3) it takes too much time to connect. this could be network traffic issues ( server and service may have been available if I were local, but I am always checking it from far away, different domain)

I think that covers my requirements.


Still not really clear to me what you're saying. If you're talking about a general survey, either for reporting or for diagnosing a problem that has occurred, then, yes, various forms of checking if a host or service is "alive" can help provide pieces to the puzzle.

The only points I was trying to make are

1) This very common approach that drives many of these "How do I check if it's up?" questions, is utterly pointless:


2) There are many possible definitions of "whether a server is alive", some of which I listed above. Depending on which ones are relevant for your needs, you take different approaches to test them.

I am designing a system whose task is to check if a service is available on a particular server. And if it takes a long time (and times out on me, if not up and running or says 'ok I am alive' after a long, long time), it is not acceptable.
I cannot wait more than *some commonly accepted value* seconds.

I don't know how to settle on this "some commonly accepted value". If you were to design Google's login system, for example, how much time would you set before you display a message like "server is not available now.. please try again later" kind of message?


I still don't know why you want to check if this server is "alive". What will you do with the information once you have your answer?
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tien Shan wrote:Rob, I don't know if the service in question is supported. I will check (=google) the documentation. The service I will be using is called CAS, the Central Authentication Service. You may have heard about it or may have even used it.

A quick search for CAS protocol shows that it uses HTTP, so you can continue using URL and URLConnection.
 
Tien Shan
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob: Thank you for doing the digging for me. I implemented the URLConnection/setConnectTimeout, and it is working fine!

Jeff: I "inherited" a piece of software that checks remote servers, and if "alive", prods them for some information that we are interested in. But what was happening was when a server did not respond for any reason, the process would stop there, message bar displaying "connection to xyz.." and waiting, and waiting until the browser displayed "server timeout". This was not acceptable for us, because even if one is down, we still needed to check the remaining ones. that was (is) our requirement. I am sorry, I cannot be more specific than this.

Task accomplished-- let me close this thread.

Thank you both of you again, and cheers!

 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!