13
头图

This article catalog :

  • Network hierarchy
  • Three handshake
  • Is it okay to shake hands twice?
  • Wave four times
  • Why wait for 2MSL for the fourth wave?
  • Why is it waved four times?
  • What are the characteristics of TCP?
  • What is the difference between TCP and UDP?
  • The characteristics of the HTTP protocol?
  • HTTP message format
  • What are the HTTP status codes?
  • What is the difference between HTTP1.0 and HTTP1.1?
  • The difference between HTTP1.1 and HTTP2.0?
  • What is the difference between HTTPS and HTTP?
  • What is a digital certificate?
  • HTTPS principle
  • DNS resolution process?
  • Enter the URL in the browser to return to the page process?
  • What is the difference between Cookie and Session?
  • What is symmetric encryption and asymmetric encryption?

This article has been included in the github repository. This repository is used to share high-frequency interview questions from major Internet companies and summary of Java core knowledge, including Java basics, concurrency, MySQL, Springboot, MyBatis, Redis, RabbitMQ, etc., a must for interviews! Welcome everyone star!
github address: https://github.com/Tyson0314/Java-learning
If github is not accessible, you can visit the gitee repository.
gitee address: https://gitee.com/tysondai/Java-learning

Network hierarchy

The computer network system is roughly divided into three types, OSI seven-layer model, TCP/IP four-layer model and five-layer model. In general interviews, the five-layer model is often examined.

TCP/IP five-layer model: application layer, transport layer, network layer, data link layer, and physical layer.

  • Application layer : Provide interactive services for applications. There are many application layer protocols in the Internet, such as domain name system DNS, HTTP protocol, SMTP protocol and so on.
  • transport layer : Responsible for providing data transmission services for the communication between the two host processes. The protocols of the transport layer mainly include Transmission Control Protocol TCP and User Data Protocol UDP.
  • network layer : Choose appropriate routing and switching nodes to ensure timely data transmission. It mainly includes the IP protocol.
  • data link layer : When transmitting data between two adjacent nodes, the data link layer assembles the IP datagrams handed over from the network layer into a frame , which is transmitted on the link between the two adjacent nodes frame.
  • physical layer : to achieve transparent transmission of bit streams between adjacent nodes, as far as possible to shield the transmission medium and physical equipment differences.

Three handshake

Assume that the sending end is the client and the receiving end is the server. At the beginning, the status of the client and server are both CLOSED .

  1. The first handshake: The client initiates a connection establishment request to the server. The client randomly generates a starting sequence number x. The field sent by the client to the server contains the flag bit SYN=1 and the sequence number seq=x . The state of the client before the first handshake is CLOSE , and the state of the client after the first handshake is SYN-SENT . The status of the LISTEN at this time is 061b460f3f3005.
  2. Second handshake: After receiving the message from the client, the server will randomly generate a server's starting sequence number y, and then reply to the client with a message, which includes the flag bits SYN=1 , ACK=1 , and the sequence number seq=y , confirmation number ack=x+1 . The status of the LISTEN before the second handshake is 061b460f3f3061, and the status of the server after the second handshake is SYN-RCVD , and the status of the client at this time is SYN-SENT . ( SYN=1 means to establish a connection with the client, ACK=1 means to confirm that the serial number is valid)
  3. The third handshake: After the client receives the message from the server, it will send the message to the server again, which contains the flag bit ACK=1 , the serial number seq=x+1 , and the confirmation number ack=y+1 . The state of the client before the third handshake is SYN-SENT , and the states of the client and server after the third handshake are both ESTABLISHED . The connection establishment is now complete.

Is it okay to shake hands twice?

The third handshake is mainly for prevent the invalid connection request segment suddenly transmitted to the server, causing problems.

  • For example, when client A sends a connection request, A may not receive the confirmation message due to network congestion, so A retransmits the connection request again.
  • After the connection is successful, the connection is released after waiting for the completion of the data transmission.
  • Then the first connection request sent by A waits for a certain time after the connection is released before reaching the server B. At this time, B mistakenly believes that A has sent a new connection request again, so it sends an acknowledgment segment to A.
  • If the three-way handshake is not used, as long as B sends a confirmation, a new connection is established. At this time, A will not respond to B's confirmation and does not send data, so B will wait for A to send data, wasting resources.

