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!
All and any comments / bugs / suggestions are welcomed!
19 comments:
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
great source code
thank you
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?
@dashingsidds: I didn't check that. If I get time then I will check and let you about it.
Hi,
Thanks for your post, it helped me a lot.
Hi Thanks for ur help it's a useful post for me......
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.
@Anonymous: yes you can use my code in your client application and let me know if you have any issue
Great Code, thanks
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.
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?
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.
Goodone.
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...
Thanks Asim,
saved me a lot of time.
Great post.
Ray.
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 :)
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.
Hi,
Source code Link is not available anymore. Could you please update or send via email? :(
Thanks a lot in advance.
Post a Comment