LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Verify network connection (https://www.linuxquestions.org/questions/programming-9/verify-network-connection-609533/)

yhus 12-27-2007 10:24 PM

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

indienick 12-27-2007 10:36 PM

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?

yhus 12-27-2007 11:10 PM

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

indienick 12-28-2007 11:13 PM

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



All times are GMT -5. The time now is 12:04 AM.