通过平均发送字节数的5倍来计算发送文件速度/秒 [英] calculate sending file speed/sec by taking the average of 5 times of sent bytes

查看:98
本文介绍了通过平均发送字节数的5倍来计算发送文件速度/秒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

im试图使用平均值计算每秒的传输文件速度
我每秒将发送的字节 sum prevSum 的字节数相差5次

  • 下面的代码是否给我正确的速度?
  • 我应该更改速率数组的大小吗?
  • 或者我应该更改Thread.Sleep(value)吗?
    我很困惑,因为每次改变一点点速度值都会改变..正确的解决方案是什么??

    im trying to calculate the transfer file speed per second using the average
    i took the different between the sent bytes sum and the prevSum 5 times per second

  • does the code below give me the correct speed?
  • should i change the rate array size ?
  • or should i change Thread.Sleep(value) ?
    im so confused because each time a change a little thing the speed value changes.. what's the correct solution for that ??

        static long prevSum = 0;
        static long[] rate = new long[5];
        private static void SpeedPerSec(object o)
        {
            fileProgress fP = (fileProgress)o; //get the form conrtols
            while (busy)    // while sending file is active
            {
                for (int i = 0; i < rate.Length; i++)
                {
                    //diff between the sent bytes and prev sent bytes
                    rate[i] = (sum - prevSum);
                    Thread.Sleep(1000/rate.Length);
                }
                prevSum = sum;
                fP.RateLabel(Convert.ToInt64(rate.Average()));   
                //print the trasnfer rate which take a long value .. it just print the value in MB or KB string
            }
        }
    

    这是sendFile代码:

    here is the sendFile Code :

        public static bool busy = false;
        public static Socket client;
        public static int packetSize = 1024*8;
        public static int count = 0;
        public static long fileSize;
        public static long sum = 0;
    public static void sendFile(string filePath)
        {
            // run the progres Form
            Thread thFP = new Thread(fpRUN);
            fileProgress fP = new fileProgress("Sending...");
            thFP.Start(fP);
    
            FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
            string fileName = Path.GetFileName(filePath);
            byte[] fileData;
            try
            {
                //sending file name and file size to the server
                busy = true;
                fileSize = fs.Length;
                byte[] fileDetial = null;
                string detail =  fileName + "," + fileSize.ToString();
                fileDetial = Encoding.ASCII.GetBytes(detail);
                client.Send(fileDetial);
    
                //sending file data to the server
    
                fileData = new byte[packetSize];
                count = 0;
                sum = 0;
    
                Thread thSpeed = new Thread(SpeedAndTimeLeft); //*here the thread of SPEED per second method
                thSpeed.Start(fP);
                fP.SizeLabel(fileSize);                     // tell the form the file size
                Thread thClock = new Thread(fP.clock);
                thClock.Start();
                while (sum < fileSize)
                {
                    fs.Seek(sum, SeekOrigin.Begin);
                    fs.Read(fileData, 0, fileData.Length);
                    count = client.Send(fileData, 0, fileData.Length, SocketFlags.None);
                    sum += count;
                    fP.ProgressBarFileHandler(sum,fileSize);        //progressbar value
                    fP.SentLabel(sum, fileSize);                    //tell the form how much sent                }
            }
            finally
            {
                busy = false;
                fs.Close();
                fileData = null;
                MessageBox.Show(string.Format("{0} sent successfully", fileName));
            }
        }
    

    推荐答案

    我不明白您为什么使用long []速率变量...如果要计算传输速率并每秒更新一次,则应该将当前的文件大小存储在一个变量中,然后在休眠之后查看新的文件大小.然后从新文件中减去先前的文件Sieze,您将获得最后一秒的传输速率(实时传输速率).对于一般传输速率,您应该通过下载/上传开始时的时间戳来进行计算,然后在每次睡眠后用当前文件大小除以到目前为止的总秒数来计算传输速率.

    I don't understand why you are using the long[] rate variable... If you want to calculate the transfer rate and update it each second you should store the current fileSize in a variable, and then after the sleep see the new fileSize. Then substract the previous fileSieze from the new one and you have the transfer rate for the last second (the live transfer rate). For the general transfer rate you should calculate it by taking a time-stamp when the download/upload started, and then, after each sleep calculate the rate by dividing the current fileSize with the total seconds passed so far.

    这篇关于通过平均发送字节数的5倍来计算发送文件速度/秒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