How can I rate limit an upload using TcpClient?

后端 未结 4 1373
刺人心
刺人心 2021-01-06 18:23

I\'m writing a utility that will be uploading a bunch of files, and would like to provide the option to rate limit uploads. What is the best approach for rate limiting uploa

4条回答
  •  我在风中等你
    2021-01-06 18:36

    I know this is an old entry, but i think this information can be usefull for someone who get here through google or another web search.

    If we use the solution posted by "arbiter" we will found that the thread will send a large amount of data, and then it will sleep for a large amount of time, cause the usually speed limits are over 32 to 200 kb per second while with an standar pc, the thread can manage over 10 to 100 MB per second.

    I used the next solution into my project. Note that is only a piece of code and you will have to modify it to adjust to your own. It is write in Visual basic. By the way, sorry about my english...

        Dim SpeedLimit As Long = User.DownloadKbSpeedLimit * 1024, Elapsed As Long = 0
        'Try to adjust buffersize to the operating system.
        'Seem to be stupid, but the test shows it goes better this way.
        If Environment.Is64BitOperatingSystem Then
            stream.BufferSize = 64 * 1024
        Else
            stream.BufferSize = 32 * 1024
        End If
        'If buffersize is bigger than speedlimite, cut the buffersize to avoid send too much data
        If SpeedLimit > 0 AndAlso e.BufferSize > SpeedLimit Then e.BufferSize = SpeedLimit
        'Create Byte array to send data
        Dim Buffer(e.BufferSize) As Byte
        'Create Watch to control the speed
        Dim Transmitted As Integer = 0, Watch As New Stopwatch()
        Watch.Start()
        'Start sending data
        While True
            'This enables the program to control another events or threads
            System.Threading.Thread.Sleep(10)
            Windows.Forms.Application.DoEvents()
            'Recover data and write into the stream
            If SpeedLimit = 0 OrElse Transmitted < SpeedLimit Then
                Dim Readed As Integer = SomeFileStream.Read(Buffer, 0, Buffer.Length)
                If Readed 0 Then Exit While
                Stream.Write(Buffer, Readed) 
                Transmitted += Readed
            End If
            If Watch.ElapsedMilliseconds > OneSecond Then
                Transmitted = 0
                Watch.Restart()
            End If
        End While
        Watch.Stop()
        Stream.Close() : Stream.Dispose()
    

    Hope this can help anyone. Bye.

提交回复
热议问题