首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何计算网络速度?

如何计算网络速度?
EN

Stack Overflow用户
提问于 2012-05-08 21:33:12
回答 2查看 9.7K关注 0票数 3

解决:

我正在使用WCF通过流传输文件。客户端调用服务中的方法,然后服务从客户端获取文件。在路上,我正以CallBack的速度发回。

我的问题是,我无法确定我计算的速度。当服务从客户端获取文件时,它使用下载速度。但是,当客户端发送文件时,它是上传速度。我需要计算哪一个,以及如何计算?

尚未解决:

当客户端调用服务的方法(并向它提供对文件的引用的流)时,从客户端调用方法到服务的方法开始激活为止,太长(取决于文件的大小)。这一切为什么要发生?一个千兆字节的文件要花很长时间。

*从服务的方法开始,所有的事情运作良好,没有任何问题。所以展示服务是浪费时间。

(客户)

代码语言:javascript
复制
Stream TheStream = File.OpenRead(@"C:\BigFile.rar");
Service1.GiveAFile(TheStream);

谢谢。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-05-08 22:21:34

来源:如何在c#中计算网络带宽速度

代码语言:javascript
复制
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();
        }

    }
}
票数 3
EN

Stack Overflow用户

发布于 2012-05-08 22:05:51

关于第二个问题:

很可能您的服务是将整个文件加载到内存中,然后再流回客户端。

您可以查看以下问题,以了解如何正确地对其进行分组。

如何在不将整个文件加载到内存的情况下读取/流文件?

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10506837

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档