2012-05-08 151 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

你能给我更多关于你的解决方案的信息吗? –