Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

testing to see if a server is up  RSS feed

 
Mark Stein
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I'm trying to write a program that basically tests if a server is up. Essentially if it is, the program just goes back to sleep, but if not, it logs it, and increases the frequency to determine how long it's down.
The only way I see that I can figure if the server isn't responding is to base it on an exception being thrown. But now I have two problems. I've used HttpURLConnection, and while it throws the exception, there's no way to shorten the timeout. So I tried using a socket, but either I can set a timeout (using SocketAddress, or I can set a port number. There doesn't seem to be a way to do both. If I use Socket(address, port), it automatically creates the connection... so how could I use setSoTimeout?
Also, I've been running my tests with a domain name that exists, but doesn't have a dns listing yet, but my socket still says it connects without throwing an error... how can that be?
Thanks.
Mark Stein
 
Mark Stein
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Never mind... I figured it out.
I manually opened a socket and set a time out value. Using buffered input and output readers, I then sent an http request, and read the first line of the response... If it times out before the response is recieved, I can assume the server is down.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!