简体   繁体   English

计算带宽

[英]Calculating Bandwidth

Is there any way i can calculate bandwidth (packets sent and received) by an exe/application via net? 有没有办法可以通过网络计算exe /应用程序的带宽(发送和接收的数据包)? have loooked into IPGlobalProperties , 已经陷入了IPGlobalProperties

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? 和其他类....我想要由单个应用程序收到的数据包发送...我已经检查了http://netstatagent.com/我需要类似的东西... .net中有什么可以帮助我吗?

My app connects to web service to send some image files... and also receives files... 我的应用程序连接到Web服务以发送一些图像文件...并且还接收文件...

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: 一种方法是为您的应用程序检索性能计数器 “.NET CLR Networking / Bytes Received”和“.NET CLR Networking / Bytes Sent”的值:

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;
}

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 请注意,性能计数器 直到您第一次使用相关网络库时才会创建 (您将获得InvalidOperation:指定类别中不存在实例'XXX')并且您需要插入

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

in your app.config. 在你的app.config中。

For a full sample download NetworkTraffic.cs and NetworkTraffic.exe.config . 有关完整示例,请下载NetworkTraffic.csNetworkTraffic.exe.config

I remembered reading an article about this and dug it up for you, http://nayyeri.net/blog/how-to-calculate-network-utilization-in-net/ 我记得读过一篇关于此的文章并为你挖出来, http://nayyeri.net/blog/how-to-calculate-network-utilization-in-net/

an excerpt before their code: 他们的代码之前的摘录:

.NET comes with three performance counters for the used parameters in network utilization formula out of the box. .NET带有三个性能计数器,用于开箱即用的网络利用率公式中的使用参数。 All of these counters are located in Network Interface category and are named "Bytes Sent/sec", "Bytes Received/sec" and "Current Bandwidth". 所有这些计数器都位于网络接口类别中,并命名为“Bytes Sent / sec”,“Bytes Received / sec”和“Current Bandwidth”。 The only parameter that requires some extra effort to be calculated is time_in_sec. 需要额外努力才能计算的唯一参数是time_in_sec。

"Bytes Sent/sec" and "Bytes Received/sec" counters calculate their values based on different samples and the best way to get a better value from these counters is finding the summation of their values in a loop because in some cases their values may be zero or very different from the real state of the network. “Bytes Sent / sec”和“Bytes Received / sec”计数器根据不同的样本计算它们的值,从这些计数器获得更好值的最佳方法是在循环中找到它们的值的总和,因为在某些情况下它们的值可能是是零或非常不同于网络的真实状态。 Then we can find the time_in_sec parameter by finding the number of times that the loop is iterated because our performance counters find their values for one seconds the overall time in seconds is equal to the number of iterations. 然后我们可以通过查找迭代循环的次数来找到time_in_sec参数,因为我们的性能计数器找到它们的值一秒钟,总时间(秒)等于迭代次数。

我查找每个应用程序的字节数/秒...不适用于整个计算机....似乎不适用于控制台应用程序...错误消息:“控制台应用程序不存在于指定的类别中。”

这不起作用......据我所知bytesSentPerformanceCounter.InstanceName =“”//这里你需要提供网卡名称...

somehow the bytes sent is way too less than bytes received... its not that im browsing net from my application... I send to the web service images(as bytes) and other XML files (few kbs as string input to web service function). 不知怎的,发送的字节比收到的字节少得多......它不是我从我的应用程序浏览网...我发送到Web服务图像(作为字节)和其他XML文件(几个kbs作为字符串输入到Web服务功能)。 In return i sometimes return error codes or bool... still the bytes sent is way too less than received... received is 5 times more...im puzzled... 作为回报,我有时会返回错误代码或bool ...仍然发送的字节太少于收到...收到的是5倍...我很困惑......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM