Summary of computer network interview questions (updating...)

1. What are the network protocols? What is the role of each layer in the seven-layer structure of the OSI protocol? Which protocols do they correspond to?

a. Common network protocols

  • TCP (Transport Control Protocol): Transmission Control Protocol

  • IP (Internet Protocol): Internet protocol

  • UDP (User Datagram Protocol): User Datagram Protocol, which is located in the same transport layer as TCP, and used in conjunction with the IP protocol, saves the header when transmitting data, but it cannot provide retransmission of data packets, so it is suitable for transmitting shorter document.

  • FTP (File Transfer Protocol): A remote file transfer protocol that allows users to copy files from a remote host to their own computer.

  • SMTP (Simple Mail transfer Protocol): Simple Postal Transfer Protocol, used to transmit email.

  • NFS (Network File Server): A network file server that enables multiple computers to transparently access each other's directories.

b. The role of each layer in the seven-layer structure of the OSI protocol

  • Physical layer : transmit bits through the medium, determine mechanical and electrical specifications (Bit)

  • Data link layer : decorate bits into frames and point-to-point transmission (frame frame)

  • Network layer : Responsible for the transmission of data packets from source to destination and Internet interconnection (Packet)

  • Transport layer : Provide end-to-end reliable message delivery and error recovery (segment)

  • Session layer : establish, manage and terminate sessions (session protocol data unit SPDU)

  • Presentation layer : translates the data. Encryption and compression (meaning Protocol Data Unit PPDU)

  • Application layer : the means to allow access to the OSI environment (Application Protocol Data Unit APDU)

c. Protocols corresponding to each

insert image description here


2. TCP three-way handshake

  • By default, the TCP processes of the client client and the server sever are in the CLOSED (closed) state .
  • The server TCP service process first establishes the transmission control block TCB , and then the server enters the LISTEN state , waiting for the client connection request.

insert image description here

  • The first handshake: The client TCP process also first establishes the transmission control block TCB, and then sends a connection request segment to the server. At this time, SYN=1 , randomly selects an initial sequence number seq=x, and this message cannot carry Data , but a serial number is consumed. After sending, the client enters the SYN-SENT (synchronization has been sent) state

  • The second handshake: After receiving the client request connection segment, if the server agrees to establish a connection, it will send a confirmation message , in which SYN=1, ACK=1 , confirmation number ack=x+1 , and at the same time Randomly select a sequence number of your own seq=y , and confirm that the message segment cannot carry data, but also consumes a sequence number. After sending, the server enters the SYN-RCVD (synchronously received) state

  • The third handshake : After receiving the confirmation message, the client checks whether ACK=1, ack=x+1 is correct , if correct, sends a confirmation message to the server, and confirms that ACK=1, ack=y in the message +1, seq=x+1, enter the ESTAB-LISHED state after sending , the server also enters the ESTAB-LISHED state after receiving the confirmation message, at this time the TCP connection between the two parties is formally established.

The above connection establishment process is the TCP three-way handshake.


3. Why is TCP a three-way handshake?

  • In order to prevent the invalid connection request segment from being sent to the Server side suddenly.
  • explain:

-Assuming that the current TCP connection is a two-way handshake mechanism, as shown in the figure below, the server will enter the ESTAB-LISHED state after receiving the connection request from the client, and there will
be such a problem:

The client sends a connection request message. Due to network reasons, it is blocked on a certain network node for a long time, so the client retransmits the connection request message once. The second request message reaches the server normally, the connection is established normally, and the data transmission is completed. After that, release the connection. But assuming that the request message sent for the first time is not lost at this time, but arrives at the server after a period of delay, this is an invalid connection request message segment, but after receiving it, the server will think that it was resent by the client A connection request , so after sending a confirmation message to the client, it enters the ESTAB-LISHED state , but the client does not send a request for a new connection, it ignores the confirmation message from the server , but the server has been waiting for the client to send data, resulting in a waste of server resources serious.


4.TCP's four waves

The four finger gestures of TCP are the TCP connection release process, which is different from the three-way handshake process when the TCP connection is established, and the TCP connection release process is more complicated. Two important features of the TCP protocol:

  • The TCP protocol is connection-oriented : When an application uses the TCP protocol to communicate, it must first establish a TCP connection.
  • The TCP connection is a two-way communication : the two ends of the communication are equal, and they can both send and receive data , just like when we make a phone call, we can both speak and listen.

These two important features determine the release process of the TCP connection.

insert image description here
After the data transmission of the TCP connection ends, both the client and the server of the communication can choose to release the current TCP connection . At this time, both the client and the server are in the ESTABLISHED (connection establishment) state , and the TCP connection release starts from this state. We assume that the application process of the client is active Initiate TCP connection release:

  • The first wave : The client actively sends a connection release message ( FIN=1, seq=u ) to the server . The header control bit of the message is FIN=1, which means that its own data has been sent and requests to release the TCP connection . Sequence number seq=u , the value the last byte sequence number of the data transmitted before the client plus 1, and the client enters the FIN-WAIT-1 (termination wait 1) state after sending .

  • The second wave : After receiving the connection release message from the client, the server will send a confirmation message (ACK=1, ack=u+1, seq=v) , the serial number seq=v, the value is the data that has been transmitted before the server The last byte sequence number is increased by 1 , and then the server enters the CLOSE-WAIT (closed wait) state .At this time, the connection from client to server is released, and the TCP connection is in a half-closed state., the client no longer sends data, but the server can still send data to the client . The client enters the FIN-WAIT-2 (termination wait 2) state after receiving the confirmation from the server , and then waits for the server to send a connection release message

  • The third wave : After the server in the CLOSE-WAIT state sends all the data, it actively releases the connection, and the connection release message sent by the server ( FIN=1, ACK=1, seq=W, ack=u+1 ), because In the semi-closed state, the server may have sent some data again, so the value of the serial number is W, and at the same time keep the confirmation number ack=U+1 consistent with the last time. After sending, the server enters the LAST-ACK (final confirmation) state and waits for the client confirm.

  • The fourth wave : After receiving the server connection release message, the client sends a confirmation message (ACK=1, ack=w+1, seq=u+1), and the connection has not been released yet.The client has to wait for the 2MSL time set by the timer before finally entering the CLOSED state. The time MSL is the longest packet lifetime . RFC793 recommends 2 minutes, but in the current network, this time is set to be smaller. That is to say, the client has to wait for 4 minutes to enter the CLOSED state and start the next connection.


