Socket principle exploration

 

1. Socket for network process communication

In the process of using the TCP/IP protocol suite for network communication, the IP address of the network layer can uniquely identify a network host, and the protocol + port of the transport layer can uniquely identify the application process in the host. (IP address, protocol, port) can uniquely identify the process in the network in the network, thus establishing the cornerstone for the communication of the process in the network.

The TCP/IP protocol family provides a programming interface for network communication. Socket is its representative. Socket originated from Unix. Under the Unix philosophy of everything being a file, socket is an implementation of an "open-read/write-close" mode. , the server and the client each maintain a "file". The socket can be regarded as the handle of the file or called the file descriptor. After the connection is established and opened, it can write content to its own file for the other party to read or read the content of the other party. , closes the file when the communication ends.

Socket is an abstraction layer between the application layer and the transport layer. It abstracts the complex operation of the TCP/IP layer into several simple interfaces for the application layer to call the realized process to communicate in the network. The role and position of socket in the communication model are as follows:

 

2. How sockets communicate

The establishment of socket communication requires five elements ( protocol, source IP, source port, destination IP, destination port ). As long as such a five-element combination is guaranteed to be unique, a socket communication connection can be established.

The communication establishment process is roughly as follows: 1) The server initializes the socket, binds with the port (eg 8080), listens on the port, and calls accept to wait for the client to connect. 2) The client initializes the socket, and the client randomly assigns a port (such as 6688) to initiate a connection to the server listening port (8080). After the server IP layer receives the request, it will be handed over to the transport layer protocol according to the specific protocol type. Stack processing, the transport layer is then handed over to the socket layer for processing, the socket layer uses the listening port (8080) as the server port to establish a connection with the client, and creates a new socket instance (this instance contains the client IP, client port 6688, server port Port 8080, server IP) is used to identify the server handle of this connection, and put it into the connection queue (the size of the connection queue is specified by the server when listening, once the number of connections reaches the limit, it will be rejected or delay new connection requests).

After the connection is established, the two ends can transmit data through their respective terminal handle socket instances (because it contains a quintuple combination of socket communication) (the client still sends data to port 8080 of the server), and the transport layer of the destination host receives the data. After the data, it is still processed by the socket layer, and the socket layer can accurately distinguish the attribution relationship between the data packet and the socket instance according to the source/sink pair. The communication diagram is as follows:

 

 

 

 3. Three-way handshake and four-way wave for socket establishment/close connection

The three-way handshake to establish a connection is as follows:



 

 

 The detailed process of these three handshakes is roughly as follows:

  1. The client socket object blocks after the connect call, and this process sends a SYN ( Synchronize Sequence Numbers ).
  2. The server listens to the connection request, and if the request is allowed, it sends an acknowledgment packet ACK for the received SYN to the client, and also sends a SYN itself. At this point accept is still blocked.
  3. After the client receives SYN+ACK, connect returns and sends an ACK confirmation packet to the server. After the server receives it, accept returns and the connection is established successfully.

 The four waves to close the connection are as follows:



 

 The detailed process of these four handshakes is roughly as follows:

  1. One end first calls close to actively close the connection, then TCP sends a FIN M packet to the other end.
  2. The read at the other end returns 0 to indicate that the FIN segment has been received, and it is known that the other end has closed the connection, then first send an acknowledgement packet ACK to the FIN
  3. After sending the confirmation packet, it also calls close to close the connection, causing its TCP to also send a FIN N packet.
  4. The source TCP that received the FIN finally sends an ACK packet to complete the four-way handshake.

Note: After either party calls close(), both transmission directions of the connection are closed and no more data can be sent. If one party calls shutdown(), the connection is in a half-closed state and can still receive data from the other party.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327042306&siteId=291194637