2024 Java Interview--Network Basics (1)

Table of Contents of Series Articles

  1. 2024 Java Interview (1) – Spring Chapter
  2. 2024 Java Interview (2) – Spring Chapter
  3. 2024 Java Interview (3) – Spring Chapter
  4. 2024 Java Interview (4) – Spring Chapter
  5. 2024 Java Interview – Collection
  6. 2024 java interview–redis(1)
  7. 2024 java interview–redis(2)


TCP three-way handshake

Three-way handshake process:
Client - sends a data packet with the SYN flag - server has a one-time handshake and the Client enters the syn_sent state

​Server-sends data packets with SYN/ACK flags-client two-step handshake server enters syn_rcvd

​Client - sends a data packet with the ACK flag - the server enters the Established state after a three-way handshake connection

​Why three times:
​Mainly
to establish a reliable communication channel and ensure that the client and server have the ability to send and receive data at the same time.

Why not twice?
1.
Prevent invalid request messages from being transmitted to the server again, establishing redundant links and wasting resources.

2. Two handshakes can only ensure that the one-way connection is smooth. (In order to achieve reliable data transmission, both communicating parties in the TCP protocol must maintain a sequence number to identify which of the data packets sent out have been received by the other party. The process of the three-way handshake is that the communicating parties inform each other of the sequence number The necessary steps to confirm that the other party has received the starting value of the sequence number; if there are only two handshakes, at most only the starting sequence number of the connection initiator can be confirmed, and the sequence number selected by the other party cannot be obtained. confirm)

TCP four wave process

Four waving processes:
Client
- sends a data packet with the FIN flag - server, closes the connection with the server, and the client enters the FIN-WAIT-1 state

When the server receives this FIN, it sends back an ACK. The confirmation sequence number is the received sequence number plus 1, and the server enters the CLOSE-WAIT state.

Server - sends a FIN packet - client, closes the connection with the client, and the client enters the FIN-WAIT-2 state

The client receives this FIN, sends back an ACK message to confirm, and sets the confirmation sequence number to the received sequence number plus 1, in the TIME-WAIT state

Why four times:

Because it is necessary to ensure that the data between the client and the server can be transmitted.

CLOSE-WAIT:

The meaning of this state actually means that it is waiting to be closed.

TIME-WAIT:

In order to solve network packet loss and other problems caused by network instability, ensure that the connecting party can close its connection within the time range

How to check the number of links in TIME-WAIT status?

netstat -an |grep TIME_WAIT|wc -l View the number of connections waiting for time_wait status connections

Why is there too many TIME-WAITs? What is the solution?

Possible reasons : On a TCP server with high concurrency and short connections, when the server completes the request, it immediately closes the connection normally according to the initiative.

Solution : Load balancing server; the web server first closes the connection from the load balancing server

1. OSI and TCP/IP model

OSI seven layers: physical layer, data link layer, network layer, transport layer, session layer, presentation layer, application layer

TCP/IP five layers: physical layer, data link layer, network layer, transport layer, application layer

2. Common network service layering

Application layer: HTTP, SMTP, DNS, FTP

Transport layer: TCP, UDP

Network layer: ICMP, IP, routers, firewalls
Data
link layer: network cards, bridges, switches

Physical layer: repeaters, hubs

3. Differences and scenarios between TCP and UDP

type Features performance Application scenarios header byte
TCP Connection-oriented, reliable, byte stream The transmission efficiency is slow and requires a lot of resources. File and email transfer 20-60
UDP No connection, unreliability, data segment Fast transmission efficiency and few resources required Voice, video, live broadcast 8 bytes

TCP-based protocols: HTTP, FTP, SMTP

UDP-based protocols: RIP, DNS, SNMP

4. TCP sliding window, congestion control

TCP ensures reliable transmission of data by: applying data segmentation, numbering data packets, checksums, flow control, congestion control, timeout retransmission and other measures;

Congestion control purpose : To prevent excessive data from being injected into the network and avoid overloading routers and links in the network

Congestion control process : TCP maintains a congestion window, which changes dynamically with the degree of network congestion, and reduces the occurrence of network congestion through algorithms such as slow start and congestion avoidance.

