Python Network Programming-socket programming

Source: Internet
Author: User

Three-time handshake of TCP in the socket to establish a connection

We know that TCP establishes a connection for a "three-time handshake", which is the exchange of three groupings.

The process is as Follows:

    • The client sends a SYN J to the server
    • The server responds to a SYN K to the client and acknowledges the SYN J ACK j+1
    • The client then wants the server to send a confirmation ack k+1

Only three handshake is done, but this three handshake takes place in the socket of the several functions? Please see:

Figure 1, TCP Three-time handshake sent in socket

As you can see, when the client calls connect, the connection request is triggered, The SYN J packet is sent to the server, then connect enters the blocking state, the server hears the connection request, receives the SYN J packet, calls the Accept function to receive the request to send SYN K to the client, ACK j+ 1, then accept into the blocking state, the client receives the server SYN k, after the ACK j+1, then connect returns, and the SYN K confirmation, the server received an ACK k+1, accept return, This three times the handshake is completed, the connection is Established.

Summary: The Client's Connect returns in the second time of the three handshake, while the Server-side accept is returned for the third time in the Three-time handshake.

5, the socket TCP Four handshake release connection detailed

The above describes the Three-time handshake creation process of TCP in the socket and the socket function Involved. Now that we introduce the four-time handshake in the socket to release the connection, see:

Figure 2, TCP Four-time handshake sent in socket

The process is as Follows:

    • An application process first calls close to actively close the connection, when TCP sends a FIN M;
    • After the other end receives fin m, perform a passive shutdown to confirm the Fin. Its reception is also passed as a file terminator to the application process, because the receive of fin means that the application process can no longer receive additional data on the corresponding connection;
    • After some time, the application process that receives the file terminator calls close to close its socket. This causes its TCP to send also a fin N;
    • This fin is received by the source send side TCP to confirm it.

So there is a fin and an ack in each Direction.

Python Network Programming-socket programming

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.