C program to check if network connection available or not
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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...
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.
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?
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?
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.
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...
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.