计算带宽 [英] Calculating Bandwidth

查看:131
本文介绍了计算带宽的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有什么办法可以计算出带宽(发送的数据包和接收)经由网络一个exe /应用程序?已loooked进入 IPGlobalProperties

Is there any way i can calculate bandwidth (packets sent and received) by an exe/application via net? have loooked into IPGlobalProperties,

和其他类....我想发送n中的接收由单个应用程序包.. 我检查 http://netstatagent.com/ 我需要类似的东西......是有什么在.net中,它可以帮助我吗?

and other classes .... i want packets sent n received by a single application.. i have checked http://netstatagent.com/ I need something similar... is there anything in .net which can help me?

我的应用程序连接到Web服务发送一些图像文件...还接收文件...

My app connects to web service to send some image files... and also receives files...

推荐答案

一种方法是检索的值<一href="http://msdn.microsoft.com/en-us/library/system.diagnostics.performancecounter.aspx">performance柜台.NET CLR网络/接收的字节和.NET CLR网络/发送的字节数为您的应用程序:

One way is to retrieve the value of the performance counters ".NET CLR Networking/Bytes Received" and ".NET CLR Networking/Bytes Sent" for your application:

PerformanceCounter bytesSentPerformanceCounter= new PerformanceCounter();
bytesSentPerformanceCounter.CategoryName = ".NET CLR Networking";
bytesSentPerformanceCounter.CounterName = "Bytes Sent";
bytesSentPerformanceCounter.InstanceName = GetInstanceName();
bytesSentPerformanceCounter.ReadOnly = true;

float bytesSent = bytesSentPerformanceCounter.NextValue();

//....

private static string GetInstanceName()
{
  // Used Reflector to find the correct formatting:
  string assemblyName = GetAssemblyName();
  if ((assemblyName == null) || (assemblyName.Length == 0))
  {
    assemblyName = AppDomain.CurrentDomain.FriendlyName;
  }
  StringBuilder builder = new StringBuilder(assemblyName);
  for (int i = 0; i < builder.Length; i++)
  {
    switch (builder[i])
    {
      case '/':
      case '\\':
      case '#':
        builder[i] = '_';
        break;
      case '(':
        builder[i] = '[';
        break;

      case ')':
        builder[i] = ']';
        break;
    }
  }
  return string.Format(CultureInfo.CurrentCulture, 
                       "{0}[{1}]", 
                       builder.ToString(), 
                       Process.GetCurrentProcess().Id);
}

private static string GetAssemblyName()
{
  string str = null;
  Assembly entryAssembly = Assembly.GetEntryAssembly();
  if (entryAssembly != null)
  {
    AssemblyName name = entryAssembly.GetName();
    if (name != null)
    {
      str = name.Name;
    }
  }
  return str;
}

请注意,该性能计数器不会创建,直到您第一次使用相关的网络库(你会得到InvalidOperation:实例'XXX'中指定的目录不存在),而且你需要插入

Note that the performance-counters aren't created until the first time you use the relevant network libraries (you will get InvalidOperation : Instance 'XXX' does not exist in the specified Category) and that you need to insert

<configuration>
  <system.net>
    <settings>
      <performanceCounters enabled="true" />
    </settings>
  </system.net>
</configuration>

在你的app.config。

in your app.config.

对于全样本下载 NetworkTraffic.cs NetworkTraffic。 exe.config

这篇关于计算带宽的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