Click here to Skip to main content
Licence CPOL
First Posted 28 Aug 2007
Views 18,895
Downloads 930
Bookmarked 26 times

Getting IP Details and Pinging the Gateway

By | 5 Sep 2007 | Article
This program gets all the IP details and pings the Gateway.

Screenshot - Netmon.jpg

Introduction

Whenever we encounter any Internet related problems, we keep switching between cmd mode and IE browsers to monitor the network stats. This application resolves the details of all the Network Interfaces and also pings all the Gateways.

Why it's useful, the problem it solves, etc.

Till now, to get the IP details, we had to switch between the Command Prompt and browsers and constantly key in commands. This application comes in handy and resolves the IP properties of the system and can ping all the Gateways.

Using the code

The code is handy and can be incorporated in any existing code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.NetworkInformation;
using System.Diagnostics;

namespace Netmon
{
    public partial class Netmon : Form
    {
        public Netmon()
        {
            InitializeComponent();
        }

        private void Start_Click(object sender, EventArgs e)
        {

            {
                richTextBox2.Text = "\n";
                //Gateway IP 
                NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces();
                foreach (NetworkInterface adapter in adapters)
                {
                    IPInterfaceProperties adapterProperties = adapter.GetIPProperties();
                    GatewayIPAddressInformationCollection addresses = 
                                               adapterProperties.GatewayAddresses;
                    if (addresses.Count > 0)
                    {
                        foreach (GatewayIPAddressInformation address in addresses)
                        {
                            richTextBox2.Text += "Gateway Address  | " + 
                                                 address.Address.ToString() + "\r\n";

                            // Pinging
                            IPAddress addr = address.Address;
                            Ping pingSender = new Ping();
                            PingOptions options = new PingOptions();

                            // Use the default Ttl value which is 128,
                            // but change the fragmentation behavior.
                            options.DontFragment = true;

                            // Create a buffer of 32 bytes of data to be transmitted.
                            string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
                            byte[] buffer = Encoding.ASCII.GetBytes(data);
                            int timeout = 120;
                            int count = 0; float Success = 0; float  loop = 0;
                            for (int i = 0; i < 25; )
                            {
                                PingReply reply = 
                                  pingSender.Send(addr, timeout, buffer, options);

                                {
                                    //richTextBox2.Text += "\n" + "Address: " + 
                                    //                     reply.Address.ToString() + "\t";
                                    try
                                    {
                                        richTextBox2.Text += "\n" + "RoundTrip time: " + 
                                                             reply.RoundtripTime + 
                                                             " ms" + "\t";
                                    }
                                    catch 
                                    {
                                        richTextBox2.Text += "\n" + "RoundTrip time: " + 
                                                             "  " + " ms" + "\t";
                                    }

                                    try
                                    {
                                        richTextBox2.Text += "Time to live(TTL): " + 
                                                             reply.Options.Ttl + "\t";
                                    }
                                    catch
                                    {
                                        richTextBox2.Text += "Time to live(TTL): " + 
                                                             "       " + "\t";
                                    }
                                    //richTextBox2.Text += "Don't fragment: " + 
                                    //           reply.Options.DontFragment + "\t";
                                    try
                                    {
                                        richTextBox2.Text += "Status : " + 
                                                             reply.Status + "\t";
                                    }
                                    catch 
                                    {
                                      richTextBox2.Text += "Status : " + 
                                                           reply.Status + "\t";
                                    }
                                    count += Convert.ToInt32(reply.RoundtripTime);
                                    if (reply.Status == IPStatus.Success)
                                    { Success += 1; } 
                                } 
                                i++;
                            }
                            loop += 1;
                            richTextBox2.Show();
                        }
                        richTextBox2.Text += "\n" + "Avg. Roundtime : " + 
                                             count / loop+" ms";
                        float  AvgSuccess = ((Success / loop) * 100);
                        richTextBox2.Text += "\n" + "Avg. Success (%)  : " + 
                                             AvgSuccess  + "%";
                    }
                }
            }
        }
    }
        
    private void Netmon_Load(object sender, EventArgs e)
    {
        {
            // Initialize Variables
            richTextBox1.Text = "";
            string iphostname = "";
            try
            {
                iphostname = Dns.GetHostName();  // Resolving Host name
                IPHostEntry ipentry = Dns.GetHostEntry(iphostname);
                IPAddress[] addr = ipentry.AddressList;// Resolving IP Addresses
                for (int i = 0; i < addr.Length; i++)
                {
                    try
                    {
                      richTextBox1.Text += "IP Address            | " + 
                                           Convert.ToString(addr[i]) + "\r\n"; }
                    catch
                    {
                      richTextBox1.Text += "IP Address            | " + "\r\n";
                    }
                }
                     
                richTextBox1.Text += "Hostname             | " + 
                                     ipentry.HostName + "\r\n";
            }
            catch
            {
              richTextBox1.Text += "Hostname             | " + "\r\n";
            }
            // More Details
            try
            {
              // Getting DNS Addresses
              for (int i = 0; i < 3;i++ )
                  richTextBox1.Text += "DNS Address       | " + 
                    System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces(
                     )[i].GetIPProperties().DnsAddresses[0].ToString() + "\r\n";
            }
            catch
            {
              richTextBox1.Text += "DNS Address       | " +  "\r\n";
            }
            try
            {
              // Getting Network Type Description
              for (int i = 0; i < 2; i++)
                richTextBox1.Text += "Description           | " + 
                  System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces(
                  )[i].Description.ToString() + "\r\n";
            }
            catch
            {
              richTextBox1.Text += "Description       | "  + "\r\n";
            }
            try
            {
              // Getting the Details of Connection
              for (int i = 0; i < 2; i++)
                richTextBox1.Text += "Name                    | " + 
                  System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces(
                  )[i].Name.ToString() + "\r\n";
            }
            catch
            {
              richTextBox1.Text += "Name                    | " + "\r\n";
            }
            try
            {
              // Operational Status
              for (int i = 0; i < 2; i++)
                richTextBox1.Text += "Operation Status   | " + 
                  System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces(
                  )[i].OperationalStatus.ToString() + "\r\n";
            }
            catch
            {
              richTextBox1.Text += "Operation Status   | " + "\r\n";
            }
            try
            {
              // Getting DHCP Details
              for (int i = 0; i < 3; i++)
                richTextBox1.Text += "DHCP                   | " + 
                  System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces(
                  )[i].GetIPProperties().DhcpServerAddresses[0].ToString() + "\r\n";
            }
            catch
            {
              richTextBox1.Text += "DHCP                   | " + "\r\n";
            }
            try
            {
              // Getting DNS Details
              for (int i = 0; i < 3; i++)
                richTextBox1.Text += "DNS Suffix            | " + 
                  System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces(
                  )[i].GetIPProperties().DnsSuffix.ToString() + "\r\n";
            }
            catch
            {
              richTextBox1.Text += "DNS Suffix            | " + "\r\n";
            }
            try
            {
              NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces();
              foreach (NetworkInterface adapter in adapters)
              {
                IPInterfaceProperties adapterProperties = adapter.GetIPProperties();
                GatewayIPAddressInformationCollection addresses = 
                                                     adapterProperties.GatewayAddresses;
                if (addresses.Count > 0)
                {
                    foreach (GatewayIPAddressInformation address in addresses)
                    {
                        richTextBox1.Text += "Gateway Address  | " + 
                                             address.Address.ToString() + "\r\n";
                    }
                }
              }
            }
            catch
            {
              richTextBox1.Text += "Gateway Address  | " + "\r\n";
            }
        }
    }
}
// END 

Instead of writing the classes from scratch, .NET framework is used, thus reducing the complexity of the code.

Points of interest

Earlier when I tried to implement some code using .NET in-built functionalities, it was very hard to find them, and finally when I got a clue and used trial and error approach, I was able to get all the details.

History

This is the basic version that I have created and might not be optimal.

Any suggestions for improvements are welcome.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Sharadendu Kumar Singh

Web Developer

India India

Member

I'm a Mechanical Engineer - 2005 Passout
Working in an IT firm in B'lore.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralPing tool Pinmemberandreas6123:58 14 Jun '10  
GeneralRe: Ping tool Pinmemberasturium2:23 16 Jun '10  
QuestionPlease provide the full Source code PinmemberPawJershauge0:49 29 Aug '07  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 5 Sep 2007
Article Copyright 2007 by Sharadendu Kumar Singh
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid