LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Faster way to check if there is internet connection in C? (https://www.linuxquestions.org/questions/programming-9/faster-way-to-check-if-there-is-internet-connection-in-c-4175458115/)

Xeratul 04-14-2013 12:23 PM

Faster way to check if there is internet connection in C?
 
Hi,

I would like to check before doing some networking/backup stuffs that internet is available.
(c pure programming lang. (not in c++))

I could find some solution but arent adapted to Linux so much.
http://stackoverflow.com/questions/8...net-connection

Would you know the best way?

for instance, in bash, i use :
Code:

curl --silent --head http://www.google.com/  |egrep Found
libcurl is not portable much.

I would that it works as well under ms windows.

regards

mariostg 04-14-2013 02:27 PM

Something like this? http://en.wikibooks.org/wiki/C_Progr...orking_in_UNIX

The Beej's guide is also often cited as C network programming reference http://beej.us/guide/bgnet/

Xeratul 04-16-2013 01:02 PM

Quote:

Originally Posted by mariostg (Post 4931478)
Something like this? http://en.wikibooks.org/wiki/C_Progr...orking_in_UNIX

The Beej's guide is also often cited as C network programming reference http://beej.us/guide/bgnet/

the quite problem is that which likely not working with a regular limited portable (windows / Unix ) env : gcc -lncurses simple.c

netinet is not into pdcurses.dll

Code:

#include <arpa/inet.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>

It compiles well under debian.

I will try under windows... and let you know. lets cross fingers

dwhitney67 04-16-2013 04:41 PM

Quote:

Originally Posted by Xeratul (Post 4932621)
the quite problem is that which likely not working with a regular limited portable (windows / Unix ) env : gcc -lncurses simple.c

netinet is not into pdcurses.dll

Code:

#include <arpa/inet.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>

It compiles well under debian.

I will try under windows... and let you know. lets cross fingers

I despise Windows, but unfortunately I to have to develop software that works under it. For Windoze, you will need a different header file, which I believe is winsock2.h (perhaps winsock.h?):
Code:

#ifdef WIN32    // works for 32- and 64-bit Visual Studio
        #include <winsock2.h>
#else
        #include <arpa/inet.h>
        #include <sys/types.h>
        #include <netinet/in.h>
        #include <sys/socket.h>
#endif


Xeratul 04-17-2013 12:27 AM

Quote:

Originally Posted by dwhitney67 (Post 4932708)
I despise Windows, but unfortunately I to have to develop software that works under it. For Windoze, you will need a different header file, which I believe is winsock2.h (perhaps winsock.h?):
Code:

#ifdef WIN32    // works for 32- and 64-bit Visual Studio
        #include <winsock2.h>
#else
        #include <arpa/inet.h>
        #include <sys/types.h>
        #include <netinet/in.h>
        #include <sys/socket.h>
#endif



hopefully we can do it without winock windoze and io stuffs :)

lets ... see.

we keep in touch since we have same concern: portability

I have made several functions that work for both platfomr: nix and win. we can chat

mariostg 04-17-2013 07:39 PM

Quote:

Originally Posted by Xeratul (Post 4932621)
the quite problem is that which likely not working with a regular limited portable (windows / Unix ) env : gcc -lncurses simple.c

netinet is not into pdcurses.dll

Code:

#include <arpa/inet.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>

It compiles well under debian.

I will try under windows... and let you know. lets cross fingers

Maybe I don't get the point, but curses has nothing to do with network stuff. lncurses is unique to *nixes if not mistaken though.


All times are GMT -5. The time now is 01:33 PM.