Follow the public account “Kite Bearer” to obtain a large number of teaching videos and private summaries of gluten and enter the professional communication group
As Node grows, its use becomes more common, especially on our front end, giving us a new lease of life. In use, Node network communication part mainly involves the transmission layer and application layer, this time to Lao Lao a common USE of TCP, UDP, HTTP protocol, through the combination of theory and practice, more clearly understand and use these protocols.
First, the transport layer
1.1 the TCP
TCP is connection-oriented and ensures the correct and sequential delivery of data from the sending process to the receiving process through flow control, sequence number, acknowledgment, and timer. TCP is divided into server and client according to whether the server listens. Let’s talk about it separately.
Read the Node API carefully, and found that it secretly fits the principle of TCP, mom no longer need to worry that I can not remember these APIS. Take a look at TCP’s three-way handshake and four-way wave.
- Three-way handshake
- TCP server process first create transmission control block TCB (thread control block), ready to accept the client process connection request, at this time the server entered the LISTEN state;
- The TCP client process also creates TCB and sends a connection request packet to the server. Then, SYN=1 in the same part of the packet header and seq= X are selected. Then, the TCP client process enters the SYN-sent state. According to TCP, the SYN segment (SYN=1) cannot carry data, but must consume a sequence number.
- After receiving the request packet, the TCP server sends an acknowledgement packet if it agrees to the connection. In the acknowledgement packet, ACK=1, SYN=1, ACK= X +1, and seq= Y are initialized. Then, the TCP server process enters the SYN-RCVD state. This message also does not carry data, but again consumes a serial number.
- The TCP client process also sends an acknowledgement to the server after receiving the acknowledgement. Confirm the ACK=1, ACK= y+1, and seq= X +1 of the packet. In this case, the TCP connection is ESTABLISHED and the client enters the ESTABLISHED state. According to TCP, AN ACK packet segment can carry data, but does not consume serial numbers if it does not.
- After receiving the confirmation from the client, the server enters the ESTABLISHED state. After that, the two sides can communicate.
- Four times to wave
- The client process sends a connection release packet and stops sending data. Release the header of the data packet, FIN=1, whose sequence number is SEq = U (equal to the sequence number of the last byte of the previously transmitted data plus 1). At this point, the client enters the fin-WaIT-1 state. According to TCP, FIN packets consume a sequence number even if they do not carry data.
- After receiving the connection release packet, the server sends an acknowledgement packet with ACK=1, ACK= U +1 and its serial number seq= V. In this case, the server enters close-wait state. The TCP server notifies the higher-level application process that the client is released from the direction of the server. This state is half-closed, that is, the client has no data to send, but if the server sends data, the client still accepts it. This state also lasts for a period of time, i.e. the duration of the close-wait state.
- After receiving the acknowledgement request from the server, the client enters the fin-WaIT-2 state and waits for the server to send a connection release packet (before receiving the final data from the server).
- After sending the LAST data, the server sends a connection release packet with FIN=1 and ACK = U +1 to the client. The server is probably in the semi-closed state. Assume that the serial number is SEQ = W, then the server enters the last-ACK state and waits for the client’s confirmation.
- After receiving the connection release packet from the server, the client sends ACK=1, ACK= W +1 and its serial number is SEq = U +1. In this case, the client enters the time-wait state. Notice That the TCP connection is not released at this time, and the client can enter the CLOSED state only after 2∗∗MSL (maximum packet segment life) and the corresponding TCB is revoked.
- The server enters the CLOSED state immediately after receiving an acknowledgement from the client. Similarly, revoking the TCB terminates the TCP connection. As you can see, the server ends the TCP connection earlier than the client.
1.1.1 TCP Server
In the above figure, careful consideration can be summarized as how to create services, service correlation, and connection correlation. And then when you correlate those events with the three handshakes and the four waves, it’s easy to see that the key processes are being monitored.
- How to create a service: TCP needs to be in listening state before establishing a connection, so this involves creating a service and then listening on a specific address
- Service correlation: Once a service is created, its state is observed and a series of processes are performed based on its state, which is what server events do.
- Connection related: the purpose of server monitoring is to establish a connection and communicate, when the connection is established, it needs to monitor the entire data transmission process, which is the connection event.
1.1.2 TCP Client
The TCP client does relatively simple things, only need to establish a connection with the corresponding server, can carry out data transmission.
1.1.3 Precautions
1.2 the UDP
A connectionless transport layer protocol that provides a transaction-oriented simple unreliable message delivery service.
Second, application layer
2.1 HTTP
HTTP is probably the most commonly used protocol in daily development. It is an application-layer protocol built on top of the TCP transport layer. Understanding this protocol is critical to the overall improvement of the front-end.
2.1.1 server
2.1.2 the client
2.1.3 Two Key objects
The IncomingMessage and ServerResponse objects are intuitively important in the use of the entire protocol, especially IncomingMessage. When we use, do not understand how to use, this time should not consider the request message and response message structure? Personally, I think its key attributes just correspond to the request message and response message.
- HTTP request packets contain the request line (method field, URI field, and protocol version), request header, and requested data (entity).
- The HTTP response packet consists of three parts: the status line (version, status code, and cause statement), the header line, and the entity.
Welcome old iron to add group or private chat