LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
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 12-27-2007, 10:24 PM   #1
yhus
Member
 
Registered: May 2004
Posts: 74

Rep: Reputation: 15
Verify network connection


Hi,

I have more than 2 network interfaces and I need to check the network connection before seding packets down to it in my C++ program.

What is the best way to verify network connections and to get it's subnet address in a C++ program? For example, I have eth0 and eth1 and I need to do following in my program:

1. Verify if eth0 or eth1 has been configurated.

2. Connection to eth0 or eth1 can get through.

3. Get its subnet IP address.

Thank you.

Jim
 
Old 12-27-2007, 10:36 PM   #2
indienick
Senior Member
 
Registered: Dec 2005
Location: London, ON, Canada
Distribution: Arch, Ubuntu, Slackware, OpenBSD, FreeBSD
Posts: 1,853

Rep: Reputation: 65
This sort of thing could be quite easily done using a combination of ifconfig, ping, sed and awk in a Bash script.

Did you want the solution to be in C++, or is it completely open?
 
Old 12-27-2007, 11:10 PM   #3
yhus
Member
 
Registered: May 2004
Posts: 74

Original Poster
Rep: Reputation: 15
Verify network connection

I can be a C program as well, any bash script can also be implemented in C program. Wuold you please explain more details?

Thank you.

Jim
 
Old 12-28-2007, 11:13 PM   #4
indienick
Senior Member
 
Registered: Dec 2005
Location: London, ON, Canada
Distribution: Arch, Ubuntu, Slackware, OpenBSD, FreeBSD
Posts: 1,853

Rep: Reputation: 65
Wonderful. Unfortunately, I know the way to go about doing this, I just don't know the exact syntax. Sed and Awk are somewhat of a mystery to me, but I'm sure you could more than easily figure it out.

1. Verify if eth0 or eth1 has been configurated.
Running ifconfig and then piping the output to sed/awk to parse the lines that come out for the "inet addr:XXX.XXX.XXX.XXX" section (located in the second line of the output).

2. Connection to eth0 or eth1 can get through.
Now, I'm not quite sure what you mean by this, but if you're on a network with access to the internet, pinging a website (or something of the like) will let you know if network traffic can get through.
If you're on an intranet with no contact with the outside world, then ping the router, or destination and see if the packets are received and acknowledged.
Code:
$ ping -c4 192.168.1.100
As an example.

3. Get its subnet IP address.
This step could be rolled into step one, as you may want to avoid the few extra flops it would take to re-run a particular process (ifconfig). The subnet IP address (the "broadcast" address), is also in the second line of the output, and is the next printed value.

Here's what my ifconfig output looks like for eth0:
Code:
eth0      Link encap:Ethernet  HWaddr 00:40:f4:19:c5:f9
          inet addr:192.168.2.12  Bcast:192.168.2.255  Mask:255.255.255.0
          inet6 addr: fe80::240:f4ff:fe19:c5f9/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:15690 errors:0 dropped:0 overruns:0 frame:0
          TX packets:14484 errors:0 dropped:0 overruns:0 carrier:0
          collisions:31 txqueuelen:1000
          RX bytes:12045660 (11.4 MiB)  TX bytes:286089 (2.1 MiB)
          Interrupt:201 Base address:0x9800
 
  


Reply

Tags
connection, network, verify


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
No network connection at all Score Debian 4 03-12-2007 09:28 AM
my internet connection disconnects my network connection? emre polat Linux - Networking 2 12-19-2006 01:11 PM
Network Connection starorbs Slackware - Installation 1 09-14-2006 10:43 AM
Can't get a network connection JMJ_coder Slackware 26 04-14-2006 01:25 AM
Network Connection InsaneBob Linux - Software 2 08-30-2003 06:26 PM


All times are GMT -5. The time now is 01:10 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