what the fuck! The first time I saw a big guy explaining TCP / IP three times shaking hands and four times waving hands

Time:2020-11-3

what the fuck! The first time I saw a big guy explaining TCP / IP three times shaking hands and four times waving hands
Original text: https://blog.csdn.net/ThinkWo…

Computer network architecture

In the basic concept of computer network, hierarchical architecture is the most basic. There are many abstract concepts in computer network architecture, so we should think more when we study. These concepts are very helpful for later study.

What is a network protocol?

In order to exchange data orderly in the computer network, we must abide by some rules agreed in advance, such as the format of data exchange, whether to send a response information. These rules are called network protocols.

Why layer network protocols?
  • Simplify the difficulty and complexity of the problem. Because the layers are independent, we can divide big problems into small ones.
  • Good flexibility. When the technology of one layer changes, the other layers will not be affected as long as the interface relationship between layers remains unchanged.
  • Easy to implement and maintain.
  • Promote standardization. After separation, the functions of each layer can be described relatively simply.

The disadvantages of network protocol layering are as followsFunctionality may appear in multiple layers, creating additional overhead.

In order to interconnect computer networks with different architectures, ISO proposed a standard framework in 1977, which is the famous Open Systems Interconnection Basic reference model (OSI / RM).

The concept of OSI’s seven layer protocol architecture is clear and its theory is relatively complete. However, it is complex and not practical. TCP / IP architecture is different, but it has been widely used. TCP / IP is a four layer architecture, which includes application layer, transport layer, Internet layer and network interface layer (the name of Internet layer is to emphasize that this layer is to solve the interconnection problem of different networks), but in essence, TCP / IP There are only the top three layers. Because there is no specific content in the bottom layer of network interface, the compromise method is often adopted when learning the principles of computer network, that is, integrating OSI and TCP / IP In this way, the concept can be explained clearly. Sometimes, for convenience, the bottom two layers can be called network interface layer.

The relationship among four layer protocol, five layer protocol and seven layer protocol is as follows:

  • TCP / IP is a four layer architecture, including application layer, transport layer, Internet layer and network interface layer.
  • The architecture of five layer protocol includes application layer, transport layer, network layer, data link layer and physical layer.
  • OSI seven layer protocol model includes application layer, presentation layer, session layer, transport layer, network layer, data link layer and physical layer.

Note: the architecture of five layer protocol is only designed to introduce the network principle, and the practical application is TCP / IP four layer architecture.

TCP / IP protocol family

application layer

The task of application layer is to complete specific network application through the interaction between application processes. Application layer protocol defines the rules of communication and interaction between application processes (process: the program running in the host).

Different application layer protocols are needed for different network applications. There are many application layer protocols in the Internet, such as DNS, HTTP protocol supporting world wide web application, SMTP protocol supporting e-mail, etc.

Transport layer

The main task of transport layer is to provide common data transmission service for communication between two host processes. The application process uses this service to transmit application layer messages.

The transport layer mainly uses the following two protocols

  1. Transmission control protocol – TCP: provides connection oriented, reliable data transmission service.
  2. User data protocol – UDP: provides connectionless, best effort data transmission service (does not guarantee the reliability of data transmission).

what the fuck! The first time I saw a big guy explaining TCP / IP three times shaking hands and four times waving hands

Each application layer protocol (the highest layer of the TCP / IP reference model) generally uses one of two transport layer protocols:

Protocols running on TCP protocol:

  • HTTP (Hypertext Transfer Protocol) is mainly used for general browsing.
  • HTTP (HTTP over SSL), the secure version of HTTP protocol.
  • FTP (File Transfer Protocol) is used for file transfer.
  • POP3 (post office protocol, version 3, post office protocol).
  • SMTP (Simple Mail Transfer Protocol) is used to send e-mail.
  • Telnet (teletype over the network) logs in to the network through a terminal.
  • SSH (secure shell, used to replace telnet with poor security), is used to encrypt secure login.

