preface
The core of this paper lies in the perspective and legend of the problem; The prerequisite is to understand the general process of TCP connections. You can learn more about the details of connection establishment through this document. (1) Describe the process of establishing and disconnecting a TCP connection. (2) which step of the ACCEPT system call occurs in the TCP connection;
Information transfer Angle
- The IP address and port of the client inform the server of the link.
- The sequence number of the initial count, that is, the ISN, is reversed.
- MSS was negotiated;
- The two sides communicated window size;
State flow Angle
System call perspective
The system calls involved here include: socket, listen, bind, accept, connect, close;
Accept
In this figure, accept is drawn after server ESTABLISH. Here we will explain the accept method in detail. There are two reasons to pay too much attention to accept. This operation can be misleading. When I first looked at TCP Server, I thought accept was part of establishing a connection. The second reason is that Accept is often asked in interviews.
Here’s a look at the ACCEPT API definition in the Linux Manual:
#include <sys/types.h>
#include <sys/socket.h>
int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen);
Copy the code
-
Accpet is used for sockets that need to establish connections. In other words, accept is not required for UDP servers. Accept is required for TCP transport protocols that require connections.
-
Accept is 1- to obtain the first pending connection from the queue listening on the socket, 2- to wait for a pending connection, and 3- to create a new socket that is already connected and return the SOCKET fd. Socket: accept socket: accept socket: accept socket: accept socket: accept Accept creates a connected socket. We can see that the connection itself already exists in the queue, and that the accept actually takes a connection out of the queue, which explains why the accept actually happens after three handshakes. In practice, a listening socket has two queues. One queue processes the connection in the handshake process and the other queue processes the connection that has been established. Accept receives the connection that the handshake has been completed and is waiting for the user process to process.
-
Accept blocks can be controlled using the listening socket option: In most cases, accept blocks the caller process until it returns a connected socket. However, this can be controlled. When the socket is set to nonblocking, if there is no connection to accept, it will not block and return error.
A sublimation
A connection is established by (1) an action, which causes (2) an exchange of information, which causes (3) a change of state. Here, “action-information-state” can also be regarded as a structural analysis method, which can help design state machines in various scenarios: what core operations and core information changes should be a state.
Hopefully these three perspectives will help you understand TCP better 🙂