LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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 06-03-2011, 12:53 AM   #1
steffin
LQ Newbie
 
Registered: Jun 2011
Posts: 5

Rep: Reputation: Disabled
C program to check if network connection available or not


Hi,

I am a newbie with Linux and I am writing a simple C program which has to find whether the system is connected to network/internet or not..


Can please someone help?
 
Old 06-03-2011, 04:15 AM   #2
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
Moved: This thread is more suitable in Programming and has been moved accordingly to help your question get the exposure it deserves.
 
Old 06-03-2011, 05:06 AM   #3
markush
Senior Member
 
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,979

Rep: Reputation: Disabled
Hello steffin, welcome to LQ,

do you have any requirements how your program should check for the connection?

Note that it is a (more or less) big difference between "connected to the network" and "connected to the internet".

If you want to check for a network-connection, your program may scan the output of the ifconfig-command (which can be done with a simple shellscript and does not require C).

How about your experience with C and programming in general?

What are your ideas yet, did you yet write any code for your program?

Note that you'll find the manpages for the C-librarie in section 3, so the command
Code:
apropos network | grep -i \(3\)
will give you an overview about all network-related library-functions and may give you an idea where to search.

Markus
 
Old 06-03-2011, 05:25 AM   #4
steffin
LQ Newbie
 
Registered: Jun 2011
Posts: 5

Original Poster
Rep: Reputation: Disabled
Thanks markush for the reply..

What I want is to check whether the system is connected to the network or not..
I want the C program to get the status of the network connectivity..

This is the code I used to get the IP address of the system..Its working in Ubuntu environment..But failing in the platform environment which I actually want because the iocntl functionality is not working...


Quote:
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <net/if.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/ioctl.h>

int main(void)
{
int fd;
struct if_nameindex *curif, *ifs;
struct ifreq req;

if((fd = socket(PF_INET, SOCK_DGRAM, 0)) != -1) {
ifs = if_nameindex();
if(ifs) {
for(curif = ifs; curif && curif->if_name; curif++) {
strncpy(req.ifr_name, curif->if_name, IFNAMSIZ);
req.ifr_name[IFNAMSIZ] = 0;
if (ioctl(fd, SIOCGIFADDR, &req) < 0)
perror("ioctl"); //Getting Error here
else
printf("%s: [%s]\n", curif->if_name,
inet_ntoa(((struct sockaddr_in*) &req.ifr_addr)->sin_addr));
}
if_freenameindex(ifs);
if(close(fd)!=0)
perror("close");
} else
perror("if_nameindex");
} else
perror("socket");
return 0;
}

I dont know why..Can you specify any Linux APIs which we can use in the C program?

 
Old 06-03-2011, 05:21 PM   #5
Heraton
Member
 
Registered: Apr 2011
Location: Germany
Distribution: Mint 10, openSuSE
Posts: 58

Rep: Reputation: 3
Smile Looks like everyting works as supposed to be

Hi!

I compiled and ran your code on two different machines: Linux Mint 10 (32 bit) and openSUSE 11.3 (64 bit). I did not find anything but one thing:

The interface list you receive is containing all the interfaces. And this includes the interfaces which are down (via ifconfig or because of not being connected to a switch) or do not have an IPv4 configuration. In both cases the ioctl-call will fail, because there is no ip configuration to receive:
Quote:
ioctl: Cannot assign requested address
As far as i know this is the way ioctl is supposed to behave. ioctl simply tells you, it could not assign a configuration to the struct you told it.

regards, Heraton

Last edited by Heraton; 06-04-2011 at 01:04 AM.
 
Old 06-05-2011, 09:34 PM   #6
steffin
LQ Newbie
 
Registered: Jun 2011
Posts: 5

Original Poster
Rep: Reputation: Disabled
Thanks for running the code.

I was getting the same error,but not in Ubuntu.Dont know why!!

do you have any ideas abt modifying the code or abt any other code that can get the ip address/the connectivity status to the network.

Last edited by steffin; 06-05-2011 at 09:35 PM.
 
Old 06-06-2011, 05:28 AM   #7
steffin
LQ Newbie
 
Registered: Jun 2011
Posts: 5

Original Poster
Rep: Reputation: Disabled
Can someone give a solution for the question above?
 
Old 06-06-2011, 12:59 PM   #8
Heraton
Member
 
Registered: Apr 2011
Location: Germany
Distribution: Mint 10, openSuSE
Posts: 58

Rep: Reputation: 3
Cool fascinating

Quote:
Originally Posted by steffin
I was getting the same error,but not in Ubuntu.Dont know why!!
Did you double check that there are interfaces on your Ubuntu machine which should cause errors? If all the interfaces have an IP configuration, i see no reason to expect errors... If in doubt, just check via ifconfig that there is no ipv4 configuration before starting your program...

If you have double checked this already, what does your program print for the IP configuration of those interfaces without configuration?

regards, Heraton
 
Old 06-07-2011, 04:14 AM   #9
steffin
LQ Newbie
 
Registered: Jun 2011
Posts: 5

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Heraton View Post
Did you double check that there are interfaces on your Ubuntu machine which should cause errors? If all the interfaces have an IP configuration, i see no reason to expect errors... If in doubt, just check via ifconfig that there is no ipv4 configuration before starting your program...

If you have double checked this already, what does your program print for the IP configuration of those interfaces without configuration?

regards, Heraton
I got this problem solved.I ran some other program and found the IP address of the device.

But the problem is how do I find whether the network connection is available using this available IP address?
 
Old 06-07-2011, 07:37 AM   #10
markush
Senior Member
 
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,979

Rep: Reputation: Disabled
You can ping another host, but if this works depends on the firewall of the other system and yours.
If you're using DHCP and have a valid IP-configuration as well as valid DNS-settings (look at /etc/resolv.conf) the network should be available.

Markus
 
Old 06-08-2011, 01:38 PM   #11
Heraton
Member
 
Registered: Apr 2011
Location: Germany
Distribution: Mint 10, openSuSE
Posts: 58

Rep: Reputation: 3
Wink Did some homework for you...

I still don't understand what exactly your problem was and i would be interested to know what kind of program solved your problem. Anyway, i did some research on the connectivity topic and found some interesting threads...

Network Manager

ifconfig source code

I'm aware you're not going to build your custom kernel, but the ifconfig source might be worth a look. I just took a quick glimpse at it, and must admit i am kind of amazed how easy it is to find your way into it...

From what i've seen, i would start to delve into it in net-tools.../lib/interface.c. I would expect the check for whether the interface is up or not somewhere around line 780 (function ife_print_long) but that's still guessing. Anyway, shouldn't be hard to find.

regards, Heraton

Last edited by Heraton; 06-08-2011 at 01:42 PM. Reason: some ugly typo and grammar
 
  


Reply



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
Script to check connection (and restart network if down) mac_phil Linux - Networking 15 06-21-2016 05:59 PM
Ubuntu Program Startup After Network Connection? LAN-Dominator.nl Ubuntu 3 03-01-2010 07:27 AM
How to check which libraries does a program use? sceadu Programming 7 12-27-2004 07:39 PM
Cron job to check/restart network connection? mac_phil Linux - Networking 6 09-01-2004 01:51 PM
Please check this program(perl) for me! Rex_chaos Programming 2 04-25-2002 06:56 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:48 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration