Piz&Yumina's programming note Engrish version : 

上記の広告は1ヶ月以上更新のないブログに表示されています。
新しい記事を書く事で広告が消せます。



◇ カテゴリ : スポンサー広告

It is important for a network application to figure out the bandwidth (bytes sent and received per second) across a network.

.NET provides a variety of predefined performance counters and you can make use of ".NET CLR Networking/Bytes Sent" and ".NET CLR Networking/Bytes Received" counters for your application. These values are the total amount of bytes sent or received via the network since the application started, so you need to retrieve these values second by second and take the difference between the current value and the previous value to calculate the bandwidth.

 

The following is a minimum sample program that illustrates a concise implementation of a bandwidth monitor.

using System;

using System.Diagnostics;

using System.Threading;

 

namespace Bandwith

{

    class Program

    {

        public static float bytesSent1;

        public static float bytesSent2;

 

        public static float bytesReceived1;

        public static float bytesReceived2;

 

        public static void Main(string[] args)

        {

            PerformanceCounter bytesSentPerformanceCounter = new PerformanceCounter();

            bytesSentPerformanceCounter.CategoryName = ".NET CLR Networking";

            bytesSentPerformanceCounter.CounterName = "Bytes Sent";

            bytesSentPerformanceCounter.InstanceName = "app name[process ID]";

            bytesSentPerformanceCounter.ReadOnly = true;

 

            PerformanceCounter bytesReceivedPerformanceCounter = new PerformanceCounter();

            bytesReceivedPerformanceCounter.CategoryName = ".NET CLR Networking";

            bytesReceivedPerformanceCounter.CounterName = "Bytes Received";

            bytesReceivedPerformanceCounter.InstanceName = "app name[process ID]";

            bytesReceivedPerformanceCounter.ReadOnly = true;

 

            using (Timer timer = new Timer((state) =>

            {

                bytesSent2 = bytesSentPerformanceCounter.NextValue();

                float difSent = bytesSent2 - bytesSent1;

                bytesSent1 = bytesSent2;

 

                bytesReceived2 = bytesReceivedPerformanceCounter.NextValue();

                float difReceived = bytesReceived2 - bytesReceived1;

                bytesReceived1 = bytesReceived2;

 

                Console.WriteLine("送信:{0}KB/s, 受信:{1}KB/s", difSent / 1000, difReceived / 1000);

            }, null, 0, 1000))

            {

                Console.ReadKey();

            }

        }

    }

}

 

The program is very simple but there is a pitfall in that program. Network performance counters in .NET is disabled by default and you can not have access to these counters out of the box. To enable network performance counters in .NET, you can tweak the config file of the application as follows:

<configuration>

  <system.net>

    <settings>

      <performanceCounters enabled="true"/>

    </settings>

  </system.net>

</configuration>

 

Now that the performance counters work well, the program is displaying the bandwidth value successfully.


◇ コメントを書く/見る(12) 
◇ トラックバックする
◇ カテゴリ : .NET