Protocols running on UDP protocol:

  • BOOTP (boot protocol) is applied to diskless devices.
  • NTP (Network Time Protocol) is used for network synchronization.
  • DHCP (Dynamic Host Configuration Protocol), dynamic configuration of IP address.

Running on TCP and UDP protocols:

  • DNS (domain name service) is used to complete address search, mail forwarding, etc.
network layer

The task of the network layer is to select the appropriate network routing and exchange nodes to ensure the timely transmission of computer communication data. When sending data, the network layer transmits the packet and packet of the packet or the encapsulation component of the user datagram generated by the transport layer. In TCP / IP architecture, because the network layer uses IP protocol, packet is also called IP datagram.

The Internet is connected by a large number of heterogeneous networks through routers. The network layer protocol used by the Internet is the internetprotocol and many routing protocols, so the network layer of the Internet is also called the Internet layer or IP layer.

data link layer

Data link layer is usually referred to as link layer. The data transmission between two hosts is always transmitted on one link after another, which requires the use of special link layer protocol.

When transmitting data between two adjacent nodes, the data link layer assembles the IP datagrams handed over by the network layer into frames, and transmits frames on the link between two adjacent nodes. Each frame includes data and necessary control information (such as synchronization information, address information, error control, etc.).

When receiving data, the control information enables the receiver to know which bit a frame starts from and ends at.

The general communication flow of web application is as follows:
what the fuck! The first time I saw a big guy explaining TCP / IP three times shaking hands and four times waving hands

When transmitting data between layers, the sender will be marked with the first information of the layer when it passes through a layer. On the contrary, when the receiver transmits data between layers, the corresponding header information will be removed every time the receiver passes through a layer.

physical layer

The unit of data transmitted on the physical layer is bits. The role of physical layer is to realize the transparent transmission of bit stream between adjacent computer nodes, and shield the differences between specific transmission media and physical devices as much as possible. The data link layer above it does not need to consider the specific transmission medium of the network. “Transparent transport bit stream” means that the bit stream transmitted by the actual circuit has not changed. To the transmitted bit stream, the circuit seems invisible.

TCP / IP protocol family

Among the various protocols used in the Internet, the most important and famous are TCP / IP. Now people often refer to TCP / IP not only TCP and IP, but also the whole TCP / IP protocol family used by the Internet.

what the fuck! The first time I saw a big guy explaining TCP / IP three times shaking hands and four times waving hands

Internet Protocol Suite (IPS) is a network communication model and a whole network transmission protocol family, which is the basic communication architecture of Internet. It is often referred to as the TCP / IP protocol suite, or TCP / IP protocols for short. Because the two core protocols of the protocol family, TCP (transmission control protocol) and IP (Internet Protocol), are the earliest standards adopted in this family.

Key points:

TCP (transmission control protocol) and IP (Internet Protocol) are the first two core protocols defined, so they are collectively referred to as TCP / IP protocol family

Three handshakes and four waves of TCP

TCP is a connection oriented, reliable and byte stream based transport layer communication protocol. Before sending data, both sides of communication must establish a connection between each other. The so-called “connection” is actually a piece of information about each other, such as IP address, port number, etc. kept by the client and the server.

TCP can be regarded as a kind of byte stream, which can deal with packet loss, duplication and error in IP layer or below. In the process of connection establishment, both sides need to exchange some connection parameters. These parameters can be placed in the TCP header.

A TCP connection consists of four tuples, which are two IP addresses and two port numbers. A TCP connection is usually divided into three stages: connection, data transmission, and exit (close).Make a link with three handshakes and close one with four waves.

When a connection is established or terminated, the exchanged segment contains only TCP header and no data.

Header structure of TCP packets

Before understanding the TCP connection, let’s take a look at the header structure of the TCP message.

