2012-02-11 84 views
0

我试图让与vb.net失败一个ftp流的上传速度的FTP上传速度...获取与vb.net

我不知道如果数学都OK,我用Google搜索一会儿试图找到上传的方程,我发现它在一些代码示例,但下载...

这里是我的代码:

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 

回答

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 
+0

非常感谢,正确的数学是Math.Round((偏移/ 1024)/持续时间,2)的ToString得到kbps的。 – 2012-02-12 00:32:17