3

What does that particular field return? I want the the number of bytes received per second. Should I rely on this?

soham
  • 1,508
  • 6
  • 30
  • 47

3 Answers3

9

I think you can use it that way:

long beginValue = NetworkInterface.GetIPv4Statistics().BytesReceived;
DateTime beginTime = DateTime.Now;

// do something

long endValue = NetworkInterface.GetIPv4Statistics().BytesReceived;
DateTime endTime = DateTime.Now;

long recievedBytes = endValue - beginValue;
double totalSeconds = (endTime - beginTime).TotalSeconds;

var bytesPerSecond = recievedBytes / totalSeconds;

Code Snippet for periodically update

private object _lockObj;
private long bytesPerSecond = 0;
private Timer _refreshTimer = new Timer { Interval = 1000 };

// do in ctor or some init method
_refreshTimer.Tick += _refreshTimer_Tick;
_refreshTimer.Enabled = true;

private void _refreshTimer_Tick(object sender, EventArgs e)
{
  ThreadPool.QueueUserItem(callback => 
  {
    long beginValue = NetworkInterface.GetIPv4Statistics().BytesReceived;
    DateTime beginTime = DateTime.Now;

    Thread.Sleep(1000);

    long endValue = NetworkInterface.GetIPv4Statistics().BytesReceived;
    DateTime endTime = DateTime.Now;

    long recievedBytes = endValue - beginValue;
    double totalSeconds = (endTime - beginTime).TotalSeconds;

    lock(_lockObj)
    {
      bytesPerSecond = recievedBytes / totalSeconds;
    }
  });

}

You can combine this with some tracking to record the recieved Bytes over time

Verni
  • 211
  • 1
  • 9
  • Actually, I want to update the value dynamically and this is not the solution I want. Thanks though. – soham Nov 27 '12 at 08:04
  • Thanks. Want some clarification. you have set the Timer interval as 1sec. and again you are calling Thread.Sleep(1000). So, is it total 2sec of waiting there? And what is the objective of _lockObj? – soham Nov 27 '12 at 08:27
  • Thread.Sleep(1000) is to wait 1 sec, between begin and end measure. The Timer Tick will not be affected, so you come to ~1sec refresh time. The LockObj is there to make sure that only one access is suddenly to the variable. Sorry for bad english ;) ... it's not my native language. – Verni Nov 27 '12 at 08:54
  • couldn't get complete idea of `_lockObj` Anyway it is working without that also. I am using it with own modification. Thanks. – soham Nov 27 '12 at 09:01
  • Also, as the thread goes to sleep. My UI is getting freeze. So, any solution to this? – soham Nov 27 '12 at 09:11
  • i updated my Answer with ThreadPool Implementation. I forgot that the WinForms.Timer is Single Threaded but this code must run in a seperate thread. – Verni Nov 27 '12 at 09:21
  • First you can try the updated implementation, if it work for you, all fine. Threading is a big topic, its better you read some Tutorials like that: http://www.codeplanet.eu/tutorials/csharp/64-multithreading-in-csharp.html – Verni Nov 27 '12 at 09:33
  • It's not working because of some "cross-threaded implementation" error. – soham Nov 27 '12 at 09:46
  • Okay. I have done threaded implementation. Now it's showing the download/upload speed. But the speed is not at all accurate. Though it's giving accurate value for first few seconds then, it's erroneous. How to get more accurate value? any idea? – soham Nov 28 '12 at 06:30
4
NetworkInterface.GetIPv4Statistics().BytesReceived 

will show you the total number of bytes received for the given interface.

I dont think you can exactly use this to get the number of bytes received per second.

Check This

skjcyber
  • 5,759
  • 12
  • 40
  • 60
  • Can you suggest any method that I can use to do so? – soham Nov 27 '12 at 07:57
  • If you can get the total UP time of the network interface, then you may want this: (NetworkInterface.GetIPv4Statistics().BytesReceived)/(UP time in second) – skjcyber Nov 27 '12 at 08:02
0

http://msdn.microsoft.com/en-us/library/system.net.networkinformation.ipv4interfacestatistics.bytesreceived.aspx

According to MSDN (as others have also pointed out) it Gets the number of bytes that were received on the interface.

How does it behave if, for example, the network interface is disconnected and reconnected (e.g. a network cable is pulled out or a wireless connection drops out).

If you want a much more reliable way to capture packets, filter them, dissect them and only count the ones that matter, then have a look at http://sourceforge.net/projects/sharppcap/

BlokeTech
  • 317
  • 1
  • 7