Saturday, August 8, 2009

Calculating network bandwidth speed in C#

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.

All and any comments / bugs / suggestions are welcomed!


19 comments:

timematcher said...

Hi there,
I have found your application to be very useful.

Just wondering if i can use the same to measure bandwidth consumption of websites on my server?

Any ideas on this?

agar aik windows service bana li jaaiay to kia iis manager main mojood websites ki consumption ko map kiya ja sakta hai?

Reply will be much appreciated.

Thanks

Anonymous said...

great source code
thank you

dashingsidds said...

Hi Asim indeed a very good post. Just 1 question here. Is it possible to fetch the list of applications which are using the internet bandwidth?

Asim Sajjad said...

@dashingsidds: I didn't check that. If I get time then I will check and let you about it.

aithagoni said...

Hi,
Thanks for your post, it helped me a lot.

Anonymous said...

Hi Thanks for ur help it's a useful post for me......

Anonymous said...

Hi, i'm going to develop a client and server system that monitor the amount of bandwidth that been used from the client side. Are your code can be used in my system??
Thank you and sorry for any inconvenient.

Asim Sajjad said...

@Anonymous: yes you can use my code in your client application and let me know if you have any issue

Aziz said...

Great Code, thanks

Anonymous said...

hi, i try out your source code. But when i run it, it show up this call stack "Internet Speed.exe!Internet_Speed.frmMainForm.frmMainForm() Line 49" and also "Internet Speed.exe!Internet_Speed.Program.MainForm() Line 17 + 0x1e bytes" what are those means? fyi i am just a c# beginner user, anyway thank for your code.

Anonymous said...

First of all, thanks for the code. if let say i want to get the client bandwidth usage using your code and display the output/GUI at the server forms, need to put the code at client side? your code is only calculate bandwidth for local computer?? how server retrieve the client bandwidth usage?

techispot said...

application run with me but not working, I mean it is not measuring my download speed and upload speed it shows 0,0 to everything. Even downloading and browsing is going on.

Anonymous said...

Goodone.

Anonymous said...

great Code but, i want that program retrieve the network bandwidth on line as well as on netconnect or wireless modem, and also display how much time the user has been spent before disconnecting.

thanx...

Anonymous said...

Thanks Asim,
saved me a lot of time.
Great post.
Ray.

Unknown said...
This comment has been removed by the author.
Anonymous said...

hello i need to create a script to check the bandwidth of the company where i work could u help me giving me some tips of how i could do this ?

Thanks :)

Anonymous said...

Hello Asim,
Why the output is not matching with speedtest.com?
Please answer.. I need a code which calculates accurate (or almost) U/D speed..

Thanks.

Unknown said...

Hi,
Source code Link is not available anymore. Could you please update or send via email? :(

Thanks a lot in advance.