First, the TCP/UDP

1. What are TCP/IP and UDP

TCP/IP (Transmission control/Network Protocol) is a connection-oriented protocol. A connection must be established before data is sent (a connection must be established between the sender and the receiver). TCP provides reliable service, that is, data transmitted over a TCP connection is not lost, is not duplicated, and arrives in sequence.

UDP is one of the TCP/IP protocols. Yes Is a connectionless protocol, which does not need to establish a connection before sending data and has no reliability. Since no connection is required, it is transmitted over the network by any possible route, so whether it reaches the destination, when it arrives at the destination, and the correctness of the content are not guaranteed.

2. Differences between TCP and UDP

TCP is a connection-oriented protocol. A connection is established before data is sent. TCP provides reliable services, that is, data transmitted over a TCP connection is not lost, does not duplicate, and arrives in sequence.

UDP is a connectionless protocol and does not need to establish a connection before sending data.

TCP communication is similar to making a phone call, confirming identity, and then starting traffic.

UDP communication is similar to school broadcasting, relying on broadcasting to communicate directly.

TCP only supports point-to-point communication, while UDP supports one-to-one, one-to-many, many-to-one, and many-to-many.

TCP is byte stream oriented and UDP is packet oriented. The byte stream is byte oriented. A packet can be divided into several groups for sending. However, a UDP packet can be sent only once.

TCP header overhead (20 bytes) is larger than UDP header overhead (8 bytes)

UDP hosts do not need to maintain complex link state tables.

3. Application scenarios of TCP and UDP

The use of UDP in the case of high real-time requirements for silent writing, such as games, media communications, real-time broadcasting, real-time transmission errors can be tolerated; In most other cases, HTTP uses TCP because it requires that the content transmitted be reliable and never lost.

4. Analysis of application layer protocols running in TCP or UDP.

Protocols running over TCP:

Hypertext Transfer Protocol (HTTP) is used for common browsing.

HTTPS (HTTP over SSL) Indicates the secure version of HTTP.

File Transfer Protocol (FTP) is used for File Transfer.

POP3 (Post Office Protocol vserion 3) is used to receive mails

SMTP is used to send emails.

TELNET (Teletype over the Network) is used to log in to the Network through a terminal.

Secure Shell (SSH) is used to encrypt and Secure login.

Protocols running on UDP:

Boot Protocol (BOOTP) applies to a diskless device.

Network Time Protocol (NTP) is used for Network synchronization.

Dynamic Host Configuration Protocol (DHCP) dynamically configures IP addresses.

It runs over TCP and UDP

The Domain Name Service (DNS) is used to search for addresses and forward mails

ECHO (ECHO Protocol) for error detection and response time measurement (running on TCP and UDP)

Simple Network Management Protocol (SNMP) is used to collect and manage Network information

Dynamic Host Configuration Protocol (DHCP) dynamically configures IP addresses.

Address Resolution Protocol (ARP) is used to dynamically resolve Ethernet hardware addresses.

5. What is ARP

IP addresses can be mapped to physical addresses only after ARP is complete. Each host has an ARP cache that maps IP addresses to hardware addresses of hosts and routers on the LAN.

.

What is Network Address Translation (NAT)?

Hosts on the Intranet need to communicate with hosts on the Internet. The NAT router translates the local IP address of the host into a global IP address, including static translation (the translated global IP address remains unchanged) and dynamic NAT.

7. The process from entering the address to getting the page

(1) The browser queries the DNS and obtains the IP address corresponding to the domain name. The specific process includes searching the DNS cache of the browser, searching the DNS cache of the operating system, reading the local Host file, and querying the IP address from the local DNS server. If the domain name to be queried is included in the resources in the local configuration zone, the resolution result is returned to the client to complete domain name resolution (the resolution is authoritative).

If the domain name to be queried is not parsed by the local DNS server but the server has cached the network mapping, invoke the IP address mapping to complete domain name resolution (the resolution is not authoritative). If the url mapping is not cached by the local domain name server, a recursive or iterative query is initiated based on its Settings.

(2) After the browser obtains the IP address corresponding to the domain name, the browser requests the server to establish a link and initiate a three-way handshake.

(3) After the TCP/IP connection is established, the browser sends an HTTP request to the server.

(4) After receiving the request, the server will map the path parameters to a specific request processor for processing, and return the processing results to the browser

