LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 10-27-2004, 12:40 PM   #1
szparag
LQ Newbie
 
Registered: Oct 2004
Posts: 5

Rep: Reputation: 0
C - checking internet connection


hi.

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:
Code:
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: some_site.com/index.php) and if connection is correct (and network file exists) function return true...

what should i do to simple check connection using C programming language ??
 
Old 10-27-2004, 01:02 PM   #2
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 58
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.
 
Old 10-27-2004, 01:54 PM   #3
szparag
LQ Newbie
 
Registered: Oct 2004
Posts: 5

Original Poster
Rep: Reputation: 0
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...
 
Old 10-27-2004, 02:59 PM   #4
szparag
LQ Newbie
 
Registered: Oct 2004
Posts: 5

Original Poster
Rep: Reputation: 0
i found good way to check connection:

Code:
	char *hostname;   
	struct hostent *hostinfo;

	hostname = "google.com";
	hostinfo = gethostbyname (hostname);

	if (hostinfo == NULL) g_print("-> no connection!\n");
	else g_print("-> connection established!\n");
 
Old 10-27-2004, 03:07 PM   #5
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 58
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Slow Internet Connection when Connecting to Internet on Browsers lennysokol Linux - Networking 8 04-09-2011 04:29 PM
Nic card -> cable internet modem no internet connection zeshan_b Mandriva 21 09-26-2004 11:48 AM
Messed up my internet settings (NO internet connection) :( Almazick Linux - Networking 2 09-08-2004 12:05 PM
i can get internet connection in windows but in LINUX there is no connection ruzvay Linux - Networking 2 06-08-2004 07:02 PM
Checking if you are on the internet RedRabbit Linux - General 1 01-20-2004 08:21 AM


All times are GMT -5. The time now is 11:59 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration