Click here to Skip to main content
15,894,180 members
Articles / Desktop Programming / Windows Forms

Getting IP Details and Pinging the Gateway

Rate me:
Please Sign up or sign in to vote.
1.73/5 (8 votes)
5 Sep 2007CPOL 43.5K   2.9K   29   3
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:

C#
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)


Written By
Web Developer
India India
I'm a Mechanical Engineer - 2005 Passout
Working in an IT firm in B'lore.

Comments and Discussions

 
GeneralPing tool Pin
andreas61214-Jun-10 3:58
andreas61214-Jun-10 3:58 
I want to organize twenty-four-hour local network monitoring. Could you tell me about such program? The program has to ping hosts and save results in files on hard drive for viewing.
GeneralRe: Ping tool Pin
asturium16-Jun-10 2:23
asturium16-Jun-10 2:23 
QuestionPlease provide the full Source code Pin
Paw Jershauge29-Aug-07 0:49
Paw Jershauge29-Aug-07 0:49 

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

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