tags:

views:

210

answers:

7

I need to detect the presence/absence of internet connection. More precisely, let us suppose that the application is broken up into 2 parts - A and B.

A is responsible for checking whether or not the system is connected to the internet. If it finds that there is no connection, it starts up part B. And as soon as it detects that there is a network connection, it kills B and continues its own work.

What would be the best way to do the A part of the application? Continual pings sounds hideous. There has to be a better way of doing this (preferably in C).

A: 

ping www.apple.com? (MSN messenger does that I think)

RC
msn pings apple.com?
Marius
I think, I blocked some ICMP ping to apple.com from messenger exe back in the day of v6..
RC
Many firewalls block ICMP. Even if it works today, you are at the mercy of a firewall policy change at Apple tomorrow.
bortzmeyer
A: 

ping google.com

Marius
Many firewalls (either on your side or on Google's side) block (stupidely) ICMP.
bortzmeyer
A: 

In your code, you have to check whether the internet connection exists by using a socket to open a connection to a website.

Santhanam
+2  A: 

With sufficient privilege you can test the various network interfaces and examine their state. This would tell you if any of the interfaces was connected to a network and operating. However, this won't tell you if the connection is actually usable, i.e., connected to the internet (or your local net if that's all you need). I don't know of anyway to do that short of actually using it.

Using ICMP (ping) can be useful at a low level, but presumably what you need is a connection to an actual endpoint via TCP/IP to do real work. I would say that you should change the design of your application so that B is responsible for indicating when it is unable to continue due to the absence of resources that it relies on -- network or otherwise. A and B should communicate so that A is aware of the situation and is able to either kill B or respond to B terminating itself and thus continuing its work.

tvanfosson
The thing is, that A needs to start B *as soon as* there is no connection. Otherwise the application will lose out on a lot of data. By connection, I mean hosts beyond my local network. So pinging continuously doesn't really sound good (at least to me).The /sbin/route reply given in the other thread makes sense. I'm not on Linux right now. I'll check it as soon as I boot and log into it.
Siddhant
I honestly think your best bet is to have some alternative endpoints similar to the one you're trying to connect to. There is no other relyable way of 'detecting there is an internet connection'. And the `/sbin/route` idea makes no sense: the only thing you would know is content of the local routing table, but nothing beyond, nor will it detect firewalls on the way.
Yannick M.
A: 
  1. Firstrun: Ask user to input the network parameters, like proxy settings. Save this info.

  2. Next runs: Use these settings to check for the Internet connection. You may simply do a DNS search.

  3. If results are negative, ask user to check settings.

Xolve
A: 

Check whether the cable is connected , if so ping your internet connection to any host as google.com.

Mercy Joseph
+1  A: 

A lot of companies have measures in place to prevent outgoing ICMP requests, TCP connections to ports other than 80/443 for example, or even to prevent you from reaching the internet directly by (transparently) proxying your traffic.

Under an internet connection I would understand any way to contact the outside, be it UDP, TCP or ICMP. Depending on what your application needs to contact the internet for, I would suggest to check over the same protocol, as that is the only thing that matters to your app.

If your application uses HTTP to communicate to an external source, try to connect to a few sites you would suspect to not be blacklisted and that have a reliable uptime. Like google.com, microsoft.com, apple.com, and so on...

Edit:

I am unsure what the specifics are, so let me give you an example with a hypothetical situation.

Application A collects data on the system it is running on and forwards it to a Web Service listening on yourserverhost.yourcompany.com:80 Application B would basically take over the job of the Web Service when it is down and log everything so no data is lost.

  • When all is well, App A will be sending the data to your web service
  • Once this connection drops, you immediatly launch App B (the obvious remark here would be, why not keep App B running as a failsafe)
  • App A connects to App B and forwards what it had been buffering
  • App A continues to try to reestablish the connection to your Web Service and once it is back up will request App B to stop

If the problem you are facing is nothing like this, please provide a more concrete description of what App A and App B are supposed to be doing. I will be more than happy to help.

Yannick M.
Grammar nazi time: albeit != be it. Albeit means "however" or "even if." You can remember this by its etymology: "Al(though) it be."
jcdyer
Fixed, and thanks :-)
Yannick M.