C# 计算带宽
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/442409/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Calculating Bandwidth
提问by
Is there any way i can calculate bandwidth (packets sent and received) by an exe/application via net? have looked into IPGlobalProperties, and other classes.
有什么方法可以通过网络计算 exe/应用程序的带宽(发送和接收的数据包)?已经研究了IPGlobalProperties和其他类。
I want packets sent and received by a single application. I have checked http://netstatagent.com/and need something similar.
我想要由单个应用程序发送和接收的数据包。我已经检查过http://netstatagent.com/并且需要类似的东西。
Is there anything in .Net which can help me?
.Net 中有什么可以帮助我的吗?
My app connects to web service to send and receive some image files.
我的应用程序连接到网络服务以发送和接收一些图像文件。
回答by Rasmus Faber
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-countersaren'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 : Instance 'XXX' does not exist in the specified Category)并且您需要插入
<configuration>
<system.net>
<settings>
<performanceCounters enabled="true" />
</settings>
</system.net>
</configuration>
in your app.config.
在你的 app.config 中。
For a full sample download NetworkTraffic.csand NetworkTraffic.exe.config.
如需完整示例,请下载NetworkTraffic.cs和NetworkTraffic.exe.config。
回答by Rasmus Faber
i m lookin for bytes/sec per application... not for the whole computer ....Doesnt seem to be working for a console appl... Error message:"console app does not exist in the specified Category."
我正在寻找每个应用程序的字节/秒......不是为整个计算机......似乎没有为控制台应用程序工作......错误消息:“控制台应用程序不存在于指定的类别中。”
回答by Rasmus Faber
This does not work... as far as i know bytesSentPerformanceCounter.InstanceName ="" // here you need to give network card name ...
这不起作用......据我所知 bytesSentPerformanceCounter.InstanceName ="" // 这里你需要给出网卡名称......
回答by Rasmus Faber
somehow the bytes sent is way too less than bytes received... its not that i m 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). In return i sometimes return error codes or bool... still the bytes sent is way too less than received... received is 5 times more...i m puzzled...
不知何故,发送的字节数比接收的字节数少得多......它不是我从我的应用程序浏览网络......我发送到网络服务图像(作为字节)和其他XML文件(几kbs作为网络服务的字符串输入功能)。作为回报,我有时会返回错误代码或 bool ......发送的字节仍然比接收的字节少......收到的 5 倍......我很困惑......
回答by ccook
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. All of these counters are located in Network Interface category and are named "Bytes Sent/sec", "Bytes Received/sec" and "Current Bandwidth". The only parameter that requires some extra effort to be calculated is 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. 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.
.NET 附带三个性能计数器,用于开箱即用的网络利用率公式中使用的参数。所有这些计数器都位于网络接口类别中,并命名为“发送的字节数/秒”、“接收的字节数/秒”和“当前带宽”。唯一需要额外计算的参数是 time_in_sec。
"Bytes Sent/sec" 和 "Bytes Received/sec" 计数器根据不同的样本计算它们的值,从这些计数器中获得更好值的最佳方法是在循环中找到它们值的总和,因为在某些情况下它们的值可能会与网络的真实状态为零或非常不同。然后我们可以通过查找循环迭代的次数来找到 time_in_sec 参数,因为我们的性能计数器找到了一秒的值,总时间(以秒为单位)等于迭代次数。