Wave four times

  1. A's application process first sends a connection release segment ( FIN=1,seq=u ) to its TCP, stops sending data again, actively closes the TCP connection, enters the FIN-WAIT-1 (terminate waiting 1) state, and waits for B's confirmation.
  2. After receiving the connection release segment ACK=1,ack=u+1,seq=v ), and B enters the CLOSE-WAIT (close waiting) state. At this time, TCP is in a half-closed state, and the connection from A to B is released.
  3. After receiving B's confirmation, A enters the FIN-WAIT-2 (termination waiting 2) state, and waits for the connection release message segment sent by B.
  4. After B sends the data, it will send out the connection release segment ( FIN=1,ACK=1,seq=w,ack=u+1 ), B enters the LAST-ACK (final confirmation) state, and waits for A's confirmation.
  5. After A receives the connection release segment of B, it sends an acknowledgement segment ( ACK=1,seq=u+1,ack=w+1 ), and A enters the TIME-WAIT (time waiting) state. At this time, TCP is not released, and the time 2MSL (maximum segment survival time) CLOSED state. After B receives the confirmation segment sent by A, it closes the connection. If it does not receive the confirmation segment sent by A, B will retransmit the connection release segment.

Why wait for 2MSL for the fourth wave?

  • guarantees that the last ACK segment sent by A can reach B . The ACK segment may be lost. If B does not receive the confirmation message, it will retransmit the connection release segment over time, and then A can receive the retransmitted connection release segment within 2MSL Retransmit an acknowledgment and restart the 2MSL timer. Finally, both A and B enter the CLOSED state. If A is in the TIME-WAIT state and does not wait for a period of time, but releases the connection immediately after sending the ACK segment, it cannot receive B replay. The transmitted connection release segment, so the confirmation segment will not be sent again, and B will not be able to enter the CLOSED state normally.
  • prevents invalid connection request segments from appearing in this connection . After sending the last ACK message segment, A can make all the message segments generated by this connection disappear from the network after 2MSL, so that the old connection request message will not appear in the next new connection part.

Why is it waved four times?

Because when the Server side receives the SYN connection request message from the Client side, it can directly send the SYN+ACK message. But when the connection is closed, when the server receives the connection release message from the client, it may not immediately close the SOCKET , so the server first responds with a ACK message, telling the client that I have received yours Connection release message. Only when all the messages on the server side have been sent, the server side can send a connection release message, and then the two sides will truly disconnect. Therefore, four waves of hands are required.

What are the characteristics of TCP?

  • TCP is a connection-oriented transport layer protocol
  • point-to-point , each TCP connection can only have two endpoints.
  • TCP provides reliably deliver services.
  • TCP provides full-duplex communication .
  • faces the byte stream .

What is the difference between TCP and UDP?

  1. TCP connection-oriented ; UDP is connectionless, that is, there is no need to establish a connection before sending data.
  2. TCP provides reliable service ; UDP does not guarantee reliable delivery.
  3. TCP faces the byte stream , which treats data as a series of unstructured byte streams; UDP is message-oriented.
  4. TCP has congestion control ; UDP has no congestion control, so network congestion will not reduce the sending rate of the source host (useful for real-time applications, such as real-time video conferencing, etc.).
  5. Each TCP connection can only be point to point ; UDP supports one-to-one, one-to-many, many-to-one and many-to-many communication modes.
  6. TCP header overhead is 20 bytes; UDP header overhead is small, only 8 bytes.

The characteristics of the HTTP protocol?

  1. HTTP allows the transmission of data of any type The type of transmission is marked by Content-Type.
  2. Stateless . For each request sent by the client, the server considers it to be a new request, and there is no connection between the previous session and the next session.
  3. Support client/server mode .

HTTP message format

The HTTP request consists of four parts: request line, request header, blank line, and request body

  • request line : Including the request method, the resource URL to be accessed, and the HTTP version used. GET and POST are the most common HTTP methods, in addition to DELETE、HEAD、OPTIONS、PUT、TRACE .
  • request header : the format is "attribute name: attribute value", the server obtains the client's information according to the request header, mainly cookie、host、connection、accept-language、accept-encoding、user-agent .
  • request body : user request data such as user name, password, etc.

