Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / desktop / system

Network Utility

4.92/5 (8 votes)
26 Jun 2014CPOL3 min read 35.2K   1.9K  
A network utility app to get system information and analyze your network
In this article, you will find information on how some features of Network Utility were implemented and the .NET namespaces and classes used.

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:

  1. System Information
  2. Net Stat
  3. Ping
  4. 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 NetworkInterfaces. 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.

C#
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 PIDs for connections. Unfortunately, it doesn't. However there is an unmanned IP Helper API (GetExtendedTcpTable function) that can 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.

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

History

  • 26th June, 2014: Initial version

License

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