Use C ++ to implement HTTP server-how to control the sending bandwidth of a socket

Source: Internet
Author: User
Use C ++ to implement HTTP server-how to control the sending bandwidth of a socket
Author: que rongwen
Date:

Purpose
Limit the sending bandwidth for each client socket on the HTTP server.

Prerequisites
Use IOCP to manage sockets. The thread cannot be blocked and other connections are affected.

How to Implement

Idea 1. When WSASend () is called each time, calculate the current bandwidth. If the limit is exceeded, only one minimum package is sent. For example, if one byte is used, the bandwidth is reduced to a proper value.
Advantages: simple and easy to implement.
Disadvantage: in addition to being unable to accurately control the sending bandwidth, this method has another big problem: even if only one byte of data is sent each time WSASend () is called, when the network conditions are good, the sending speed can still reach about 30 kb/s (I tested the results with Que's HTTP Server on the local machine for reference only ), that is, the minimum bandwidth can only be controlled at 30 kb/s, which is of little practical significance.

Idea 2. When WSASend () is called each time, if the speed is found to exceed the limit, the time point for the next data transmission will be calculated and then the data will be sent in a delayed manner.
Assume that the number of bytes sent is BytesSent, the maximum speed is MaxSpeed, and the time when the socket starts to send data is StartTime. The next time the data packet is sent, NextSendTime = BytesSent/MaxSpeed + StartTime. view the current time CurTime each time you call WSASend (). If CurTime <NextSendTime indicates that the speed is too high, a timer is set to be triggered after the time interval (NextSendTime-CurTime, continue to send the next package. if the speed limit is not exceeded, the processing is normal.
Advantage: You can precisely control the bandwidth of any size.
Disadvantage: timer support is required, which consumes more resources and makes programming more complex. synchronization is required. if the value of (NextSendTime-CurTime) is too long, the client may think that the server does not respond and the connection is disconnected, affecting normal services.
To overcome the above disadvantages, we should set a maximum latency value MaxDelay. When (NextSendTime-CurTime)> MaxDelay, the timer timeout value is set to MaxDelay, and after the timer is triggered, send only one minimum package to keep the connection active. taking Que's HTTP Server as an example, MaxDelay is set to 2 seconds. If the delay sending time (NextSendTime-CurTime) is greater than 2 seconds, a timer is set to be triggered after 2 seconds, sends a 512-byte packet. in this way, we can conclude that the theoretical minimum bandwidth limit of the Que's HTTP Server is 256B/s. as an HTTP server, this value is sufficient. If you need to limit a smaller bandwidth, you can reduce the value of MinDataLength or increase the value of MaxDelay. for example, if you set MaxDelay = 5S, MinDataLength = 10 Bytes, the minimum bandwidth is 2 B/s.

Conclusion
The second method can effectively and accurately control the sending bandwidth, which is also used by the Que's HTTP Server.

Click to view more details about the Que's HTTP Server.

----

----

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.