request message example :

POST /xxx HTTP/1.1 请求行
Accept:image/gif.image/jpeg, 请求头部
Accept-Language:zh-cn
Connection:Keep-Alive
Host:localhost
User-Agent:Mozila/4.0(compatible;MSIE5.01;Window NT5.0)
Accept-Encoding:gzip,deflate

username=dabin 请求体

The HTTP response also consists of four parts: status line, response header, blank line and response body .

  • status line : protocol version, status code and status description.
  • response header : The response header field mainly has connection、content-type、content-encoding、content-length、set-cookie、Last-Modified,、Cache-Control、Expires .
  • response body : the content returned by the server to the client.

response message example :

HTTP/1.1 200 OK
Server:Apache Tomcat/5.0.12
Date:Mon,6Oct2003 13:23:42 GMT
Content-Length:112

<html>
    <body>响应体</body>
</html>

What are the HTTP status codes?

What is the difference between HTTP1.0 and HTTP1.1?

  • long connection : HTTP1.0 uses short connection by default, each request needs to establish a new TCP connection, and the connection cannot be reused. HTTP1.1 supports long connections, multiplexing TCP connections, allowing the client to send multiple requests through the same connection. However, this optimization strategy also has problems. When a request at the head of the queue fails to receive the response resources, it will block subsequent requests. This is the problem of " blocked ".
  • : HTTP1.0 does not support . HTTP1.1 adds the range field, which is used to specify the position of the data byte. supports the .
  • error status response code : 24 new error status response codes have been added to HTTP 1.1. For example, 409(Conflict) indicates that the requested resource conflicts with the current state of the resource, and 410(Gone) indicates that a resource on the server is permanently deleted.
  • Host header processing : In HTTP1.0, each server is considered to be bound to a unique IP address. Therefore, the URL in the request message does not convey the host name. In the HTTP1.1 era, virtual host technology has developed rapidly. There can be multiple virtual hosts on a physical server, and they share an IP address, so HTTP1.1 adds HOST information.

The difference between HTTP1.1 and HTTP2.0?

The features supported by HTTP2.0 compared to HTTP1.1:

  • new binary format : HTTP1.1 transmits data based on text format; HTTP2.0 uses binary format to transmit data, which makes parsing more efficient.
  • multiplexing : In a connection, multiple requests or responses are allowed to be sent at the same time, and these requests or responses can be transmitted in parallel without being blocked , avoiding the "head-of-line jam" problem in HTTP1.1.
  • header compression , HTTP1.1 header contains a lot of information, and it must be sent repeatedly every time; HTTP2.0 separates the header from the data and encapsulates it into a header frame and a data frame, uses a specific algorithm to compress the header frame , effectively reducing the size of header information. And HTTP2.0 records the previously sent key-value pairs on the client and server, and the same data will not be sent repeatedly. example, request a sends all header information fields, and request b only needs to send difference data , which can reduce redundant data and reduce overhead.
  • server push : HTTP2.0 allows the server to push resources to the client without the client sending a request to the server to obtain it.

What is the difference between HTTPS and HTTP?

  1. HTTP is the hypertext transfer protocol, and the information is plaintext transmission ; HTTPS is the ssl encrypted transfer protocol security and
  2. HTTP and HTTPS use different ports. HTTP port is 80 and HTTPS is 443.
  3. The HTTPS protocol needs to apply for a certificate from the CA, which generally requires a certain fee.
  4. HTTP runs on the TCP protocol; HTTPS runs on the SSL protocol, and SSL runs on the TCP protocol.

What is a digital certificate?

The server can apply for a certificate from the certificate authority CA to avoid man-in-the-middle attacks (to prevent the certificate from being tampered with). The certificate contains three parts: certificate content, certificate signature algorithm and signature . The signature is to verify the identity.

The server transmits the certificate to the browser, and the browser obtains the public key from the certificate. The certificate can prove that the public key corresponds to this website.

digital signature of the production process :

  1. The CA uses the certificate signature algorithm to perform the hash operation on the certificate content.
  2. Of the value of the hash with the CA's private key encryption , get a digital signature.