(5) Browser parsing and rendering attempt, if encountered static resources such as JS files, CSS files and images, repeat the above steps and request these resources to the server;

(6) The browser renders the page according to the resources and data it requests, and finally presents a complete page to the user.

8. TCP three-way handshake

In network data transmission, the transport layer protocol TCP is to establish the reliable transmission of the connection, TCP establishes the connection process, we call the three-way handshake.

See Figure -process-Extension

(1) First handshake: The Client sets the SYN to 1 and randomly generates an initial sequence number (SEQ) and sends it to the Server. Then the Client enters the SYN_SENT state.

(2) Second handshake: After receiving the SYN from the Client, the Server acknowledges the Client’s request for establishing a connection, sets its SYN as 1 and ACK as 1, and generates an acknowledge number=ssequence number+1. In addition, the Server randomly generates an initial sequence number and sends it to the Client: Enter the SYN_RCVD state.

(3) Third handshake: the client checks whether the acknowledge number is sequence number +1 and ACK is 1. After checking, the client sets its ACK to 1 to generate an acknowledge number= server sequence number +1 and sends it to the server. Enter the Established state. The server checks for ACK values of 1 and

Acknowledge number +1 indicates that the number is also in the Established state. Complete the three-way handshake and establish the connection.

To put it simply:

