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";
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";
IPAddress addr = address.Address;
Ping pingSender = new Ping();
PingOptions options = new PingOptions();
options.DontFragment = true;
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);
{
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";
}
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)
{
{
richTextBox1.Text = "";
string iphostname = "";
try
{
iphostname = Dns.GetHostName();
IPHostEntry ipentry = Dns.GetHostEntry(iphostname);
IPAddress[] addr = ipentry.AddressList;
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";
}
try
{
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
{
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
{
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
{
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
{
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
{
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";
}
}
}
}
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.