使用vb.net获取ftp上传速度

时间:2012-02-11 22:43:21

标签: vb.net upload ftp transfer ftpwebrequest

我试图通过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

1 个答案:

答案 0 :(得分:3)

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

例如,大致如下:

    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