1

I want to know the best way to identify if the internet is working or not. I was thinking of Ping, but maybe the target is down this will make my application think that the internet is down.

So if anyone got any idea, it would be great to share.

sikas
  • 5,435
  • 28
  • 75
  • 120
  • 4
    Pinging a very reliable host(ie: Google) might be a good way to go. – christopher Apr 10 '13 at 18:32
  • If I were to ping something, I'd ping Google's DNS servers by IP: 8.8.8.8 or 8.8.4.4 – Corey Ogburn Apr 10 '13 at 18:33
  • @ChrisCooney Not necessarily - many office environments block outgoing ICMP. – Dai Apr 10 '13 at 18:33
  • You should also do it by IP address to make sure it is the internet that is down, and not your current DNS resolver. On that note, you could ping google's open DNS resolvers: https://developers.google.com/speed/public-dns/docs/using – mellamokb Apr 10 '13 at 18:33
  • Probably the best alternative is to make an HTTP request to a very reliable host, like Google – Agustin Meriles Apr 10 '13 at 18:35
  • Ok, I saw it somewhere that there is a C# function that detects the internet .. does anyone know it? – sikas Apr 10 '13 at 18:41

2 Answers2

4

Rather than picking a host or DNS to ping, try pinging the web resource you intend to interact with. Or, rather than ping, actually try to interact with that endpoint and thoroughly prepare for a situation where that host cannot be reached.

I.E. a twitter client might try to connect to twitter.com and if it can't (whether they have no internet connection or twitter.com is blocked) the program could report "Twitter is currently unreachable".

Corey Ogburn
  • 24,072
  • 31
  • 113
  • 188
2

Ping a working DNS Like 4.2.2.4,4.2.2.3,4.2.2.2, 8.8.8.8, 8.8.4.4

Ping myPing = new Ping();
String host = "4.2.2.4;
byte[] buffer = new byte[32];
int timeout = 1000;
PingOptions pingOptions = new PingOptions();
PingReply reply = myPing.Send(host, timeout, buffer, pingOptions);
if (reply.Status == IPStatus.Success) {
  // presumably online
}
KF2
  • 9,887
  • 8
  • 44
  • 77