2

現在のダウンロード速度を測定したい。TCP経由で巨大なファイルを送信しています。毎秒転送速度を取得するにはどうすればよいですか?IPv4InterfaceStatisticsまたは同様の方法を使用する場合、ファイル転送速度をキャプチャする代わりに、デバイス転送速度をキャプチャします。デバイス転送速度のキャプチャに関する問題は、転送する単一のファイルではなく、ネットワークデバイスを介して進行中のすべてのデータをキャプチャすることです。

ファイル転送速度を取得するにはどうすればよいですか?私はc#を使用しています。

4

1 に答える 1

2

ストリームを制御して読み取り量を通知することはできないため、ストリームの読み取りの前後にタイムスタンプを付けて、受信または送信されたバイトに基づいて速度を計算できます。

using System.IO;
using System.Net;
using System.Diagnostics;

// some code here...

Stopwatch stopwatch = new Stopwatch();

// Begining of the loop

int offset = 0;
stopwatch.Reset();
stopwatch.Start();

bytes[] buffer = new bytes[1024]; // 1 KB buffer
int actualReadBytes = myStream.Read(buffer, offset, buffer.Length);

// Now we have read 'actualReadBytes' bytes 
// in 'stopWath.ElapsedMilliseconds' milliseconds.

stopwatch.Stop();
offset += actualReadBytes;
int speed = (actualReadBytes * 8) / stopwatch.ElapsedMilliseconds; // kbps

// End of the loop

を入れてStream.Readtry/catch読み取り例外を処理する必要があります。ストリームへの書き込みと速度の計算についても同じですが、影響を受けるのは次の2行だけです。

myStream.Write(buffer, 0, buffer.Length);
int speed = (buffer.Length * 8) / stopwatch.ElapsedMilliseconds; // kbps
于 2011-01-09T21:38:35.597 に答える