5. Why does the client have to wait for 2MSL in the TIME-WAIT state?

  • In order to ensure that the last ACK message sent by the client can reach the server. Because this ACK message may be lost, the server in the LAST-ACK state cannot receive the confirmation of its release of the connection message. If the server times out and retransmits the message, the client can receive it within 2MSL time, confirm it again, and restart the 2MSL timer.

  • Prevent the occurrence of "expired connection request segment", 2MSL time, which can make the message segments within the duration of the connection disappear from the network. When the next TCP connection is established, the last old connection request segment will not appear


6.TCP handshake and the state of each handshake client and server (11 states of Socket)

  • CLOSED : Indicates the initial state .

  • LISTEN : This state indicates that a SOCKET on the server side is in the listening state and can accept connections.

  • SYN_SENT : This state echoes SYN_RCVD. When the client SOCKET performs a CONNECT connection, it first sends a SYN message, then enters the SYN_SENT state , and waits for the server to send the second message in the three-way handshake. The SYN_SENT state indicates that the client has sent a SYN message.

  • SYN_RCVD : This state indicates that a SYN message has been received . Under normal circumstances, this state is an intermediate state during the three-way handshake session of SOCKET on the server side when establishing a TCP connection, which is very short. In this state, after receiving the ACK message from the client, it will enter the ESTABLISHED state.

  • ESTABLISHED : Indicates that the connection has been established.

  • FIN_WAIT_1: The real meaning of the FIN_WAIT_1 and FIN_WAIT_2 states is to wait for the FIN message of the other party . The difference is:

    The FIN_WAIT_1 state means that when the socket is in the ESTABLISHED state, it wants to actively close the connection and sends a FIN message to the other party . At this time, the socket enters the FIN_WAIT_1 state.
    - The FIN_WAIT_2 state means that the socket enters the FIN_WAIT_2 state after the other party responds with ACK. Under normal circumstances, the other party should immediately respond to the ACK message, so the FIN_WAIT_1 state is generally difficult to see, and the FIN_WAIT_2 state can be seen with netstat.

  • FIN_WAIT_2The party that actively closes the link will enter this state after sending out FIN and receiving ACK. Call it a half-connected or half-closed state. The socket in this state can only receive data, not send it.

  • TIME_WAIT : Indicates that the FIN message from the other party has been received , and an ACK message has been sent , and it can return to the CLOSED available state after waiting for 2MSL . If in the FIN_WAIT_1 state, when receiving a message with both the FIN flag and the ACK flag from the other party, it can directly enter the TIME_WAIT state without going through the FIN_WAIT_2 state.

  • CLOSING: This state is special and belongs to a relatively rare state. Under normal circumstances, when you send a FIN message, you should receive (or receive at the same time) the other party's ACK message first, and then receive the other party's FIN message. But the CLOSING state means that after you sent the FIN message, you did not receive the other party's ACK message, but instead received the other party's FIN message. Under what circumstances will this happen? If both parties are closing a SOCKET almost at the same time, then there will be a situation where both parties send FIN messages at the same time, that is, the CLOSING state will appear, indicating that both parties are closing the SOCKET connection.

  • CLOSE_WAIT: This state indicates that it is waiting to be closed . When the other party closes a SOCKET and sends a FIN message to itself, the system will respond with an ACK message to the other party, and then enter the CLOSE_WAIT state. Next, check whether there is any data to send to the other party. If not, you can close the SOCKET and send a FIN message to the other party, that is, close the connection. So in the CLOSE_WAIT state, the connection needs to be closed .

  • LAST_ACK: This state is to passively close one party after sending a FIN message, and finally wait for the other party's ACK message. After receiving the ACK message, it can enter the CLOSED available state.


7. The difference between TCP and UDP

  • TCP is connection-oriented (for example, you need to dial up to establish a connection before making a call); UDP is connectionless , that is, you do not need to establish a connection before sending data
  • TCP provides reliable service . That is to say, the data transmitted through the TCP connection is error-free, not lost, not repeated, and arrives in order;
    UDP does its best to deliver, that is, does not guarantee reliable delivery
  • TCP is oriented to byte streams . In fact, TCP regards data as a series of unstructured byte streams; UDP is message-oriented
    UDP without congestion control , so network congestion will not reduce the sending rate of the source host (for real-time applications useful, such as IP telephony, real-time video conferencing, etc.)
  • TCP connections can only be point-to-point ; UDP supports one-to-one, one-to-many, many-to-one and many-to-many interactive communications
  • The TCP header overhead is 20 bytes ; the UDP header overhead is small, only 8 bytes
  • The logical communication channel of TCP is a full-duplex reliable channel , while UDP is an unreliable channel

insert image description here


updating…

Guess you like

Origin blog.csdn.net/weixin_45075135/article/details/114686475