Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if a server is available

Tags:

c#

I'm looking for a way to check if a server is still available. We have a offline application that saves data on the server, but if the serverconnection drops (it happens occasionally), we have to save the data to a local database instead of the online database. So we need a continues check to see if the server is still available.

We are using C# for this application

The check on the sqlconnection.open is not really an option because this takes about 20 sec before an error is thrown, we can't wait this long + I'm using some http services as well.

like image 527
user29964 Avatar asked Mar 05 '09 11:03

user29964


People also ask

How do you check if a server is reachable or not?

Answer. Ping is a network utility that is used to test if a host is reachable over a network or over the Internet by using the Internet Control Message Protocol “ICMP”. When you initiate an ICMP request will be sent from a source to a destination host.

How do you check server is reachable or not in Linux?

The ping command is a simple network utility command-line tool in Linux. It's a handy tool for quickly checking a host's network connectivity. It works by sending an ICMP message ECHO_REQUEST to the target host. If the host reply with ECHO_REPLY, then we can safely conclude that the host is available.


2 Answers

Just use the System.Net.NetworkInformation.Ping class. If your server does not respond to ping (for some reason you decided to block ICMP Echo request) you'll have to invent your own service for this. Personally, I'm all for not blocking ICMP Echo requests, and I think this is the way to go. The ping command has been used for ages to check reachability of hosts.

using System.Net.NetworkInformation;
var ping = new Ping();
var reply = ping.Send("google.com", 60 * 1000); // 1 minute time out (in ms)
// or...
reply = ping.Send(new IPAddress(new byte[]{127,0,0,1}), 3000);
like image 63
John Leidegren Avatar answered Oct 04 '22 20:10

John Leidegren


From your question it appears the purpose of connecting to the server is to use its database. Your priority must be to check whether you can successfully connect to the database. It doesn't matter if you can PING the server or get an HTTP response (as suggested in other answers), your process will fail unless you successfully establish a connection to the database. You mention that checking a database connection takes too long, why don't you just change the Connection Timeout setting in your application's connection string to a more impatient value such as 5 seconds (Connection Timeout=5)?

like image 27
Chris Driver Avatar answered Oct 04 '22 21:10

Chris Driver