首頁>Program>source

我是否可以通過網路通過exe /應用程式計算頻寬(發送和接收的資料包)? 已经研究了 IPGlobalProperties 和其他類別

我希望單个應用程式發送和接收資料包.我已经檢查過 http://netstatagent.com/ ,並且需要類似的东西。

.Net中有什麼可以帮助我的吗?

我的應用程式連線到web服務以發送和接收一些圖像檔案。

最新回復
  • 5月前
    1 #

    一種方法是檢索效能計數器" .NET CLR網路/接收的位元組數"和" .NET CLR"的值.您的應用程式的"網路/發送的位元組數":

    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 (您將获得InvalidOperation:例項" XXX"在指定的類別中不存在)並且需要插入

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

    在您的app.config中。

    要获取完整的示例下載,請下載NetworkTraffic.cs和NetworkTraffic.exe.config。

  • 5月前
    2 #

    我記得曾经讀過一篇有關此內容的文章,並為您进行了挖掘:http://nayyeri.net/blog/ 如何在網路中計算網路使用率/

    代碼前的摘錄:

    .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.

    p

    im查詢每个應用程式的位元組/秒...不適用於整个計算機...。似乎不適用於控製台應用程式...錯誤訊息:"控製台應用程式在指定類別中不存在. "

  • 5月前
    3 #

    据我所知這不起作用... bytesSentPerformanceCounter.InstanceName ="" //在這裏,您需要提供網卡名稱...

  • 5月前
    4 #

    以某種方式發送的位元組數比接收到的位元組數要少得多...不是我从我的應用程式瀏覽網路 ...我將圖像(以位元組為單位)和其他XML檔案(少量kbs作為web服務功能的字元串輸入)發送到web服務.作為迴報,我有時会返迴錯誤代碼或bool ...仍然發送的位元組远远少於接收的位元組...接收的位元組多5倍...我很困惑...

  • 如何使用Google Maps android API v2获取"我的位置更改"事件?
  • 如何以程式設計方式禁用C#控製台應用程式的快速編輯模式?