5. Causes and solutions for TCP sticky packets

TCP sticky packet means : several packets of data sent by the sender are stuck into one packet when the receiver receives it.

Sender reason:

TCP uses the Nagle algorithm by default (main role: reducing the number of message segments in the network):

Collect multiple small packets and send them together when a confirmation arrives, causing the sender to have a sticky problem.

Receiver reason:

TCP saves received data packets in the receive cache. If the speed at which TCP receives data packets into the cache is greater than the speed at which the application reads data packets from the cache, multiple packets will be cached and the application may read them. to multiple packages glued together end to end.
​Solve
the sticky bag problem:

The most essential reason is that the receiving peer cannot tell where the boundary between messages is, and the boundary is given by using some scheme, for example:

  • Send fixed length packages. The size of each message is the same. The receiver only needs to accumulate the received data until the data equals a fixed-length value and treat it as a message.

  • Add the \r\n mark at the end of the package. The FTP protocol does exactly this. But the problem is that if the data body also contains \r\n, it will be misjudged as the boundary of the message.

  • Baotou plus body length. The packet header is a fixed length of 4 bytes, indicating the length of the packet body. The receiving peer first receives the packet body length, and then receives the packet body based on the packet body length.

6. TCP and UDP message formats

TCP message format

Insert image description here
Source port number and destination port number:

Used to find the originating and receiving application processes. These two values ​​plus the source IP address and destination IP address of the IP header uniquely determine a TCP connection.

Serial number field:

The sequence number is used to identify the data byte stream sent from the TCP sender to the TCP receiver. It represents the first data byte in this message segment. If you think of a byte stream as a one-way flow between two applications, TCP counts each byte with a sequence number. The sequence number is a 32-bit unsigned number. The sequence number starts from 0 after reaching 2^32-1.

When a new connection is established, the SYN flag changes to 1. The sequence number field contains the initial sequence number ISN (Initial Sequence Number) selected by this host for this connection. The first byte sequence number of the data that the host wants to send is this ISN plus 1, because the SYN flag consumes a sequence number.

Confirm serial number:

Since each transmitted byte is counted, the acknowledgment sequence number contains the next sequence number that the endpoint sending the acknowledgment expects to receive. Therefore, the confirmation sequence number should be the last successfully received data byte sequence number plus 1. The confirmation sequence number field is valid only when the ACK flag is 1. There is no cost to sending an ACK because the 32-bit acknowledgment number field, like the ACK flag, is always part of the TCP header. Therefore, we see that once a connection is established, this field is always set and the ACK flag is always set to 1. TCP provides full-duplex services to the application layer. This means that data can be transmitted independently in both directions. Therefore, each end of the connection must maintain the sequence number of transmitted data in each direction.
Capital
length:

The header length gives the number of 32-bit words in the header. This value is needed because the length of the optional field is variable. This field occupies 4 bits, so TCP has a maximum of 60 bytes of header. However, there are no optional fields and the normal length is 20 bytes.
Flag
field : There are 6 flag bits in the TCP header. Multiple of them can be set to 1 at the same time.
The URG emergency pointer is valid and the ACK confirmation sequence number is valid.
Window
size:

TCP's flow control is provided by each end of the connection through a declared window size. The window size is the number of bytes starting from the value specified in the acknowledgment sequence number field. This value is the number of bytes the receiving end expects to receive. The window size is a 16-bit field, so the maximum window size is 65535 bytes.

Checksum:

The checksum covers the entire TCP segment: TCP header and TCP data. This is a mandatory field that must be calculated and stored by the originator and verified by the receiver.
Urgent
pointer:

The emergency pointer is only valid when the URG flag is set. The urgent pointer is a positive offset, added to the value in the sequence number field, to represent the sequence number of the last byte of urgent data. TCP's emergency mode is a way for the sender to send urgent data to the other end.

Options:
The most
common optional field is the longest message size, also known as MSS (Maximum Segment Size). Each connecting party usually specifies this option in the first segment of the communication (the segment in which the SYN flag is set to establish the connection). It indicates the maximum length of message segment that the local end can receive.

UDP message format

Insert image description here
The port number:

