如何在 C# 中计算 UDP 传输速率 (kbps)

本文关键字:传输速率 kbps UDP 计算 | 更新日期: 2023-09-27 18:30:27

首先,我知道UDP不是一个可靠的协议,但我需要在UDP中做到这一点,我知道最终可能发生的后果。

假设我有一个长byte数组,我已将其分成 32 字节的块分成一个List<byte[]>。这意味着发送的每个数据包的长度为 32 字节。

这些数据包将在foreach循环中发送,我不在乎它们是否到达目的地。 这意味着我不期望主机的确认(至少目前如此)。

我的问题是,在服务器接收数据包时,我将如何计算服务器的当前传输速率(最好以 kbps 为单位)?

我有点困惑我怎么能实现这样的计算......

如何在 C# 中计算 UDP 传输速率 (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;。每次您调用转账费率时都会计算转账率。请注意,它是字节/秒,如果你想要另一个单位,相应地除以 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";
        }
    }
}

在处理传输速率的异步环境中,对我们来说效果很好。