首先,我知道 UDP 不是一个可靠的协议,但我需要在 UDP 中这样做,而且我知道最终可能会发生的后果。
假设我有一个长byte
数组,我已将 32 字节的块拆分为List<byte[]>
. 这意味着发送的每个数据包长度为 32 字节。
这些数据包将foreach
循环发送,我不在乎它们是否到达目的地。这意味着我不期待主机的确认(至少现在是这样)。
我的问题是,我将如何计算服务器在接收数据包时的当前传输速率(最好以 kbps 为单位)?
我对如何实现这样的计算有点困惑......
如果您的问题是“我如何计算传输速率”,您可以将下载的字节总数除以经过的总秒数。
bytes
--------- = transfer rate
seconds
在 C# 中测量时间的一个好方法是StopWatch类,由于计算机科学中的 K 是 1024(或 2^10),您可以将字节数除以 1024(或将其移位),然后除以秒数下载该数量的千字节。
如果您对平均传输率感兴趣,则需要以间隔测量下载的字节数。您可以使用二维列表来执行此操作,其中包含测量点和下载的字节以及所花费的时间。为简单起见,将其分解为进行计算的类
private readonly Stopwatch watch;
private readonly long[,] average;
public .ctor() {
// use 10 measure points, for a larger or smaller average, adjust the 10
average = new long[10, 2];
watch = Stopwatch.StartNew();
}
public long BytesTransferred {
set {
for (int i = average.GetLength(0) - 1; i > 0; --i) {
average[i, 0] = average[i - 1, 0];
average[i, 1] = average[i - 1, 1];
}
average[0, 0] = sent = value;
average[0, 1] = watch.ElapsedMilliseconds;
}
}
public long TransferRate {
get {
int l = average.GetLength(0) - 1;
double bytes = average[0, 0] - average[l, 0];
double seconds = (average[0, 1] - average[l, 1]) / 1000d;
return (long)(bytes / seconds);
}
}
在您的下载方法中,中断一个新线程,创建上述类的一个实例,并BytesTransferred = totalBytes;
在每个间隔中调用。每次调用 TransferRate 时都会计算 TransferRate。请注意,它是 bytes/s,如果您想要另一个单位,请相应地除以 1024。
我们在fireBwall 上使用一个简单的系统,每次检查时都会更新传输速率。它还存储发送的全部信息。我刚刚从 FireBwall 的 Google 代码页面复制了这个。
/// <summary>
/// Class to manage an adapters total transfered data
/// </summary>
public class BandwidthCounter
{
/// <summary>
/// Class to manage an adapters current transfer rate
/// </summary>
class MiniCounter
{
public uint bytes = 0;
public uint kbytes = 0;
public uint mbytes = 0;
public uint gbytes = 0;
public uint tbytes = 0;
public uint pbytes = 0;
DateTime lastRead = DateTime.Now;
/// <summary>
/// Adds bits(total misnomer because bits per second looks a lot better than bytes per second)
/// </summary>
/// <param name="count">The number of bits to add</param>
public void AddBytes(uint count)
{
bytes += count;
while (bytes > 1024)
{
kbytes++;
bytes -= 1024;
}
while (kbytes > 1024)
{
mbytes++;
kbytes -= 1024;
}
while (mbytes > 1024)
{
gbytes++;
mbytes -= 1024;
}
while (gbytes > 1024)
{
tbytes++;
gbytes -= 1024;
}
while (tbytes > 1024)
{
pbytes++;
tbytes -= 1024;
}
}
/// <summary>
/// Returns the bits per second since the last time this function was called
/// </summary>
/// <returns></returns>
public override string ToString()
{
if (pbytes > 0)
{
double ret = (double)pbytes + ((double)((double)tbytes / 1024));
ret = ret / (DateTime.Now - lastRead).TotalSeconds;
lastRead = DateTime.Now;
string s = ret.ToString();
if (s.Length > 6)
s = s.Substring(0, 6);
return s + " Pb";
}
else if (tbytes > 0)
{
double ret = (double)tbytes + ((double)((double)gbytes / 1024));
ret = ret / (DateTime.Now - lastRead).TotalSeconds;
lastRead = DateTime.Now;
string s = ret.ToString();
if (s.Length > 6)
s = s.Substring(0, 6);
return s + " Tb";
}
else if (gbytes > 0)
{
double ret = (double)gbytes + ((double)((double)mbytes / 1024));
ret = ret / (DateTime.Now - lastRead).TotalSeconds;
lastRead = DateTime.Now;
string s = ret.ToString();
if (s.Length > 6)
s = s.Substring(0, 6);
return s + " Gb";
}
else if (mbytes > 0)
{
double ret = (double)mbytes + ((double)((double)kbytes / 1024));
ret = ret / (DateTime.Now - lastRead).TotalSeconds;
lastRead = DateTime.Now;
string s = ret.ToString();
if (s.Length > 6)
s = s.Substring(0, 6);
return s + " Mb";
}
else if (kbytes > 0)
{
double ret = (double)kbytes + ((double)((double)bytes / 1024));
ret = ret / (DateTime.Now - lastRead).TotalSeconds;
lastRead = DateTime.Now;
string s = ret.ToString();
if (s.Length > 6)
s = s.Substring(0, 6);
return s + " Kb";
}
else
{
double ret = bytes;
ret = ret / (DateTime.Now - lastRead).TotalSeconds;
lastRead = DateTime.Now;
string s = ret.ToString();
if (s.Length > 6)
s = s.Substring(0, 6);
return s + " b";
}
}
}
private uint bytes = 0;
private uint kbytes = 0;
private uint mbytes = 0;
private uint gbytes = 0;
private uint tbytes = 0;
private uint pbytes = 0;
MiniCounter perSecond = new MiniCounter();
/// <summary>
/// Empty constructor, because thats constructive
/// </summary>
public BandwidthCounter()
{
}
/// <summary>
/// Accesses the current transfer rate, returning the text
/// </summary>
/// <returns></returns>
public string GetPerSecond()
{
string s = perSecond.ToString() + "/s";
perSecond = new MiniCounter();
return s;
}
/// <summary>
/// Adds bytes to the total transfered
/// </summary>
/// <param name="count">Byte count</param>
public void AddBytes(uint count)
{
// overflow max
if ((count * 8) >= Int32.MaxValue)
return;
count = 8 * count;
perSecond.AddBytes(count);
bytes += count;
while (bytes > 1024)
{
kbytes++;
bytes -= 1024;
}
while (kbytes > 1024)
{
mbytes++;
kbytes -= 1024;
}
while (mbytes > 1024)
{
gbytes++;
mbytes -= 1024;
}
while (gbytes > 1024)
{
tbytes++;
gbytes -= 1024;
}
while (tbytes > 1024)
{
pbytes++;
tbytes -= 1024;
}
}
/// <summary>
/// Prints out a relevant string for the bits transfered
/// </summary>
/// <returns></returns>
public override string ToString()
{
if (pbytes > 0)
{
double ret = (double)pbytes + ((double)((double)tbytes / 1024));
string s = ret.ToString();
if (s.Length > 6)
s = s.Substring(0, 6);
return s + " Pb";
}
else if (tbytes > 0)
{
double ret = (double)tbytes + ((double)((double)gbytes / 1024));
string s = ret.ToString();
if (s.Length > 6)
s = s.Substring(0, 6);
return s + " Tb";
}
else if (gbytes > 0)
{
double ret = (double)gbytes + ((double)((double)mbytes / 1024));
string s = ret.ToString();
if (s.Length > 6)
s = s.Substring(0, 6);
return s + " Gb";
}
else if (mbytes > 0)
{
double ret = (double)mbytes + ((double)((double)kbytes / 1024));
string s = ret.ToString();
if (s.Length > 6)
s = s.Substring(0, 6);
return s + " Mb";
}
else if (kbytes > 0)
{
double ret = (double)kbytes + ((double)((double)bytes / 1024));
string s = ret.ToString();
if (s.Length > 6)
s = s.Substring(0, 6);
return s + " Kb";
}
else
{
string s = bytes.ToString();
if (s.Length > 6)
s = s.Substring(0, 6);
return s + " b";
}
}
}
在处理传输速率的异步环境中对我们来说效果很好。