Network statistics for upload and download
Posted on 8/2/2010 9:41:46 AM
in #Dot Net Framework
Last few days while working with my broadband network (downloading few stuff) I wanted to check the exact amount of download per minute. I tried to check the data from task manager and resource monitor. But these gave data in terms of percentage of network use. Hence I could never find the exact amount of download. /o:p>/P>
So I wrote a small application to sniff the network and find out the exact amount of download. After fetching the data we can do lots of thing like creating speed chart etc…/o:p>/P>
To fetch the network we use the System.Net.NetworkInformation namespace/o:p>/P>
First we need to get the network Interface to track. A Machine might be connected to many network and we need to track each network differently. We can do that by the code below./P>
foreach (NetworkInterface currentNetworkInterface in NetworkInterface.GetAllNetworkInterfaces())
{
if (currentNetworkInterface.Name.ToLower() == "NameOf network")
{
networkInterface = currentNetworkInterface;
break;
}
}/P>
We get the network usage of the current network with the help of GetIPv4Statistics function which returns object of the IPv4InterfaceStatistics class./P>IPv4InterfaceStatistics interfaceStatistic = networkInterface.GetIPv4Statistics();
You can find the total bytes send and received with the help of the property./o:p>/P>
lngBytesSend = interfaceStatistic.BytesSent;
lngBtyesReceived = interfaceStatistic.BytesReceived/P>
To find the speed of the download and upload you need to find the difference of the Bytes send and received between required time interval and you will have the speed./P>
Remember bytes send and received are kept differently hence total download = bytes send + bytes received./P>
Vikram/o:p>/P>/SPAN>/SPAN>/SPAN>
|