Used to represent sending and receiving processes. Since the IP layer has assigned the IP datagram to either TCP or UDP (according to the value of the protocol field in the IP header), the TCP port number is viewed by TCP, and the UDP port number is viewed by UDP. TCP port numbers and UDP port numbers are independent of each other.

length:

The UDP length field refers to the byte length of the UDP header and UDP data. The minimum value of this field is 8 bytes (sending a 0-byte UDP datagram is OK).

Checksum:

The UDP checksum is an end-to-end checksum. It is calculated by the sender and then verified by the receiver. Its purpose is to discover any changes in UDP headers and data between the sender and the receiver.

IP message format : The ordinary IP header is 20 bytes long, unless it contains optional fields.
4
-digit version:

The current protocol version number is 4, so IP is sometimes called IPV4.

4-digit header length:

The header length refers to the number of 32-bit words in the header, including any options. Since it is a 4-bit field, the header length is up to 60 bytes.

Type of Service (TOS):

The service type field includes a 3-bit priority field (now ignored), the 4-bit TOS subfield and the 1-bit unused bit must be set to 0. The 4-bit TOS represents: minimum delay, maximum throughput, maximum reliability and minimum cost. Only 1 bit can be set among the 4 bits. If all 4 bits are 0, it means normal service.
​Total
Length:
The ​Total
Length field refers to the length of the entire IP datagram, in bytes. Using the header length and total length fields, you can know the starting position and length of the data content in the IP datagram. Since this field is 16 bits long, the IP datagram can be up to 65535 bytes long. When the datagram is fragmented, the value of this field also changes.

Identification field:
The identification
field uniquely identifies each datagram sent by the host. Usually its value is incremented by 1 every time a message is sent.

Survival time:

The TTL (time-to-live) field sets the maximum number of routers that the datagram can pass through. It specifies the time-to-live of the datagram. The initial value of the TTL is set by the source host (usually 3 2 or 6 4), and once it passes through a router that handles it, its value is reduced by 1. When the value of this field is 0, the datagram is discarded and an ICMP message is sent to notify the source host.

Header checksum:

The header checksum field is a checksum code calculated based on the IP header. It does not calculate the data after the header. ICMP, IGMP, UDP, and TCP all contain both coverage header and data checksum codes in their respective headers.

Destination address and source address:

It refers to the hardware address of the network card (also called MAC address), which is 48 bits long and is fixed when the network card leaves the factory.
Data
: The data length in
the Ethernet
The maximum value of 1500 is called the maximum transmission unit (MTU) of Ethernet. Different network types have different MTUs. If a data packet is routed from Ethernet to a dial-up link, and the degree of the data packet is greater than the MTU of the dial-up link, then The data packet needs to be fragmented). There is also "MTU:1500" in the output of the ifconfig command. Note that the concept of MTU refers to the maximum length of the payload in the data frame, excluding the length of the frame header.

HTTP protocol

1. HTTP protocol 1.0_1.1_2.0

HTTP1.0 : The server disconnects the TCP connection immediately after completion of processing ( no connection ), the server does not track each client and does not record past requests ( stateless )

HTTP1.1 : KeepAlived long connections avoid the overhead of connection establishment and release; use Content-Length to determine whether the current request data has been fully accepted ( stateful ) HTTP2.0
: Introducing the concepts of binary data frames and streams, in which frame pairs The data is sequenced; because of the sequence, the server can transmit data in parallel.

The main differences between http1.0 and http1.1 are as follows:
1. Cache processing: 1.1 Add more cache control strategies (such as: Entity tag, If-Match)
2. Network connection optimization: 1.1 Support breakpoint resume download
3. Increase in error status codes: 1.1 adds 24 error status response codes. The rich error codes make each status clearer.
4. Host header processing: Supports the Host header field and no longer uses IP as the requester flag.
5. Long connections: reduced The cost and latency of establishing and closing connections.

The main differences between http1.1 and http2.0:
1. New transmission format: 2.0 uses binary format, 1.0 still uses text-based format
2. Multiplexing: connection sharing, different requests can be transmitted using the same connection (Finally, it is combined into a normal request based on the ID number on each request)
3. Header compression: Since the header in 1
. Server push: same as Google's SPDUY (an upgrade of 1.0)