browser verification process :

  1. Obtain the certificate, obtain the certificate content, certificate signature algorithm, and digital signature.
  2. Use the public key of the CA organization to decrypt the digital signature (because it is an organization trusted by the browser, the browser saves its public key).
  3. Use the signature algorithm in the certificate to hash the content of the certificate .
  4. Compare the decrypted digital signature with the hash value obtained by hashing the content of the certificate. If they are equal, it indicates that the certificate is trustworthy.

HTTPS principle

The first is the TCP three-way handshake, and then the client initiates an HTTPS connection establishment request. The client first sends a Client Hello , and then the server responds with Server Hello , and then sends its certificate to the client, and then the two parties go through the key exchange, and finally use The exchanged key encrypts and decrypts data.

  1. negotiates the encryption algorithm . In Client Hello , the client will inform the server of its current information, including the TLS version to be used by the client, supported encryption algorithms, domain names to be accessed, and a random number (Nonce) generated for the server. You need to inform the server of the domain name you want to access in advance so that the server can send the certificate of the corresponding domain name.

  2. Server response Server Hello , telling the client server encryption algorithm selected .

  3. Then the server sent 2 certificates to the client. The second certificate is the certificate of the issuing authority (CA) of the first certificate.

  4. The client uses the RSA public key publicly released by the certificate authority CA to verify the certificate . The following figure shows that the certificate is successfully authenticated.

  5. After the verification is passed, the browser and the server via exchange algorithm key generated shared symmetric key .

  6. Start to transmit data, and use the same symmetric key to encrypt and decrypt.

DNS resolution process?

  1. The browser searches own DNS cache
  2. If not, search the DNS cache and hosts file
  3. If not, the operating system sends the domain name to the local domain name server , and the local domain name server queries its own DNS cache. If the search succeeds, it returns the result. Otherwise, it initiates a query request root domain name server, top-level domain name server, and authority domain name server , And finally return the IP address to the local domain name server
  4. The local domain name server returns the obtained IP address to the operating system , and at the same time the IP address
  5. The operating system returns the IP address to the browser and also caches the IP address itself
  6. The browser gets the IP address corresponding to the domain name

Enter the URL in the browser to return to the page process?

  1. resolves the domain name and finds the host IP.
  2. The browser uses IP to directly communicate with the website host, three-way handshake , to establish a TCP connection. The browser will initiate a TCP connection to port 80 of the web program on the server with a random port.
  3. After the TCP connection is established, the browser initiates an HTTP request to the host.
  4. The server responds to the request and returns response data.
  5. The browser parses the response content, renders , and presents it to the user.

What is the difference between Cookie and Session?

  • different scope of action. , Cookie is stored on the client side, and Session is stored on the server side.
  • The validity period of . Cookie can be set to keep for a long time. For example, we often use the default login function. Session generally has a short expiration time, and it will become invalid if the client is closed or Session timeout.
  • privacy policy of different from 161b460f400252. Cookies are stored on the client side and are easy to be stolen; Sessions are stored on the server side, and the security is better than cookies.
  • storage size different , single Cookie saved data can not exceed 4K; Session is stored for no upper limit, but the server for performance reasons, do not store too much data in the Session, and the need to set Session deletion mechanisms.

What is symmetric encryption and asymmetric encryption?

symmetric encryption : two communication parties using same key encrypted. The characteristic is that the encryption speed is fast, but the disadvantage is that the leakage of the key will cause the ciphertext data to be cracked. Common symmetric encryption has AES and DES algorithms.

Asymmetric encryption : It needs to generate two keys, public key and private key . The public key is public and anyone can obtain it, while the private key is kept privately. The public key is responsible for encryption and the private key is responsible for decryption; or the private key is responsible for encryption and the public key is responsible for decryption. This encryption algorithm is more , but has a much larger amount of calculation than symmetric encryption , and encryption and decryption are both slow. Common asymmetric algorithms are RSA and DSA .


The code word is not easy, if you think it is helpful to you, you can encourage it!
I am programmer Dabin, focusing on sharing hard core knowledge of Java back-end, welcome everyone to pay attention~


程序员大彬
468 声望489 粉丝

非科班转码,个人网站:topjavaer.cn