High-performance server programming principles

The first part of Socket programming

The main principle of socket programming is to monitor http/https requests through socket system calls.

The main APIs are socket, bind, listen, accept, recv.

1. socket(int domain, int type, int protocol)

Establish a communication port through system calls, domain specifies the protocol type, PF_INET/AF_INET Ipv4 network protocol 

type data flow control protocol, such as tcp

protocol protocol number such as IPPROTO_TCP

2.bind(int socket, struct  sockaddr *addr, int addrlen)

Bind the communication port to the address and port. The address can be a certain network card address, or it can be bound to INADDR_ANY (0.0.0.0) on all network cards of the machine.

addrlen length of addr structure

3.listen(int socket, int backlog) Specifies the mode of socket communication.

Only applicable to socket type SOCK_STREAM or SOCK_SEQPACKET. The upper limit of the maximum number of backlog connections.

4.accept(int socket, struct sockaddr *clientaddr, int clientaddrlen)

Waiting to receive a client connection connection, returning a new socket. clientaddr The address of the originator of the request.

5.recv(int socket, void *buffer, int buffersize, unsigned int flags)

Read the data sent by the client to the socket into the buffer. flags specifies what to do when reading data.

6. int send(int s, const void *msg, int len, unsigned int flags) sends data from the specified socket to the other host. The parameter s is the socket for which the connection has been established.

The second part I/O multiplexing

After receiving the client's request through accept, read the requested data and do the corresponding processing. At this time, other links need to wait. In order to improve the response efficiency, it is necessary to hand over the request processing work to other processes after receiving the client's request, and the main process continues to respond to subsequent requests. The kqueue model is used to illustrate.

1.kqueue() creates a queue

2.struct event structure

Different descriptor types have different filter types.

ident descriptor

events on filter descriptors 

flags event handling action add/delete

fflags is related to descriptors

3. ENV_SET macro for initialization event

4.kevent($k, struct event *changeEventList,  nchangeEventList,  struct event *firedEventList,  nfiredEventList, struct timestemp *time)

The third part is the combination of Socket and I/O multiplexing

First create a socket, put the socket into kq, when a link arrives, kevent will get a triggered event, in which there is a descriptor of the socket, through the accept method, use the new socket to take over the request, and put The new socket is also put into kq, so that when the new socket processes user requests, the main socket can receive new requests at the same time.

TCP listening socket, if there is data in the completed connection queue (received the last ACK of the three-way handshake), this event will be notified. The application that receives the notification generally calls accept(), and the number of nodes in the completion queue can be obtained through data. Stream or datagram socket, when the socket layer of the protocol stack receives data in the buffer, the event will be notified, and data is set to the number of bytes of readable data.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324516715&siteId=291194637