C++ implements millions of concurrent reactor servers from 0.
Down|Planting uri":
There are many basic concepts that must be understood in network programming, such as OSI, TCP IP, byte sequence, etc., which are the basis for developing network applications, which can help us better understand the working principle of network programs and learn some important concepts together.
osiopen system interconnection) was developed by the International Organization for Standardization (ISO).The standard of computer interconnection is the basic framework for designing and describing computer network communication。The OSI model divides network communication into 7 layers: TCP IP protocol without presentation layer and session layer, and each layer corresponds to the OSI protocol mainly as follows:
Application layer: TFTP, HTTP, FTP, etc.
Transport layer: TCP, UDP
Network layer: IP, IGMP, etc.
Data layer: MTU, etc.
Physical layer: ISO 2110, IEEE802, etc.
TCP IP contains many protocols, such as TCP, UDP, IP, HTTP, FTP, etc., which are commonly used on the Internet, and some concepts must be understood
Transport Control Protocol (TCP): a transmission control protocol
UDP (User Datagram Protocol): a user datagram protocol
InternetWorking Protocol (IP): an inter-network protocol
HyperText Transfer Protocol (HTTP): Hypertext Transfer Protocol
FTP (File Transfer Protocol): a text transfer protocol.
Before I improve the network IO model, let me ask a question, do you know how many clients can be connected to a single server theory?
Believe you know that the TCP connection is uniquely confirmed by a quadruple, and that quadruple is:Local IP, Local Port, Peer IP, Peer Port
As a service provider, the server usually listens on a port locally and waits for the client to connect. Therefore, the local IP and port of the server are fixed, so only the peer IP and port will change for the quadruple of the server-side TCP connectionMaximum TCP Connections = Number of Client IPs Number of Client Ports
For IPv4, the number of IPs on the client is up to 2 to the power of 32, and the number of ports on the client is up to 2 to the power of 16, that isThe maximum number of TCP connections on a single server is about 2 to the 48th power
This theoretical value is quite "full", but the server certainly can't carry that large number of connections, and it will be limited by two main aspects:
File descriptor, the socket is actually a file, which corresponds to a file descriptor. Under Linux, there is a limit to the number of file descriptors that can be opened by a single process, and the unmodified value is generally 1024, but we can increase the number of file descriptors by ulimit;
System memoryEach TCP connection has a corresponding data structure in the kernel, which means that each connection will occupy a certain amount of memory;
If the server has only 2 GB of memory and the network card is gigabit, can it support 10,000 concurrent requests?
Concurrent 10,000 requests is the classic C10k problem, where C is the acronym for Client and C10K is the problem of a single machine processing 10,000 requests at the same time.
From the perspective of hardware resources, for a server with 2GB memory Gigabit network card, if each request processing occupies less than 200KB of memory and 100KB of network bandwidth, it can meet 10,000 concurrent requests.