0

这是文件传输(服务器-客户端 tcp 套接字)

下面的代码显示每秒传输速率 (kb/s)。

rate/s每次向客户端发送数据时,我都想显示速度( )。如何每次计算速度(不使用 usings thread.sleep(1000))?

private void timeElasped()
    {
        int rate = 0;
        int prevSent = 0;
        while (fileTransfer.busy)
        {
            rate = fileTransfer.Sent - prevSent ;
            prevSum = fileTransfer.Sent;
            RateLabel(string.Format("{0}/Sec", CnvrtUnit(rate)));
            if(rate!=0)
                Timeleft = (fileTransfer.fileSize - fileTransfer.sum) / rate;
            TimeSpan t = TimeSpan.FromSeconds(Timeleft);
            timeLeftLabel(FormatRemainingText(rate, t));
            Thread.Sleep(1000);
        }
    }
4

2 回答 2

1

你有两个决定要做:

  1. 您希望在多长时间内采用平均传输速度?
  2. 您希望多久更新/报告一次结果?

回想一下,没有当前瞬时传输速度之类的东西。或者,更准确地说,当前瞬时传输速度始终是网络接口的全物理速度(例如 100 Mbps)或零,对应于“在这一微秒内有一个数据包正在发送/接收”和“线路空闲”。所以你必须平均。

在上面的代码中,您选择了一秒作为 (1) 和 (2) 的值。(1) 和 (2) 相等是最简单的编码情况。

我建议您为 (1) 选择更长的时间段。除了最流畅的文件传输之外,平均仅超过一秒钟将使所有传输速度都变得非常不稳定。例如,考虑 Cisco IOS 默认情况下平均超过 5 分钟,并且不允许您配置少于 30 秒。

对于 (2),您可以继续使用 1 秒,或者,如果您愿意,甚至可以少于 1 秒。

为 (1) 选择一个值,该值是您为 (2) 选择的值的倍数。让nbe (1) 除以 (2)。例如,(1) 是 10 秒,(2) 是 500 毫秒,而n=20.

创建一个带有n条目的环形缓冲区。每次 (2) 过去后,将环形缓冲区中最旧的条目替换为自上次时间 (2) 过去以来传输的字节数,然后重新计算传输速度,即缓冲区中所有条目的总和除以 (1) .

于 2012-01-24T13:52:52.613 回答
0

在表单构造函数中

Timer timer1 = new Time();
public Form1()
{
    InitializeComponent();
    this.timer1.Enabled = true;
    this.timer1.Interval = 1000;
    this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
}

或从工具箱中添加并设置以前的值

发送字节的总和应该是公开的,因此我们的方法可以每秒获取它的值

long sentBytes = 0;      //the sent bytes that updated from sending method
long prevSentBytes = 0;   //which references to the previous sentByte
double totalSeconds = 0;   //seconds counter to show total time .. it increases everytime the timer1 ticks.
private void timer1_Tick(object sender, EventArgs e)
{
    long speed = sentBytes - prevSentBytes ;  //here's the Transfer-Rate or Speed
    prevSentBytes = sentBytes ;
    labelSpeed.Text = CnvrtUnit(speed) + "/S";   //display the speed like (100 kb/s) to a label
    if (speed > 0)                //considering that the speed would be 0 sometimes.. we avoid dividing on 0 exception
    {
        totalSeconds++;     //increasing total-time
        labelTime.Text = TimeToText(TimeSpan.FromSeconds((sizeAll - sumAll) / speed));
        //displaying time-left in label
        labelTotalTime.Text = TimeToText(TimeSpan.FromSeconds(totalSeconds));
        //displaying total-time in label
    }
}

private string TimeToText(TimeSpan t)
{
    return string.Format("{2:D2}:{1:D2}:{0:D2}", t.Seconds, t.Minutes, t.Hours);
}

private string CnvrtUnit(long source)
{
    const int byteConversion = 1024;
    double bytes = Convert.ToDouble(source);

    if (bytes >= Math.Pow(byteConversion, 3)) //GB Range
    {
        return string.Concat(Math.Round(bytes / Math.Pow(byteConversion, 3), 2), " GB");
    }
    else if (bytes >= Math.Pow(byteConversion, 2)) //MB Range
    {
        return string.Concat(Math.Round(bytes / Math.Pow(byteConversion, 2), 2), " MB");
    }
    else if (bytes >= byteConversion) //KB Range
    {
        return string.Concat(Math.Round(bytes / byteConversion, 2), " KB");
    }
    else //Bytes
    {
        return string.Concat(bytes, " Bytes");
    }
}
于 2012-03-05T21:11:01.823 回答