tags:

views:

1759

answers:

6

Hello,

C# 2008

I am using this code to test for an internet connection. As my application will have to login to a web server. However, if the user internet connection was to fail or cable pulled out. I will have to notify the user.

 // Ping www.google.com to check if the user has a internet connection.
 public bool PingTest()
 {
  Ping ping = new Ping();

  PingReply pingStatus = ping.Send(IPAddress.Parse("208.69.34.231"));

  if (pingStatus.Status == IPStatus.Success)
  {
   return true;
  }
  else
  {
   return false;
  }
 }

The only way I think I can test for an Internet connection is to ping www.google.com. So I have a used a server timer which I have set for 500 milliseconds and in the lapsed event it will call this ping function. If the ping returns false. Then my app will take appropriate action.

Do you think using google as a way to test an Internet connect is a good thing. If google was to fail, then my app would not function. Is polling 1/2 second to much or too little? Just wondering about my whole idea if it is good or not?

Many thanks,

+1  A: 

It'd suggest to put more than 0.5s, because sometimes your lataency can get higher. I sometimes have even 3.5s on DSL.

Darth
+11  A: 

Why ping Google? The only server you really care about is the web server you want to talk to, right? So ping that instead. That way, you can notify the user if anything is wrong between them and the web server - that's the only thing that matters.

If something goes wrong with the internet which means that the user can only do anything within their own town, you may not be able to reach Google but if you can still reach the web server you're interested in, why report a problem? On the other hand, if the web server goes down but Google doesn't, why would you not want to tell the user that there's a problem?

Likewise, why use a ping? Why not send an HTTP request to a known URL on the web server? After all, if the network is up but the web server process itself is down, surely that's just as bad as the user's network going down.

EDIT: I hadn't noticed in the question that you were repeating this every half second. That's really not pleasant for anyone. Surely once a minute is often enough, isn't it?

Jon Skeet
Maybe if the ping to the server fails, ping Google. That way he could tell if the user was on the 'net, but the server is down.
Jonathan C Dickinson
For security reasons the ping (icmp) has been disabled on the web server. I did create a socket to test the web server on a port number. However, it was very slow to respond.
robUK
Which port though? The only one you care about is the one listening for web requests, surely.
Jon Skeet
+2  A: 

I might be missing something but, Why do you need to test that the server is up and the web service running before you need to use it? If you call a method in your web services and you don't get a timely response, then take what ever action you see fit or need?

It just seems over engineering of the design.

[edit]

Thanks for the clarification robUK. You should listen to NET Rocks podcast episode 'Udi Dahan Scales Web Applications!' (date 12/08/2008). They discuss adding a GUID to a request and wait until you get the same GUID in the response. Maybe you could fire it off every second for say 10 attempts and if you don't get a response back (with the GUID attached) after 10 attempts, then take the required action?

Good luck with it..

Skittles
Hello. The app need to monitor all the time incase something happens and my app as to take appropriate action. I have tested pinging google. But I don't think this is a good idea now. As if the ping fails only once and remember I am pinging every few seconds. Then the app would think that the Internet connection has been lost and take action.
robUK
+2  A: 

I have an app that needs to do something similar. We have a bit of a different architecture in place, namely a pub/sub message bus. For those applications interested in hearing "heartbeats" from the various services, we allow them to register their interest with the bus. Every few second or so, each service will publish a health state message that consumers will then receive. If the consumers do not receive a health state message for a certain amount of time, they can take appropriate action until they hear it again.

This setup is much more scalable than polling the server or even worse pinging it. Imagine if you had 100 or 1000 clients all polling or pinging your sever every few seconds. Bad.

JP Alioto
Sounds like a good solution, Was it difficult to implement?
Skittles
Not really ... if you grab brother Juval's sample code (http://msdn.microsoft.com/en-us/magazine/cc163537.aspx) you can have a mock-up of the plumbing running quite quickly. The services we run OTOH are uber complex. :)
JP Alioto
Yes, you are correct. Having all those client pinging a single web site is a bad idea. However, I have never done this before so looking for different ways to do this. I have just quickly scanned the article. Am I correct in thinking that the client will subscribe to the service. If the web server goes down the service will callback to the client to notify them. Is there any other articles on this? I guess duplex would be if the client goes of line then a callback could be sent to the service.
robUK
Absolutely. There's tons of great stuff on ESB, http://esb.codeplex.com/ for example. Of course, you are not implementing the entire ESB pattern. But some of the concepts that are part of an ESB are what you are trying to do.
JP Alioto
+3  A: 

Check out this duplicate question which has a good answer that doesn't require ping:

C# - How do I check for a network connection

You could subscribe to the System.Net.NetworkInformation.NetworkChange.NetworkAvailabilityChanged event and only then when it indicates that the network is down do a ping/web request to the server to see if it's available.

Paul Alexander
+1  A: 

Better to see if you are on the network than to bother a server pinging it. See http://stackoverflow.com/questions/2677702/detect-internet-v-local-lan-connection for example. If you are on Windows 7 / Server 2008 R2 you can get an event when connectivity changes, which is going to be much happier for everyone than constant pinging and polling.

Kate Gregory