what the fuck! The first time I saw a big guy explaining TCP / IP three times shaking hands and four times waving hands

In the above figure, several fields need to be highlighted as follows:

(1) Serial number: the SEQ serial number, which is 32 bits, is used to identify the byte stream sent from TCP source to destination. The initiator marks this when sending data.

(2) Confirmation serial number: ack sequence number, accounting for 32 bits. Only when the ACK flag bit is 1, the confirmation sequence number field is valid, ACK = SEQ + 1.

(3) Flag bits: 6 in total, namely urg, ACK, PSH, RST, syn, fin, etc., with specific meanings as follows:

  • ACK: confirm that the serial number is valid.
  • Fin: release a connection.
  • PSH: the receiver should deliver the message to the application layer as soon as possible.
  • Rst: reset connection.
  • SYN: initiate a new connection.
  • Urg: the urgent pointer is valid.

Note that:

  • Don’t confuse ack with ack in flag bit.
  • ACK = SEQ + 1, both ends are paired.
Three handshakes

The essence of triple handshake is to confirm the ability of both sides to receive and receive data

First, I asked the messenger to deliver a letter to the other party,If the other party receives it, he will know that my sending ability and his receiving ability are OK.

So he wrote back to me,If I receive it, I will know that my sending ability and his receiving ability are OK, and his sending ability and my receiving ability are OK.

However, at this time, he didn’t know whether his sending ability and my receiving ability were OK, so I gave back one last timeIf he receives it, he will know that his sending ability and my receiving ability are OK.

This is three handshakes. Do you understand that?

