2010-02-05 7 views
4

내 응용 프로그램의 경우 사용자에게 네트워크 대역폭을 보여주고 싶습니다. 그래서, 다운로드 지연은 사용자에게 알려지게 될 것입니다. 보여줄 수 있습니까?C에서 네트워크 대역폭 측정 #

+0

@ RomanoZumbé 그 질문은 7 년이 넘었습니다. 그 당시 사람들은 upvote에 mcve를 찾지 않을 것입니다. 이것이 초기 단계입니다. 제발 사이트의 시작 단계에서 기여하고있는 사람들을 남용하지 마세요 –

+1

@SagarV 나는 아무도 "학대하지 않는다". 이 질문은 "Close Votes"리뷰 대기열에 나타났습니다. 우리의 품질 기준을 충족시키지 못하는 항목이 오늘 게시되었을 때 품질 기준을 충족하더라도 삭제해야한다고 생각합니다. 그러나 나는 그것이 왜 처음부터 위로 upvoted 얻었는지 이해합니다. –

답변

2

시도해보십시오. How to calculate network bandwidth speed in c#

enter image description here

using System; 
using System.Net.NetworkInformation; 
using System.Windows.Forms; 

namespace InterfaceTrafficWatch 
{ 
    /// <summary> 
    /// Network Interface Traffic Watch 
    /// by Mohamed Mansour 
    /// 
    /// Free to use under GPL open source license! 
    /// </summary> 
    public partial class MainForm : Form 
    { 
     /// <summary> 
     /// Timer Update (every 1 sec) 
     /// </summary> 
     private const double timerUpdate = 1000; 

     /// <summary> 
     /// Interface Storage 
     /// </summary> 
     private NetworkInterface[] nicArr; 

     /// <summary> 
     /// Main Timer Object 
     /// (we could use something more efficient such 
     /// as interop calls to HighPerformanceTimers) 
     /// </summary> 
     private Timer timer; 

     /// <summary> 
     /// Constructor 
     /// </summary> 
     public MainForm() 
     { 
      InitializeComponent(); 
      InitializeNetworkInterface(); 
      InitializeTimer(); 
     } 

     /// <summary> 
     /// Initialize all network interfaces on this computer 
     /// </summary> 
     private void InitializeNetworkInterface() 
     { 
      // Grab all local interfaces to this computer 
      nicArr = NetworkInterface.GetAllNetworkInterfaces(); 

      // Add each interface name to the combo box 
      for (int i = 0; i < nicArr.Length; i++) 
       cmbInterface.Items.Add(nicArr[i].Name); 

      // Change the initial selection to the first interface 
      cmbInterface.SelectedIndex = 0; 
     } 

     /// <summary> 
     /// Initialize the Timer 
     /// </summary> 
     private void InitializeTimer() 
     { 
      timer = new Timer(); 
      timer.Interval = (int)timerUpdate; 
      timer.Tick += new EventHandler(timer_Tick); 
      timer.Start(); 
     } 

     /// <summary> 
     /// Update GUI components for the network interfaces 
     /// </summary> 
     private void UpdateNetworkInterface() 
     { 
      // Grab NetworkInterface object that describes the current interface 
      NetworkInterface nic = nicArr[cmbInterface.SelectedIndex]; 

      // Grab the stats for that interface 
      IPv4InterfaceStatistics interfaceStats = nic.GetIPv4Statistics(); 

      // Calculate the speed of bytes going in and out 
      // NOTE: we could use something faster and more reliable than Windows Forms Tiemr 
      //  such as HighPerformanceTimer http://www.m0interactive.com/archives/2006/12/21/high_resolution_timer_in_net_2_0.html 
      int bytesSentSpeed = (int)(interfaceStats.BytesSent - double.Parse(lblBytesSent.Text))/1024; 
      int bytesReceivedSpeed = (int)(interfaceStats.BytesReceived - double.Parse(lblBytesReceived.Text))/1024; 

      // Update the labels 
      lblSpeed.Text = nic.Speed.ToString(); 
      lblInterfaceType.Text = nic.NetworkInterfaceType.ToString(); 
      lblSpeed.Text = nic.Speed.ToString(); 
      lblBytesReceived.Text = interfaceStats.BytesReceived.ToString(); 
      lblBytesSent.Text = interfaceStats.BytesSent.ToString(); 
      lblUpload.Text = bytesSentSpeed.ToString() + " KB/s"; 
      lblDownload.Text = bytesReceivedSpeed.ToString() + " KB/s"; 

     } 

     /// <summary> 
     /// The Timer event for each Tick (second) to update the UI 
     /// </summary> 
     /// <param name="sender"></param> 
     /// <param name="e"></param> 
     void timer_Tick(object sender, EventArgs e) 
     { 
      UpdateNetworkInterface(); 
     } 

    } 
} 
+0

이것이 정확한지, 전체 네트워크 어댑터 대역폭을 보여 주며, 특정 다운로드가 아닙니다. –

0

당신은 당신이 무엇을 할 수 있는지 다음 스트림을 통해 다운로드하는 것입니다 경우 스트림으로부터 상속의 요약입니다 속성을 노출 "m"의 종류를 확인하는 것입니다 읽히거나 읽힌 바이트는 기본 스트림 객체입니다. 미터기가 다운로드하고 미터기 오브젝트에서 읽는 데 사용하는 스트림 오브젝트를 캡슐화하십시오. 그러면 너무 자주 소비 된 대역폭 속도를 계산하기 위해 시간이 지남에 따라 읽은 바이트 수를 확인할 수 있습니다.

관련 문제