0

これはファイル転送です (サーバー - クライアント tcp ソケット)

以下のコードは、1 秒ごとの 1 秒あたりの転送速度 (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

次の 2 つの決定を行う必要があります。

  1. 平均転送速度をどのくらいの時間で測定しますか?
  2. どのくらいの頻度で結果を更新/報告しますか?

現在の瞬間的な転送速度などというものは存在しないことを思い出してください。または、より正確には、現在の瞬間的な転送速度は、「このマイクロ秒でパケットが送受信されている」状況と「回線アイドルです」。したがって、平均化する必要があります。

上記のコードでは、(1) と (2) の両方の値として 1 秒を選択しています。(1) と (2) が等しい場合は、コード化するのが最も簡単なケースです。

(1)については、より長い期間を選択することをお勧めします。わずか 1 秒で平均すると、最もスムーズなファイル転送を除くすべての転送速度がかなり不安定になります。たとえば、Cisco IOS のデフォルトの平均時間は 5 分を超えており、30 秒未満に設定することはできません。

(2)については、1 秒を使用し続けることも、必要に応じて 1 秒未満で使用することもできます。

(2) で選択した値の倍数である (1) の値を選択します。n(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 に答える