what the fuck! The first time I saw a big guy explaining TCP / IP three times shaking hands and four times waving hands

  • The first handshake: the client needs to send a connection request to the server. First, the client randomly generates an initial sequence number (ISN) (for example, 100). The message segment sent by the client to the server contains the syn flag (i.e., syn = 1) and the sequence number SEQ = 100.
  • The second handshake: after receiving the message from the client, the server finds syn = 1 and knows that this is a connection request, so it stores the client’s starting sequence number 100 and randomly generates a server’s starting sequence number (for example, 300). Then a message is sent back to the client. The reply message contains syn and ACK flag (syn = 1, ACK = 1), sequence number SEQ = 300, and confirmation number ack = 101 (sequence number + 1 sent by the client).
  • The third Handshake: after receiving the reply from the server, the client finds ack = 1 and ACK = 101, so it knows that the server has received the message with serial number 100; at the same time, it finds syn = 1 and knows that the server has agreed to the connection, so it saves the server’s serial number 300. Then the client replies a message to the server. The message contains the ACK flag bit (ACK = 1), ACK = 301 (server serial number + 1), and seq = 101 (the first handshake message is sent with a sequence number, so this SEQ starts from 101. It should be noted that the ACK message without data does not occupy the serial number, so the SEQ is still 101 when the data is officially sent for the first time. When the server receives the message and finds ack = 1 and ACK = 301, it will know that the client has received the message with sequence number 300. In this way, the client and the server establish a connection through TCP.
Four waves

The purpose of the four waves is to close a link

what the fuck! The first time I saw a big guy explaining TCP / IP three times shaking hands and four times waving hands

For example, the serial number of client initialization is isa = 100, and that of server initialization is isa = 300. After the TCP connection is successful, the client sends a total of 1000 bytes of data, and the server replies a total of 2000 bytes of data before the client sends the fin message.

  • The first wave: when the data of the client is transmitted, the client sends the connection release message to the server (of course, the connection release message can be sent and stop sending data when the data has not been sent out). The release connection message contains the fin flag bit (fin = 1) and the serial number SEQ = 1101 (100 + 1 + 1000, of which 1 is a serial number occupied when establishing the connection). It should be noted that after the client sends the fin message segment, it can not send data, but it can receive data normally; in addition, even if the fin message segment does not carry data, it must occupy a serial number.
  • The second wave: after receiving the fin message sent by the client, the server replies the confirmation message to the client. The confirmation message includes ack flag bit (ACK = 1), ACK = 1102 (client fin message serial number 1101 + 1), and sequence number SEQ = 2300 (300 + 2000). At this time, the server is in the shutdown waiting state, instead of sending fin messages to the client immediately. This state will continue for a period of time, because the server may still have data to send.
  • The third wave: after sending the last data (such as 50 bytes), the server sends a connection release message to the client. The message contains fin and ACK flag bits (fin = 1, ACK = 1), confirmation number and the same as the second wave, ACK = 1102, serial number SEQ = 2350 (2300 + 50).
  • The fourth wave: after receiving the fin message sent by the server, the client sends a confirmation message to the server. The confirmation message contains ack flag bit (ACK = 1), ACK = 2351 and sequence number SEQ = 1102. Note that after the client sends an acknowledgement message, the TCP connection is not released immediately, but after 2msl (twice the longest message segment lifetime), the TCP connection is released. As soon as the server receives the confirmation message sent by the client, it will release the TCP connection immediately, so the time for the server to end the TCP connection is earlier than that of the client.

Common interview questions

Why is TCP connected three times? Can’t you do it twice?

Because we need to consider the problem of packet loss during connection. If we only shake hands twice, if the confirmation message segment sent by the server to the client is lost during the second handshake, the server has prepared the received and received data (it can be understood that the server has connected successfully), but the client has not received the confirmation message from the server, so the client does not know whether the server is ready (OK In this case, the client will not send data to the server and ignore the data sent by the server.

If the handshake is three times, there will be no problem even if packet loss occurs. For example, if the acknowledgement ACK message sent by the client of the third handshake is lost and the server does not receive the ACK acknowledgement message within a period of time, the second handshake will be conducted, that is, the server will resend the syn message segment, and the client will send the ACK acknowledgement message to the server again after receiving the retransmitted message segment.

Why is TCP connected three times and closed four times?

Because TCP can only be disconnected when the client and server have no data to send. When the client sends fin message, it can only ensure that the client has no data to send, and the server does not know whether there is data sent to the client. After receiving the fin message from the client, the server can only reply to the client with a confirmation message to tell the client that the server has received your fin message, but my server still has some data to send, and the server can send fin message to the client only after these data are sent Times).

Why does the client wait for 2msl to release the TCP connection after sending the fourth wave acknowledgement message?

Here is also the problem of packet loss. If the message waving for the fourth time is lost and the server does not receive the acknowledgement ACK message, it will resend the message waving for the third time. In this way, the longest time for a message to go and return is 2msl, so it is necessary to wait for such a long time to confirm that the server has received it.

What if a connection has been established but the client suddenly fails?

TCP has a live timer, if the client fails, the server can’t wait all the time, wasting resources. The server will reset the timer every time it receives a request from the client. The time is usually set to 2 hours. If no data is received from the client within two hours, the server will send a probe message segment, and then send it every 75 seconds. If 10 probe messages are sent in a row and still fail to respond, the server will consider that the client has failed and then close the connection.

What is HTTP and the difference between HTTP and HTTPS

HTTP is an agreement and specification for transmitting hypertext data such as text, picture, audio and video between two points in the computer world
what the fuck! The first time I saw a big guy explaining TCP / IP three times shaking hands and four times waving hands

Common HTTP status codes

The HTTP status code indicates the return result of the client’s HTTP request, identifies whether the server processes normally, and indicates the error of the request.

Category of status code:

what the fuck! The first time I saw a big guy explaining TCP / IP three times shaking hands and four times waving hands

what the fuck! The first time I saw a big guy explaining TCP / IP three times shaking hands and four times waving hands

what the fuck! The first time I saw a big guy explaining TCP / IP three times shaking hands and four times waving hands

Difference between get and post

When we talk about get and post, we have to mention HTTP protocol, because the interaction between browser and server is performed through HTTP protocol, and get and post are also two methods in HTTP protocol.

The full name of HTTP is Hyper Text Transfer Protocol, which is translated into hypertext transfer protocol in Chinese to ensure the communication between browser and server. HTTP works as a request response protocol between client and server.

HTTP protocol defines different methods of interaction between browser and server. There are four basic methods, namely get, post, put and delete. These four methods can be understood as search, modification, addition and deletion of server resources.

  • Get: to get data from the server, that is, the so-called query, is only to obtain the server resources without modification.
  • Post: submit data to the server, which involves updating the data, that is, changing the data of the server.
  • Put: English meaning is to place, that is to add new data to the server, that is, add.
  • Delete: as you can see from the literal meaning, this method is the process of deleting server data.

Difference between get and post

  1. Get is not secure because during transmission, the data is placed in the requested URL; all post operations are invisible to the user. However, this practice is also absolute from time to time. Most people follow the above statement, but you can also add request body to get request and Add URL parameter to post request.
  2. The maximum data in the URL submitted by get request can only be 2048 bytes, which is added by the browser or server. The HTTP protocol does not limit the length of the URL. The purpose is to ensure the normal operation of the server and browser and prevent malicious sending of requests. There is no size limit for post requests.
  3. Get restricts the data set value of form form to ASCII characters; post supports the entire ISO10646 character set.
  4. Get is more efficient than post. Get is the default method for form submission.
  5. Get generates one TCP packet; post generates two TCP packets.

For the get request, the browser will send the HTTP header and data together, and the server will respond to 200 (return data);
For post, the browser sends the header first, the server responds 100 continue, the browser sends data, and the server responds 200 OK (return data).

What is symmetric encryption and asymmetric encryption

Symmetric key encryption refers to the way of using the same key for encryption and decryption. The biggest problem in this way is the key sending problem, that is, how to send the key to the other party safely;

Asymmetric encryption refers to the use of a pair of asymmetric keys, namely public key and private key. The public key can be released at will, but the private key is only known by itself. The party sending the ciphertext uses the public key of the other party for encryption processing. After receiving the encrypted information, the other party uses its own private key to decrypt.
Because asymmetric encryption does not need to send the private key for decryption, it can ensure security; however, compared with symmetric encryption, it is very slow

What is http2

Http2 can improve the performance of web pages.

In http1, the browser limits the number of requests under the same domain name (generally six in chrome). When many resources are requested, due to the blocking of the queue head, when the browser reaches the maximum number of requests, the remaining resources need to wait for the current six requests to complete before sending the request.

Http2 introduces multiplexing technology, which can transmit all the request data through only one TCP connection. Multiplexing can bypass the problem that the browser limits the number of requests under the same domain name, thus improving the performance of web pages.

Main differences between session, cookie and token
The HTTP protocol itself is stateless. It is impossible to judge the status of the server.

What is a cookie

A cookie is a small file (key value format) saved by the web server on the user’s browser, which contains user related information. The client sends a request to the server. If the server needs to record the user’s status, it uses response to issue a cookie to the client browser. The client browser will save the cookie. When the browser requests the website again, the browser submits the requested URL together with the cookie to the server. The server checks the cookie to identify the user.

What is a session

Session is implemented by relying on cookie. A session is a server-side object

A session is a piece of storage space allocated by the server during the session between the browser and the server. By default, the server sets the sessionid in the cookie for the browser. When the browser requests the server, the cookie contains the sessionid. The server gets the information stored in the session according to the sessionid, and then determines the identity information of the session.

The difference between cookie and session

  • Storage location and security: the security of cookie data stored on the client is poor, while that of session data on the server is relatively higher;
  • Storage space: the data saved by a single cookie can’t exceed 4K. Many browsers limit a site to save up to 20 cookies, but session does not
  • Occupying server resources: the session is stored on the server for a certain period of time. When the access increases, the server performance will be occupied. Considering the server performance, cookies should be used.
What is token

The introduction of Token: token requests data from the server frequently from the client, and the server frequently goes to the database to query the user name and password, compares them, judges whether the user name and password are correct or not, and makes corresponding prompt. In this context, token emerges as the times require.

Definition of Token: token is a string of strings generated by the server, which is used as a token for the client to request. After the first login, the server generates a token and returns the token to the client. In the future, the client only needs to bring the token to request data, without bringing the user name and password again.

The purpose of using token: the purpose of token is to reduce the pressure on the server, reduce the frequent query database, and make the server more robust.

Token is generated on the server side. If the front end uses the user name / password to request authentication from the server and the server authenticates successfully, the server will return a token to the front end. The front end can take a token to prove its legal status every time it requests

The difference between session and token

  • There are some problems in the session mechanism, such as increasing server pressure, CSRF cross site forged request attack, and weak scalability;
  • Session is stored on the server and token is stored on the client
  • Token provides authentication and authorization functions. As identity authentication, token has better security than session;
  • Session is only suitable for client code and server code running on the same server. Token is suitable for project level separation of front and rear end (front and back code running under different servers)

Is servlet thread safe

Servlets are not thread safe. Concurrent reading and writing of multithreads will lead to the problem of asynchronous data.

The solution is not to define the name attribute, but to define the name variable in doget() and dopost() methods respectively. Although the synchronized (name) {} statement block can solve the problem, it will cause the thread to wait, which is not a very scientific method.

Note: concurrent read and write servlet class attributes of multithreading can cause data to be out of sync. However, if you only read properties concurrently without writing, there is no problem of data out of sync. Therefore, it is better to define the read-only attribute in servlet as final.

What are the methods of servlet interface and the exploration of servlet life cycle

In Java Web program, servlet is mainly responsible for receiving user request HttpServletRequest, processing it in doget(), dopost(), and feedback httpservletresponse to users. Servlet can set initialization parameters for internal use of servlet.

The servlet interface defines five methods, of which the first three are related to the servlet life cycle

  • void init(ServletConfig config) throws ServletException
  • void service(ServletRequest req, ServletResponse resp) throws ServletException, java.io.IOException
  • void destory()
  • java.lang.String getServletInfo()
  • ServletConfig getServletConfig()
Life cycle:

After the web container loads the servlet and instantiates it, the servlet life cycle begins, and the container runs its init () method to initialize the servlet;

When the request arrives, the service() method of the servlet is called. The service() method will call the doget or dopost methods corresponding to the request as required;

When the server is shut down or the project is unloaded, the server will destroy the servlet instance, and the destroy() method of the servlet will be called.

The init method and the destroy method are executed only once, while the service method client executes every request for the servlet. Sometimes some resources need to be initialized and destroyed in servlets. Therefore, the code for initializing resources can be put into the init method, and the code for destroying resources can be put into the destroy method. In this way, it is not necessary to initialize and destroy resources every time the client requests.

If the client forbids cookie, can session still be used?

Generally speaking, cookie and session are two independent things. Session adopts the scheme of keeping state on the server side, while cookie adopts the scheme of keeping state on the client side.

But why can’t a session be obtained when cookies are disabled? The session ID is used to determine the server session corresponding to the current session, and the session ID is passed through a cookie. If you disable the cookie, you lose the session ID and you will not get the session.

Assuming that the user uses session when the cookie is closed, there are several ways to implement the session:

  1. Manually pass the session ID through URL and hidden form.
  2. Save the session ID in the form of file and database, and call it manually in the process of page crossing.

If there are any errors or other problems, you are welcome to comment and correct. Welcome to share.

Welcome to the official account of the brother of migrant workers:Migrant workers’ technology road
what the fuck! The first time I saw a big guy explaining TCP / IP three times shaking hands and four times waving hands