In the blocking mode of the last article, the server's concurrency is only a few k, and the real server like Nginx, Apache, Yumeiz Easy to handle tens of thousands of concurrent completely alone, so large concurrent circumstances can not be blocked.
1W concurrency is a separate point, if the single process model can be achieved, it means at least in the server you have been very powerful.
Server development is like a qigong, can not make a big concurrency, fault-tolerant treatment, that is, you have the internal strength, how deep the internal strength.
Asynchronous mode is specially for large concurrency, Linux under the general use of Epoll to manage the event, the following began our asynchronous large concurrent server combat it.
As with blocking development, take a look at the design process first:
1. Create the event model.
2. Create a listening connection and listen.
3. Join the listener connection to the event model.
4. When there are events, determine the type of event.
5. If the event is a listener connection, then the client connection is generated and the event model is added to the client connection to receive the send.
6. If the event is connected to the client, process the corresponding IO request.
In order to make the whole picture, I use a function (a function to write a 2W concurrent server, you tried), readability may be almost, but to deal with this interview problem is more than enough.
The actual development process is as follows:
First define an event structure to cache client connections
[CPP] View plaincopyprint?
01.struct my_event_s
02.{
- int fd;
- Char recv[64];
- Char send[64];
06.
Rc_pos int;
Sd_pos int;
09.};
struct my_event_s
{
int FD;
Char recv[64];
Char send[64];
int rc_pos;
int sd_pos;
};