(1) The client sends SYN to the server (the client can not confirm the door; The server confirms that the other party is sending correctly.

(2) The server returns a SYN, ACK (the client confirms that it sent the packet and accepted it normally, the other party sends the packet and accepted it normally; The server confirms that it is receiving correctly and sending correctly.

(3) the client sent ACK (the client confirmed: they sent, accept normal, the other party sent to accept normal ga good sad; The server confirmed that: oneself send, accept normal, the other party send and accept normal, so three times shake hands can confirm the double send and receive function is normal, one can not be missed.

9. Can I shake hands twice to establish a connection? Why? You can use it four times. Why?

Can not be

An invalid link request segment may be sent to the server. The segment of the first link request packet sent by the client is not lost, but is stuck on a network node for a long time. As a result, it is delayed until a certain time after the link is released to the Server. This was an insulation section that had long since failed.

However, after the Server receives the invalid connection request packet segment, it mistakenly thinks it is a new connection request sent by the client. Then the client sends an acknowledgement packet to establish a connection. Assuming that the “three-way handshake” is not used, a new connection is established as soon as the Server sends an acknowledgement. Now the client is not sending a connection

, so the server’s acknowledgement is ignored and data is not sent to the server. However, the server lacks a wechat transport connection, and has been waiting for the client to send data. A lot of server resources are wasted. The three-way handshake prevents this from happening.

In addition, two handshakes do not guarantee that the client will receive the packet from the second handshake correctly (the SERVR cannot confirm whether the client received the packet) or that the initial sequence number will be exchanged successfully between the client and server.

I’m sure I can do it four times. Three of them worked and four times, but it degrades the efficiency of the transmission.

10. What happens if the ACK from the client does not reach the server during the third handshake

Server: Because the Server does not receive an ACK, it resends the SYN+ACK every three seconds. (The Server resends the SYN+ACK every five times by default. After that, the connection is automatically CLOSED and the Client enters the CLOSED state.) The Client resends the ACK to the Server.

Client: Two situations occur:

(1) During timeout resending, if the Client sends data to the Server, the ACK of the data header is 1. Therefore, after receiving the data, the Server reads the ACK number and enters the establish state.

(2) After the Server enters the CLOSED state, if the Client sends data to the Server, the Server will reply with the RST packet.

11. What if the link has been established, but the client fails

The server resets a counter every time it receives a request from the client, usually for two hours. If it does not receive any data from the client after two hours, the server sends a probe packet segment, which is then sent every 75 minutes. If there is no response after 10 probe packets are sent, the server assumes that the client is faulty and closes the connection.

12. What is the initial serial number?

Party A of the TCP connection randomly selects A 32-digit Sequence Number (ISN) as the Initial Sequence Number (ISN), for example, 1000, and numbers the data to be transmitted based on the Sequence Number: 1001100 2… In the three-way handshake, this initial serial number is passed to the other party B so that B can confirm it when transmitting data

What data numbers are legal; During data transmission, USER A can acknowledge each byte received by user B. If USER A receives B’s acknowledge number 2001, it indicates that user B has successfully received data numbered 1001-2000.

13. TCP’s four waves

In network data transmission, the process of transport layer protocol disconnection is called quadruple wave

See Figure -process-Extension

(1) First wave: The Client sets the FIN to 1 and sends the serial number SEQ to the Server. Enter the FIN_WAIT_1 state.

(2) Second wave: After receiving the FIN, the Server sends an ACK=1 and acknowledge number= +1. The CLOSE_WAIT state is displayed. At this point, the client has no data to send, but can still accept the data sent by the server.

(3) Third wave: Server sets FIN to 1 and sends a serial number to Clietn; Enter the LAST_ACK state.

(4) Step 4 Wave: After receiving the FIN from the server, the Client enters the TIME_WAIT state. Set ACK to 1 and send an acknowledge number= serial number +1 to the server. After receiving the acknowledge number, the server acknowledges the acknowledge number and changes to CLOSE. The server does not send data to the client. The client also enters the CLOSE state after waiting for 2*MSL (maximum packet lifetime). Complete four waves.

To put it simply:

(1) The client sends the FIN

(2) The server receives the FIN and sends the ACK

(3) The server returns the FIN

(4) The client accepts FIN

Use reality to understand the nuts and bolts of the three-way handshake

Four wave disconnects to make sure all data is transferred

(1) After the client finishes the conversation with the server, the client will say to the server: I want to close the connection (first wave).

(2) The server receives the message from the client and says: OK, you are going to close the connection (second wave).

(3) The server then determines that it has nothing more to say to the client, and says to the client, I want to close the connection. (Third wave)

(4) After receiving the message that the server wants to end the connection, the client says: It has received the message that you want to close the connection (fourth wave) before hanging the wall.

14. Why not combine ACK and FIN sent by server to make three waves?

When the server receives the request for disconnection from the client, it may still have some data to send. In this case, the server replies with ACK to indicate that it has received the request for disconnection. After data is sent, disconnect data transfer from the server to the client on the FIN server.

15. What is the significance of client TIME_WAIT state

The ACK sent by the client to the server may be lost during the fourth wave. The TIME_WAIT state is used to resend the ACK packets that may be lost. If the Server does not receive an ACK, it resends the FIN. If the Client receives an ACK within 2MSL, it resends the ACK and waits for 2MSL to prevent the Server from resending the FIN without receiving an ACK.

MSL (Maximum Segment Lifetime) : indicates the Maximum Lifetime of a Segment on the network. 2MSL is the Maximum time required for sending and replying a Segment. If neither Client receives a FIN in 2MSL, the Client concludes that an ACK has been successfully received and terminates the TCP connection.

Second, the Socket

What is a socket

Two programs on the network exchange data through a two-way communication connection. The two-way connection end called socket.Scoket is usually used to connect the client to the server. Socket is a very popular programming interface of TCP/IP protocol. A Socket is uniquely determined by an IP address and a port number.

In the Java environment, Socket programming mainly refers to network programming based on TCP/IP protocol.

Sockets tend to be low-level and are rarely programmed directly using sockets. The bottom layer of the framework uses Socket more.

Third, the Http

1. What is Http protocol

Http is a protocol for transmitting hypertext data such as text, image, audio, and video reliably between the client and server.

Http protocol belongs to the application layer, several users access the first layer is Http

The HTTP request body consists of: request line, request header, request data

HTTP response packet: status line, response header field, and response content entity.

2. The difference between Socket and Http and application scenarios

A Socket connection is a so-called long connection. In theory, once a connection is established between a client and a server, it will not be actively broken

Socket usage scenarios: network games, continuous bank interaction, live broadcast

An Http link is a short connection, in which the client sends a request to the server, and the server responds by breaking the link and waiting for the next connection

Http usage scenarios: Corporate OA service, Internet service, e-commerce, office, website, etc.

3. The difference between HTTP and Https

In fact, Https is from Http plus encryption (generally SSL secure communication line) + authentication + integrity protection

The difference between:

(1) HTTP needs to get ca certificate, need money

(2) The port is different, HTTP is 80, HTTPS is 443

(3) HTTP is a hypertext transfer protocol, and information is transmitted in plain text. HTTPS is a secure SSL encrypted transport protocol.

(4) HTTP and HTTPS use different connection modes (HTTP connection is simple and stateless; HTTPS is a network protocol based on SSL and HTTP. It is more secure than HTTP for encrypted transmission and identity authentication.