A rough review of computer networks

Network hierarchy

Insert picture description here

First of all, it is necessary to connect two machines to complete the bit stream transmission, which is the function of the physical layer .
Then, it is inevitable that errors will be encountered when the two machines transmit bit streams, so the data link layer came into being. The data link layer provides error detection, data correction, and defines how to format the data for transmission. The road layer converts the bit stream into a frame, and the switch works at this layer.
With the continuous increase of network nodes, multiple nodes need to pass through when point-to-point communication. In order to find the target node and find the best path, there is a network layer .
With the further expansion of network communication requirements, massive amounts of data need to be transmitted during the communication process, so the transport layer appears. The transport layer solves the data transmission between hosts and solves the problem of transmission quality. This layer is the most important layer in the OSI model. When transmitting big data, cut the big data into small pieces of data, and then arrange a serial number for each piece of data so that when the data reaches the transmission layer of the receiving node, it can be reorganized in the correct order. This process is called Sort.
In order to improve the user experience, there is the session layer , which can automatically send and receive packets and automatically address the function. The role of the session layer is to establish and manage communication between applications.
When two systems have inconsistent grammars and want to communicate, a presentation layer is needed . At the presentation layer, data will be formatted according to a scheme that the network can understand.
At this time, although the sender knows the content and length of the transmission, the receiver does not know at all, so the application layerWas born. It stipulates that the sender and receiver must use a fixed-length message header, the message header must use a certain fixed composition, and a series of information such as the length of the message body must be recorded in the message header, so that the receiver can correctly analyze and send Data sent by the party. The application layer is designed to make it easier for customers to apply the data received from the network. Data transmission can be transmitted without this layer, but it is a bunch of binary byte arrays that are passed back and forth.
Data transmission process:
Insert picture description here
Insert picture description here


TCP protocol (Transmission Control Protocol)

The TCP protocol is a connection-oriented, reliable, byte stream-based transport layer communication protocol that
divides the data stream of the application layer into segments and sends them to the TCP layer of the target node. Each data packet has a sequence number. The other party will return the ACK confirmation flag if received, and retransmit if not received.
Use parity check sum to check the correctness of data
TCP flag : (a total of eight, here are six commonly used)
URG: emergency pointer flag
ACK: confirmation sequence number flag
PSH: push flag (indicating that the receiver contains this flag When the data of the segment data, the segment should be delivered to the application as soon as possible, rather than queued in the buffer)
RST: reset the connection flag reset
SYN: synchronization sequence number, used to establish the connection process
FIN: finish flag, used Release the connection
Sliding window: Used to inform the sender and the buffer size of the receiver to control the rate at which the sender sends data to achieve flow control.
Insert picture description here

The network layer contains the IP address (IP address can uniquely identify the host), the port number and protocol contained in the transport layer (such as: TCP protocol, TCP protocol + port number can uniquely identify a process in the host)

TCP's three-way handshake and four waved hands
Serial number: seq, occupies 32 bits, used to identify the byte stream sent from the sender to the receiver.
Acknowledgement number: ack, occupies 32 bits, only when the ACK flag is 1, the acknowledgment sequence number field is valid, ack=seq+1.
Flag bit:
SYN: initiate a new connection.
FIN: Release a connection.
ACK: Confirm that the serial number is valid.
Insert picture description here


Wave four times:
Insert picture description here

Why wait for 2MSL? (MSL means the longest lifespan of the message)
Answer: To prevent packet loss during the fourth wave, if the server does not receive the content of the fourth message, the server will resend the content of the third wave. When the client receives the third wave of the message in 2MSL, the TIME-WAIT will be cleared, and the client sends the fourth wave of the message again.
Insert picture description here


UDP (User Datagram Protocol)

UDP header structure: source port, destination port, packet length, parity check sum, user data
Insert picture description here

Features: non-connection-oriented; does not maintain the connection state, supports the transmission of the same message to multiple clients at the same time; the packet header is only 8 bytes, and the extra overhead is small
. The difference between TCP and UDP:
tcp is a connection-oriented byte stream with Reliability, orderliness, slower speed, heavyweight (reflected in the size of the message header: 20 bytes);
udp is oriented to connectionless datagrams, unreliable, disorderly, faster, and lightweight (Reflected in the size of the message header: 8 bytes);

TCP sliding window

RTT (Round Trip Time): The time it takes to send a data packet to the receipt of the corresponding ACK
RTO (Retransmission TimeOut): Retransmission time interval (After tcp sends a data packet, a retransmission timer is started, and RTO is this Timed time, RTO is calculated based on RTT)
TCP uses sliding windows for flow control rearrangement, that is, sliding windows ensure the reliability of TCP and ensure the flow control characteristics of TCP.
The most basic transmission reliability of TCP comes from the confirmation retransmission mechanism, and the reliability of the TCP sliding window is also based on the confirmation retransmission.
Insert picture description here

