Monday, January 10, 2011

To Calculating network bandwidth speed in C# | networking - Detecting network connection speed


In this post I will tell you how to use the C# language to calculate the bandwidth. Here is my simple form which is used to display information about the network bandwidth speed. For this example I have only label controls on my form and one Timer control which is used to update the status of the network bandwidth after every second.



First here is the code which i have used to get the local area connection object from the GetAllNetworkInterfaces function and is a member function of the NetworkInterface class which Provides configuration and statistical information for a network interface. This class encapsulates data for network interfaces, also known as adapters, on the local computer. You do not create instances of this class; the GetAllNetworkInterfaces method returns an array that contains one instance of this class for each network interface on the local computer. In the default construct of the form, I have used the foreach loop to get the local area connection network interface. I have matched the name of the networkInterface and when it is found I have place it in the class variable so that I can used it later.
foreach (NetworkInterface currentNetworkInterface in NetworkInterface.GetAllNetworkInterfaces())
if (currentNetworkInterface.Name.ToLower() == "local area connection")
{
networkInterface = currentNetworkInterface;
break;
}
Here is the code which is called every tick of the time and used to update the label to show the internet status. Here in the first statement I have used the GetIPv4Statistic function of the network interface class , which will return the object of IPv4InterfaceStatistics class.From that object, we can fetch the BytesSent and BytesReceived from that network interface. By creating a simple timer that handles each tick rate in each 1sec interval, we can find the speed of bytes per second by finding the difference between the new bytes with respect to the previous ones. To make it more readable, we could convert bytes into kilobytes by dividing by 1024.

IPv4InterfaceStatistics interfaceStatistic = networkInterface.GetIPv4Statistics();

int bytesSentSpeed = (int)(interfaceStatistic.BytesSent - lngBytesSend) / 1024;
int bytesReceivedSpeed = (int)(interfaceStatistic.BytesReceived - lngBtyesReceived) / 1024;

lblSpeed.Text = (networkInterface.Speed / 1000000).ToString() + " Mbps";
lblPacketReceived.Text = interfaceStatistic.UnicastPacketsReceived.ToString();
lblPacketSend.Text = interfaceStatistic.UnicastPacketsSent.ToString();
lblUpload.Text = bytesSentSpeed.ToString() + " KB / s" ;
lblDownLoad.Text = bytesReceivedSpeed.ToString() + " KB / s ";
lngBytesSend = interfaceStatistic.BytesSent;
lngBtyesReceived = interfaceStatistic.BytesReceived;
After the calculation of the upload and download speed, the new values of the bytesSend and bytesReceived are save in the class variable so that next time these variable are used to calculate the upload and download speed.You can download the source code from here.

No comments:

Post a Comment