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

时间:2012-04-07 23:46:06

标签: c# sockets udp transfer rate

首先,我知道UDP不是一个可靠的协议,但我需要在UDP中使用它,并且我知道最终可能发生的后果。

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

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

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

我对如何实现这样的计算感到有点困惑......

2 个答案:

答案 0 :(得分:3)

如果您的问题是“如何计算传输速率”,则可以将下载的字节总数除以通过的总秒数。

   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.

答案 1 :(得分:1)

我们在fireBwall上使用一个简单的系统,每次检查时都会更新传输速率。它还存储发送的总信息。我刚从fireBwall的Google Code页面复制了这个。

/// <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";
        }
    }
}

在处理传输速率的异步环境中,我们可以很好地工作。