2012-05-08 148 views
2

已解決:如何計算網絡速度?

我正在使用WCF通過流式傳輸文件。客戶端在服務中調用方法,然後服務從客戶端獲取文件。在途中,我通過CallBack發回速度。

我的問題是,我無法確定我計算的是哪種速度。當服務從客戶端獲取文件時,它使用下載速度。但是,當客戶端發送文件時,它是上傳速度。我需要計算哪一個,以及如何計算?

沒有解決尚未:

當客戶端調用服務的方法(和用參考文件給它的流),它需要TOO長(取決於的大小文件)從客戶端調用方法開始直到服務的方法開始激活。爲什麼會發生?一個千兆字節的文件將永遠佔用。

*從服務的方法開始的時候,所有的東西都可以正常工作,沒有問題。因此,展示服務是浪費時間。

(客戶端)

Stream TheStream = File.OpenRead(@"C:\BigFile.rar"); 
Service1.GiveAFile(TheStream); 

感謝。

+0

下載速度和上傳速度是對於任何給定傳輸(忽略緩衝,當然)是相同的。 –

+1

你正在計算的速度是*最慢的速度*(如果我們正在談論帶寬)。在DSL線路上,這通常是上傳速度。傳輸速率永遠不會超過最慢的上傳/下載速度。 –

+0

在這種情況下,它們不是一回事?必須有一個。 –

回答

3

來源:How to calculate network bandwidth speed in c#

CODE: 
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

我使用此代碼並在Windows Server 2012上進行測試。應用程序發送/接收速度爲13/3 kb/s,但服務器的任務管理器顯示爲136/24。即比例相同但價值不同。爲什麼? – Oleg

+1

@Oleg你需要將你的代碼中的值乘以8 – ArcadeRenegade

0

對於第二個問題:

最有可能您的服務加載整個文件到內存流回客戶端之前。

你可以看看下面的問題,以瞭解如何正確分塊。

How can I read/stream a file without loading the entire file into memory?

+0

謝謝你嘗試,但**這不是幫助**。該主題的解決方案完全是**我所實施的。我給了我的客戶的代碼,說明他如何將流發送到服務。還有其他選擇嗎? –

+1

使用response.transmitfile():http://msdn.microsoft.com/en-us/library/12s31dhy(v=vs.100).aspx而不是發送流到您的服務,只要給它的文件參考和告訴使用傳輸文件發送它。 TransmitFile不會將其緩存在內存中。 – NotMe

+0

你能給我更多關於你的解決方案的信息嗎? –