【问题标题】:Get the ftp upload speed with vb.net使用 vb.net 获取 ftp 上传速度
【发布时间】:2012-03-03 22:05:32
【问题描述】:

我正在尝试使用 vb.net 获取 ftp 流的上传速度,但未成功...

我不确定数学是否正常,我在谷歌上搜索了一段时间试图找到上传的方程式,我在一些代码示例中找到了它,但要下载......

这是我的代码:

Dim chunksize As Integer = 2048
Dim offset As Long = 0
Dim readBytes As Long = 0

Dim startTime As DateTime
Dim endTime As DateTime

While offset < buffer.Length
    readBytes = fileStream.Read(buffer, 0, chunksize)
    requestStream.Write(buffer, 0, readBytes)
    offset += readBytes

    endTime = DateTime.Now
    Dim duration = endTime - startTime
    Dim inASec As Double = 1000 / duration.Milliseconds
    startTime = DateTime.Now

    RaiseEvent FileSpeed(Math.Round((64 * inASec) / 8, 2).ToString)

    RaiseEvent FileProgress(offset, buffer.Length)
End While

【问题讨论】:

    标签: vb.net upload ftp transfer ftpwebrequest


    【解决方案1】:

    我认为你的做法有点不正确。我认为通过测量已传输的字节总数然后将其除以经过的总秒数来计算总体速度会更好。

    例如,大致是这样的:

        Dim chunksize As Integer = 2048
        Dim offset As Long = 0
        Dim readBytes As Long = 0
    
        Dim startTime As DateTime
        Dim duration As Double
    
        startTime = DateTime.Now
    
        While offset < Buffer.Length
            readBytes = fileStream.Read(Buffer, 0, chunksize)
            requestStream.Write(Buffer, 0, readBytes)
            offset += readBytes
    
            duration = startTime.Subtract(Date.Now).TotalSeconds
            ' Avoid divide by 0 errors
            If duration = 0 Then
                duration = 1
            End If
    
            RaiseEvent FileSpeed(Math.Round(offset / duration, 2).ToString)
    
            RaiseEvent FileProgress(offset, Buffer.Length)
        End While
    

    【讨论】:

    • 非常感谢,正确的数学运算是 Math.Round((offset/1024) / duration, 2).ToString 以获取 kbps。
    猜你喜欢
    • 2013-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-26
    • 1970-01-01
    • 1970-01-01
    • 2013-04-05
    • 2011-06-01
    相关资源
    最近更新 更多