Click here to Skip to main content
Click here to Skip to main content
Go to top

Network Utility

, 26 Jun 2014
Rate this:
Please Sign up or sign in to vote.
A Network Utility application to get system information and analyze your network

Introduction

Network Utility is a trivial application to get system information and analyze your network. It's trivial because some of its features can be performed using the Windows command line tool. The purpose of this article is to provide some information on how some of the features were implemented and the .net namespaces and classes used.

The utility has four separate features, each of which is detailed below.

  • System Information
  • Net Stat
  • Ping
  • Port Scanner

System Information

Information about your system, such as the machine name or username can be found in either System.Environment or System.Windows.Forms.SystemInformation. These namespaces provide system related information with very little coding. If you want to get network information such as a MAC address, use the System.Net.NetworkInformation. This namespace has a handy class called NetworkInterface that has a static GetAllNetworkInterfaces() method which returns a collection of NetworkInterface's. By iterating over this collection, you can examine and get details for each interface. The Network Utility application makes use of both these namespaces to display system and network information.

Net Stat

Net Stat displays active TCP connections. It is a simple version of the netstat command line tool, which provides more details about active connections. To my surprise, I found that the System.Net.NetworkInformation has a IPGlobalProperties class with a static GetIPGlobalProperties() method, which returns a IPGlobalProperties object. Using this object, I was able to get a list of all active TCP connections using the GetActiveTcpConnections() method. The code below shows how to get a list of active TC connections and display the remote address and port.

IPGlobalProperties properties = IPGlobalProperties.GetIPGlobalProperties();
TcpConnectionInformation[] connections = properties.GetActiveTcpConnections();

foreach (TcpConnectionInformation info in connections)
{
	Console.WriteLine(info.RemoteEndPoint.Address.ToString());
	Console.WriteLine(info.RemoteEndPoint.Port.ToString());
}

The Address property is an instance of IPAddress. Using this instance, you can attempt to resolve its hostname by using the Dns class, which is found in the System.Net namespace. The Dns class has a static GetHostEntry() method, which will attempt to resolve the IP address to a hostname. It should be noted, that if you have many connections, doing a DNS lookup on each IP may take some time. Resolving to a hostname has the benefit of identifying the connections.

The Window's netstat command line tool has a useful argument (netstat -o), which displays the system process id (PID) used by a connection. With a PID, you can easily determine the application and take any action if needed. I was hoping the .Net framework would have a class that can return the PID's for connections. Unfortunately, it doesn't. However there is an unmanned IP Helper API (GetExtendedTcpTable function) that can be be used to get the PID for each connection. More information about this function can be found here.

Ping

The purpose of a ping is to determine the reachability of a host and the time it takes to respond. It works by sending an Internet Control Message Protocol packet to a target machine and measures the time it takes from initial sending to receiving a response and records any packet loss. It turns out, the .Net framework provides a Ping class found in the System.Net.NetworkInformation namespace, which makes pinging very simple. The Ping class reference on the MSDN website provides a simple example usage.

Port Scanner

Scanning for open ports, seems like something a hacker might want to do, but it is also useful for network administrators, who want to monitor which ports are open on their systems. While the .Net framework doesn't provide a port scanning class, it is actually very simple to code. Using the TcpClient class found in the System.Net.Sockets namespace and attempting to connect to a given host using a particular port will suggest if the port is open. If a connection is made, it suggests there is an application listening on that port.

try
{
	TcpClient client = new TcpClient("127.0.0.1", "80");
	// Port open
}
catch (Exception e){
	// port closed.
}

This brings me to the end of this article. Please feel free to leave your comments and suggestions. You can follow me at www.snippetbank.net.

License

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

Share

About the Author

Syed M Hussain
Web Developer
United Kingdom United Kingdom
No Biography provided

Comments and Discussions

 
QuestionFilter PinmemberR3COV3RY11-Sep-14 2:29 
GeneralPort scanner PinmemberRiz Thon29-Jun-14 15:35 

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.

| Advertise | Privacy | Mobile
Web01 | 2.8.140916.1 | Last Updated 26 Jun 2014
Article Copyright 2014 by Syed M Hussain
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid