1

Thanks for reading! The problem is I'm trying to check if there is internet connection in my app build in C++. I tried this method:

bool ICon(){
if (system("ping -c1 -s1 www.google.com"))
{return false;}else{return true;}
}

I linked the method to a listener of a pressed key, and that works, but I will like to bring the app to next level. I don't want to press a key to check if there is or there is not Internet connection. So the method should be running all time checking internet availability, and once is disconnected send me a "FALSE". Well with a while loop of the previous method, the apps collapse. Any other idea to check internet connection fast and continuously?

Thanks in advance!!

Qantas 94 Heavy
  • 15,750
  • 31
  • 68
  • 83
Pau Senabre
  • 4,155
  • 2
  • 27
  • 36
  • whether there is an internet connection is equivalent for you to whether you can ping google? I am not kidding, just want to understand what is your requirement – 4pie0 Sep 01 '13 at 14:40
  • What happens when www.google.com is down? – Neil Kirk Sep 01 '13 at 15:11
  • 1
    People start running through the streets, waving their hands in the air, and screaming. More or less that's what will happen :) – Pau Senabre Sep 01 '13 at 17:04

1 Answers1

0

Personally I think I would solve this in one (or a combination) of the following two approaches.

  1. Make a timer with a callback function that checks internet connectivity every x second.
  2. Simply check internet access before every communication block and raise an exception if no access is available at given time.

You might want to read a similar discussion for C#

How to query internet connection status in C# ?

Community
  • 1
  • 1
Rikard Söderström
  • 1,000
  • 6
  • 14
  • Well, I tried the timer, but makes everything super slow. And I tried the second option as well, but I'm sending data through sockets very often so check internet before every message is not an option, at least not with ping (takes ages to check google). I like the timer idea, but how can make the timer run in parallel, whitout the rest of the code waiting for it? – Pau Senabre Sep 01 '13 at 17:08
  • I am wondering, why you wish to check that internet is available? If it is crucial that the information shall reach the end-point, a TCP socket will do the trick. If its okay that a packet is lost UDP is your friend. – Rikard Söderström Sep 01 '13 at 18:06