How to determine the presence / absence of an Internet connection on a computer?

I need to detect the presence / absence of internet connection. More precisely, let's say that the application is split into 2 parts - A and B.

A is responsible for checking if the system is connected to the Internet. If it detects that there is no connection, it starts 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 make a part of the application? Continuous pings sound disgusting. There must be a better way to do this (preferably in C).

+2


source to share


6 answers


With sufficient privilege, you can test various network interfaces and check their status. This will tell you if any of the interfaces are connected to the network and up. However, that doesn't tell you if the connection is actually being used, i.e. Connected to the Internet (or your local network, if that's all you need). I don't even know how to do this if not using it.



Using ICMP (ping) can be useful at a low level, but apparently you need to connect to a real endpoint over TCP / IP to do the real work. I would say that you should redesign your application so that B is responsible for indicating when it cannot continue due to the lack of resources it relies on - network or otherwise. A and B must communicate so that A is aware of the situation and is able to either kill B or react to B, who completes himself and thus continues his work.

+2


source


Many companies implement measures to prevent outgoing ICMP requests, such as TCP connections on ports other than 80/443, or even to prevent direct Internet access (transparently) from proxying your traffic.

By Internet connection, I would understand any way to communicate with the external one, be it UDP, TCP or ICMP. Depending on what your application needs to communicate with the internet for, I would suggest testing the same protocol as that is the only thing that matters to your application.

If your application uses HTTP to communicate with an external source, try connecting to multiple sites that you suspect will not be blacklisted and have reliable uptime. Like google.com, microsoft.com, apple.com, etc.

Edit:



I'm not sure what the specifics are, so let me give you an example with a hypothetical situation.

Application A collects data about the system it is running on and forwards it to a web service listening on your .host.ppp.net server Application B will basically take over the web service when it is down and write everything down so that data is not lost.

  • When all is well, app A will send data to your web service.
  • As soon as that connection drops, you immediately launch application B (an obvious note here would be why not keep application B as fault tolerant).
  • Application A connects to Application B and forwards what was buffering.
  • App A keeps trying to reconnect to your web service, and after it is backed up, App B will stop.

If the problem you are having is no different from this, please provide a more specific description of what Apps A and App B should do. I'll be more than happy to help.

+1


source


In your code, you need to check if an internet connection exists using a socket in order to open a connection to the website.

0


source


  • Firstrun: Ask the user to enter network parameters such as proxy settings. Save this information.

  • Next Runs: Use these settings to test your internet connection. You can just do a DNS lookup.

  • If the results are negative, ask the user to check the settings.

0


source


Check if the cable is plugged in if needed to connect your internet connection to any host like google.com.

0


source


ping google.com

-2


source







All Articles