2. The difference between HTTP and HTTPS

​The difference between HTTP and HTTPS:

HTTP HTTPS
Default port 80 HTTPS uses port 443 by default
Clear text transmission, unencrypted data, poor security The transmission process is SSL encrypted and has good security.
Fast response and low resource consumption The response speed is slow, consumes a lot of resources, and requires the use of CA certificates

The process of establishing an HTTPS link:
1. First, the client sends a request to the server.
2. The server sends an SSL certificate to the client. The content includes: the issuing authority, validity period, owner, signature and public key of the certificate.
3. The client The sent public key is verified for authenticity. If the verification is true, the public key is used to encrypt the symmetric encryption algorithm and symmetric key. 4. The
server uses the private key to decrypt and uses the symmetric key to encrypt the confirmation information and sends it to the client.
5. Then the client and server use the symmetric key for information transmission.

Symmetric encryption algorithm:
Both parties hold the same key, and the encryption speed is fast. Typical symmetric encryption algorithms: DES, AES

Asymmetric encryption algorithm:
Keys appear in pairs (private key, public key). The private key is known only to you and is not transmitted on the network; while the public key can be made public. Compared with symmetric encryption, which is slower, typical asymmetric encryption algorithms are: RSA, DSA

3. The difference between Get and Post requests

HTTP request:

method describe
GET Send a request to a specific resource, query data, and return entities
POST Submitting data to specified resources for processing requests may result in the creation of new resources and modification of existing resources.
PUT Upload new content to the server
HEAD Similar to a GET request, there is no specific content in the returned response, which is used to obtain headers
DELETE Requests the server to delete the resource with the specified ID
OPTIONS Can be used to send requests to the server to test the functionality of the server
TRACE Echo requests received by the server, for testing or diagnostics
CONNECT The HTTP/1.1 protocol is reserved for proxy servers that can change connections to pipelines.

The difference between get and Post:

GET POST
visibility Data is visible to everyone in the URL Data will not be displayed in the URL
safety Compared to post, get is less secure because the data sent is part of the URL Safe because parameters are not saved in browser history or web server logs
Data length Restricted, maximum 2kb Unlimited
encoding type application/x-www-form-urlencoded multipart/form-data
cache can be cached cannot be cached

4. Common HTTP response status codes

100: Continue — continue. The client should continue its request.

200: OK — The request was successful. Generally used for GET and POST requests.

301: Moved Permanently — permanent redirect.

302: Found — Temporary redirect.

400: Bad Request - The syntax of the client's request is incorrect and the server cannot understand it.

403: Forbideen — The server understands the request from the client, but refuses to execute the request.

404: Not Found — The server cannot find the resource (webpage) based on the client's request.
​500
: Internal Server Error — The server has an internal error and cannot complete the request.

502: Bad Gateway - An invalid response was received from the remote server while trying to perform a request as a gateway or proxy server.

5. The difference between redirection and forwarding

Redirect:redirect:

The address bar changes.
Redirection
can access resources of other sites (servers).
Redirection
is two requests. Cannot use request object to share data

Forward: forward:

The forwarding address bar path remains unchanged

Forwarding can only access resources under the current server

Forwarding is a request, and you can use the request object to share data

6. The difference between Cookie and Session

Cookies and Sessions are both session methods used to track the identity of browser users, but they are different:

Cookie data is stored on the client side (browser side), and Session data is stored on the server side.

Cookies are not very secure. Others can analyze the cookies stored locally and deceive them. Sessions should be used for security reasons.

Cookies are generally used to save user information. The main function of Session is to record the user’s status through the server.

Browser input URL process

Process : DNS resolution, TCP connection, sending HTTP request, server processing request and returning HTTP message, browser rendering, end

process protocol used
1. The browser searches for the IP address of the domain name DNS and the DNS search process (browser cache, router cache, DNS cache) DNS: Get the IP corresponding to the domain name
2. Establish TCP connection based on IP TCP: Establishing a connection with the server
3. The browser sends an HTTP request to the server HTTP: Send request
4. Server responds with HTTP response HTTP
5. Browser rendering

Guess you like

Origin blog.csdn.net/weixin_43228814/article/details/132641421