LastByteAcked: the last byte of the ack response that has been sent and received
LastByteSent: the last byte of the ack response that has been sent but has not been received
LastByteWritten: the last byte that has not been sent but can be sent;
LastByteRead: has been accepted and The last byte of the ack has been replied
NextByteExpected: the position of the largest continuous sequence received
LastByteRcvd: the last byte of the ack that has been accepted but has not been replied;

AdvertisedWindow = MaxRcvBuffer-(LastByteRcvd-LastByteRead), MaxRcvBuffer is the maximum amount of data that the receiver can receive, that is, the size of the receiver's buffer pool. According to the above data, the amount of data that the current receiver can still receive AdvertisedWindow can be calculated, and then this data can be told to the sender to calculate the current data size that can be sent, namely EffectiveWindow = AdvertisedWindow-(LastByteSent-LastByteAcked )
Insert picture description here

Only when 32~35 are confirmed, the sliding window will slide to the position starting with 36.
Insert picture description here

The receiver has three states: accepted and responded to ACK, not received but ready to receive, and unreceivable. The size of the data that has not been received but is ready to be received is the size of the receiving window. PS: There is no state of receiving but not replying ACK, because reply ACK is TCP stack reply, there is no delay.

HTTP protocol (hypertext transfer protocol)

HTTP is a stateless protocol based on request and response mode, often based on TCP connection.
Main features:
support client/server mode: the client sends a request to the server through the URL, and the server sends a response message to the client is
simple and fast: when the client requests a service from the server, only the request method (GET, POST, HEAD) needs to be transmitted And the path is
flexible: HTTP allows any type of data object to be passed in, and the type being transmitted is marked by the ContentType.
Connectionless: Connectionless meaning: limit each connection to process only one request, the server processes the client's request, and receives the client's request The connection is disconnected after the response.
Stateless: the protocol has no memory of the transaction processing.
If you want to keep the connection for a period of time, you need to add Connection: keep-alive to the http request header. HTTP1.1 defaults to a long connection
Insert picture description here

HTTP request/response steps:
1. The client connects to the web server
2. Sends an http request
3. The server receives the request and returns an HTTP response
4. Releases the TCP connection
5. The client browser parses the HTML content and
enters the URL in the browser address bar , The process experienced after pressing Enter:
1. DNS resolution: the server first resolves the ip address corresponding to the domain name in the url
2. Establishes a TCP connection: establishes a TCP connection based on the IP address and port
3. Sends HTTP requests
4. Server processing Request and return an HTTP message
5. The browser parses and renders the page
. 6. The connection ends.
HTTP status code
1xx: Indication message-indicates that the request has been received, continue processing
2xx: Success-indicates that the request has been successfully received, understand and process
3xx: Redirect – To complete the request must be further operations
4xx: client error – the request has a syntax error or the request cannot be fulfilled
5xx: server-side error – the server failed to fulfill the legal request
Insert picture description here

The difference between GET request and POST request:
Http message level: GET request information is placed in the URL, and POST is placed in the body of the message.
Database level: GET request conforms to idempotence (the result of one or more operations on the database is consistent) and security (the operation on the database does not change the data in the database), POST does not comply, because POST is generally used to create new Resources, so it will change the data in the database (not in compliance with security), and every post request will add a new resource (not in compliance with idempotence), the biggest difference between put and post methods is also this, put method is consistent Idempotent.
Other aspects: GET can be cached and stored, but POST cannot.
The difference between cookie and session:
cookie is stored on the client side, session stored on the server side,
session is more secure than cookie, because users can easily obtain cookie information for analysis.
If you consider reducing server performance, you should use cookie
Insert picture description here

If the browser does not support cookies or the user has blocked all cookies, JSESSIONID can be appended to all URLs in the HTML page, and these pages will be sent to the client as a response. In this way, the client clicks on any link to bring the JSESSIONID back to the server, which is the URL writeback. And tomcat uses these two mechanisms at the same time to realize
the difference between HTTP and HTTPS for session :
1. HTTPS needs to apply for a certificate from CA, HTTP does not need
2. HTTPS ciphertext transmission, HTTP plaintext transmission
3. Different connection methods, HTTPS is used by default 443 port, HTTP uses 80 port
4. HTTPS = HTTP + encryption + certificate + integrity protection, more secure than HTTP
Insert picture description here

SSL (Security Sockets Layer, Secure Sockets Layer) is a security protocol that provides security and data integrity for network communication. It is an external API of the operating system. After SSL3.0, it was renamed to TLS, which uses identity verification and data encryption to ensure network communication. Security and data integrity.
-------------------------------Socket------------------ -----------------
Insert picture description here

Guess you like

Origin blog.csdn.net/qq_41962339/article/details/113729257