-   Programming (
-   -   C - checking internet connection (

szparag 10-27-2004 01:40 PM

C - checking internet connection

how can i check if the internet connection is established using C programming language ???

i know one way to do it using system ping function. this method is simple:

system("ping -c1 > ping.tmp");
and after one PING i can check file with some text returning by ping command and using simple compare: if the file ping.tmp is empty connection isn't established but if file contains some text i can "guess" that connection works correctly...

but it isn't good way to check internet connection.

using PHP and fopen function i could open network file (for example: and if connection is correct (and network file exists) function return true...

what should i do to simple check connection using C programming language ??

itsme86 10-27-2004 02:02 PM

You can't. It's impossible. Have you ever seen another program determine 100% whether or not you have a working internet connection? The reason the answer is "no" should tell you something.

The only thing you can check for sure is whether you have a working route between 2 particular hosts.

szparag 10-27-2004 02:54 PM

testing connection between two hosts is that what i'm looking for.
but i want to find another way than using system ping command and checking returned value...

i don't know how to use it but i think that mayby "socket" should solve my problem. or maybe there is another (simple) way to test connection between local computer and host in the network...

szparag 10-27-2004 03:59 PM

i found good way to check connection:


        char *hostname; 
        struct hostent *hostinfo;

        hostname = "";
        hostinfo = gethostbyname (hostname);

        if (hostinfo == NULL) g_print("-> no connection!\n");
        else g_print("-> connection established!\n");

itsme86 10-27-2004 04:07 PM

It will grab that info from DNS cache if available, so it might not be the best test. You're better off finding a site that doesn't drop ICMP packets and do your own "ping" to it.

All times are GMT -5. The time now is 07:21 PM.