-4

Can you give me a simplest method to check the Internet availability using a C program in Linux?

I have implemented a program to run the system command 'traceroute' in C. Before this I have to check whether the Internet connection is available or not. So can you suggest a method?

  • 4
    "Doesn't work well" is like taking long coffee breaks or what? – Eugene Sh. Jun 20 '17 at 17:58
  • Is there any library functions? (I'm new to network programming) – Piyumi Guruge Jun 20 '17 at 18:03
  • Well, there is a big problem - the internet does not exist as one entity, so you cannot 100% reliably detect any 'connection ' to it. You can guess with some certainty - if you ping Google and it fails,.you have no internet connection..or Google is unreachable for some reason at that time..... – ThingyWotsit Jun 20 '17 at 19:34
  • 'Before this I have to check whether the Internet connection is available or not' - why? – ThingyWotsit Jun 20 '17 at 19:36
  • @ThingyWotsit : Thank you. Now I understand what's the path I have to follow. I'm trying with pinging google – Piyumi Guruge Jun 21 '17 at 20:42

1 Answers1

1

Short answer: no. Neither C nor C++ has direct knowledge of networking, or any built-in networking functions.

Depending on what 3rd-party libraries you are using, there may be something you can use, but that depends on the library, it isn't directly part of C.

I can imagine there are libraries out there that might try to ping some internal/external address, perform a name lookup, etc. But if you're on an isolated network, not having the ability to ping, lookup, or cross a firewall to get to a certain location may be perfectly valid, and doesn't indicate a networking problem.

Stéphane
  • 19,459
  • 24
  • 95
  • 136
  • Thank you. Can you suggest me a method to check the Internet availability? I'm asking about a simple c function or something like that.. – Piyumi Guruge Jun 20 '17 at 18:12
  • 3
    "Simple"? No. Depends on what is needed, and the network. Do you need to check connectivity to a server? Check if the name server is up? Check if the firewall is letting TCP or UDP packets through? Check if a port is open? There is no "simple" solution to this question. – Stéphane Jun 20 '17 at 18:15
  • Ok. I'm just running the 'traceroute' command using a C program. So, I have to check the Internet connection before run the system command. That's my problem. Please can you suggest a method to do this. – Piyumi Guruge Jun 20 '17 at 18:25
  • 'So, I have to check the Internet connection before run the system command' - why? Run it anyway. If it fails, deal with it. – ThingyWotsit Jun 20 '17 at 19:35