Interviewer: How does Socket TCP disconnect?

Release: 2023-08-17 16:10:10
forward
1745 people have browsed it
  • Preface

  • What is Socket?

  • Socket running process

    • Based on TCP

    • Based on UDP

  • ##How Socket TCP establishes a connection

    • In which socket functions does the three-way handshake occur?

  • How does Socket TCP disconnect?

    • Why wait for 2MSL after the fourth wave


##PrefaceWhen it comes to the Internet, everyone will definitely think of TCP, UDP, HTTP, three-handle-four-wave, etc. But when it comes to Socket, everyone may be a little confused. They only know that it will be used in the network, but what exactly is Socket? ? What is a socket? Why is the network inseparable from Socket?

What is Socket?Socket is actually a socket. Most people’s understanding of Socket is that it can implement a simple network communication, but it

"What problems are specifically solved? What is its practical effect? Why does a Socket appear?"

Interviewer: How does Socket TCP disconnect?Socket is actually in the
"application layer A product between the transport layer and the transport layer. It encapsulates many complex operations of the transport layer into some simple interfaces, allowing the application layer to call them to realize process communication in the network. Socket is developed for port communication. The tool is more low-level.

Socket is actually similar to a dishwasher. Its function is to wash dishes (network communication). Without it, you may need to wash the dishes manually (manually call the transport layer and application layer). APIs between each other), but with it, you only need to click the switch and adjust the duration (encapsulating the API). You don’t need it, but without it, washing the dishes (interaction between the application layer and the transport layer ) would become very tedious.

A complete network communication must go through the network cable and network card of the physical transport layer. The IP protocol of the network transport layer can know to transmit the data to Which machine will run different processes in the computer system, so how to Interviewer: How does Socket TCP disconnect?"identify which process the network data in the network card is for"
, this is actually what Socket is designed to solve. .

Socket is"encapsulation of TCP/IP or UDP/IP protocol". Socket itself is actually a calling interface. Through this interface, when we develop network applications, we don't need to worry about how the underlying layer is implemented, reducing the difficulty of development.

Socket running process

Based on TCP

Interviewer: How does Socket TCP disconnect?

Server

  • ##socket(): Indicates creating a socket, and the bottom layer will generate a file descriptor to represent the socket
  • bind(): The port and address used to bind the service are generally fixed here because they need to be specified when the client connects
  • listen(): When the binding is completed, listen will listen to the data packets of this port
  • accept(): It is equivalent to a switch, indicating that I am ready and can accept the request. , but it will be blocked until the client connects successfully
  • read(): Read the content sent by the client
  • write (): The client writes the data to be returned
  • close(): Disconnects,
    「Wave four times」

Client

  • socket(): Indicates creating a socket, and the bottom layer will generate a file descriptor to represent the socket
  • connet(): Indicates connecting to the specified address. Before that, its own port will be randomly created. TCP's
    "Three-way handshake starts from here"
  • write(): The client writes the data to be sent
  • read(): The client reads the data returned by the server Data
  • close(): Disconnect,
    "wave four times" , send disconnection information to the client

Based on UDP

Interviewer: How does Socket TCP disconnect?

I won’t go into details here. In fact, they are very similar. From the flow chart You can see that

Because UDP is stateless, there is no connection for the server, and it will receive the client's request after calling the Recvfrom() method, and will block until the information is received.

How Socket TCP establishes a connection

After the Socket binds the server address, it begins to establish a connection with the server. The way TCP establishes a connection is actually The famous three-time handshake

Interviewer: How does Socket TCP disconnect?
  • First handshake: A's TCP process creates a transmission control block TCB, and then sends a connection request segment to B. Then set the synchronization bit SYN to 1 and select an initial sequence number seq=x. At this time, client A enters the SYN-SENT (synchronization sent) state.
  • Second handshake: B receives the connection request segment. If it agrees to establish the connection, it sends a confirmation to A. In the confirmation message segment, the synchronization bit SYN=1, the confirmation bit ACK=1, the confirmation number ack=x 1, and an initial sequence number seq=y is also selected for itself. At this time, server B enters the SYN-RCVID state.
  • The third handshake: After A receives B’s confirmation, it sends a confirmation to B. Confirmation message ACK=1, confirmation number ack=y 1. At this time A enters the ESTAB-LISHED state. When B receives A's confirmation, it also enters the ESTAB-LISHED state. The connection is established

In which functions of the socket does the three-way handshake occur?

Interviewer: How does Socket TCP disconnect?
  • When the client calls connect, a connection request is triggered and a SYN signal is sent to the server. At this time, connect enters the blocking state;
  • The server monitors Connection request, that is, receiving SYN, calling the accept function to receive it, and entering the blocking state. Before that, it will try its best to use the socket, bind, and listen functions; and then return the relevant syn and ack signals
  • Customer The terminal receives the information from the server. At this time, the connect is completed, the blocking state is released, and the ack signal is sent to the server
  • The server receives the ack, accepts the blocking, and completes the connection

After the connection is established, connect() has been executed, and the server can send data to the client.

How Socket TCP disconnects

Interviewer: How does Socket TCP disconnect?
  • ##First wave: A Send a connection release message first segment, the termination control bit FIN=1 in the segment header, and the sequence number seq=u (equal to the last sequence number of the data sent before A plus 1); then A enters the FIN-WAIT-1 (termination wait 1) state and waits for B's confirmation.
  • The second wave: After B receives A's connection release message segment, it immediately sends a confirmation message segment, the confirmation number ack=u 1, the sequence number seq=v (equal to B The last sequence number of the previously sent data is increased by 1); then B enters the CLOSE-WAIT (closed waiting) state.
  • The third wave: A enters the FIN-WAIT-2 (termination wait 2) state after receiving B's confirmation message segment, and continues to wait for B to send a connection release message segment. ;
    • If B has no data to send, B will send a connection release message segment to A. The termination control bit FIN=1 in the segment header and the sequence number seq=w (Some data may be sent in the semi-closed state), the confirmation number ack=u 1, then B enters the LAST-ACK (last confirmation) state, waiting for A's confirmation.
  • The fourth wave: A receives B's connection release message segment and sends a confirmation. The confirmation bit in the confirmation segment ACK=1, the confirmation number ack=w 1, Serial number seq=u 1; then A enters the TIME-WAIT (time waiting) state. When B receives the confirmation segment again, B enters the CLOSED state.

Why do you have to wait for 2MSL after the fourth wave?

First of all, the time of 2MSL is from the client (A) The timing starts when ACK is sent after receiving FIN. If within the TIME-WAIT time, because the ACK of the client (A) is not transmitted to the server (B), the client (A) receives the FIN message resent by the server (B), then the 2MSL time will be Reset. The reasons for waiting for 2MSL are as follows

  • 1. The original connection data packet disappears
    • If B does not receive its own ACK, it will If the FiN is retransmitted after a timeout, then A receives the retransmitted FIN again and will send ACK again
    • If B receives its own ACK, it will not send any more messages
After the last wave, A does not know whether B has received his message. Including ACK, A needs to wait for both of the above situations. We need to take the maximum value of the waiting time of the two situations to deal with the worst case scenario. The worst case scenario is: going to ACK Message maximum survival time (MSL) The maximum survival time (MSL) of the incoming FIN message. This is exactly 2MSL, which is enough time to make the original connected data packet disappear from the network.

  • 2. Ensure that the ACK can be received by the server and close the connection correctly

Because this ACK may be lost, which will cause the server to not receive it. Confirm the FIN-ACK message. Assume that the client does not wait for 2MSL, but releases the close directly after sending the ACK. Once the ACK is lost, the server cannot enter the closed connection state normally.

The above is the detailed content of Interviewer: How does Socket TCP disconnect?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:Java后端技术全栈
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!