[Linux] C language Linux system programming-11 TCP communication statuses, linux-tcp
Three-way handshake
The client initiates a SYN request, and the client is in the SYN_SENT State (the first handshake)
After the server receives the message, it changes from LISTEN to SYN_REVD and replies to the client. After the client receives the response, it is in the state of ESTABLISHED. This State indicates that the client is ready for communication (the second handshake)
The client replies to the server after receiving the second handshake response. The server is also in ESTABLISHED after receiving the response, indicating that the handshake is successful and the server can communicate (the third handshake)
Data Transmission
The client and server are in the communication status and will not change.
Four Waves
The client actively sends the FIN request to close it. The client is in the FIN_WAIT_1 status (transient) (the first wave)
The server is in the CLOSE_WAIT status (semi-closed) After receiving the message and responds (Second Wave)
The client is in the FIN_WAIT_2 status after receiving the request, waiting for the server to send a close request.
Server sends a FIN disconnection request immediately and is in the LAST_ACK (Third Wave)
After receiving the message and responding, the client is in the TIME_WAIT state. This is the last State at the end of the active disconnection, meaning it will wait for a certain period of time (2MSL-1min ), the status changes to CLOSED after waiting (fourth wave)
11 TCP statuses
CLOSED initial status
1. Establish a three-way handshake
Client server
(1) SYN_SENT ======> (2) LISTEN
(4) ESTABLISHED <========= (3) SYN_RCVD
(5) ========> (6) ESTABLISHED
2. Data Transmission Phase
3. Disconnect and wave four times
(1) FIN_WAIT_1 ========> (2) CLOSE_WAIT
(4) FIN_WAIT_2 <=========== (3)
(6) TIME_WAIT <=========== (5) LAST_ACK
(7) ==========>
Run the netstat command to view various statuses:
1.
2.
3.
4.
The server listens to port 1234 of the Local Machine and connects the client. The TIME_WAIT status is maintained for two MSL durations, that is, it is automatically disabled in 1-4 minutes.
Reference code:
Client. c
# Include <stdio. h> # include <string. h> # include <stdlib. h> # include <unistd. h> # include <arpa/inet. h> # include <sys/socket. h> int main () {// create socket int sock = socket (AF_INET, SOCK_STREAM, 0); sleep (20); // send it to the server (specific IP address and port) initiate the request struct sockaddr_in serv_addr; memset (& serv_addr, 0, sizeof (serv_addr); // fill serv_addr.sin_family = AF_INET with 0 for each byte; // use the IPv4 address serv_addr.sin_addr.s_addr = inet_addr ("127.0.0.1"); // The specific IP address serv_addr.sin_port = htons (1234); // port connect (sock, (struct sockaddr *) & serv_addr, sizeof (serv_addr); sleep (20); // read the data returned by the server char buffer [40]; read (sock, buffer, sizeof (buffer) -1); printf ("Message form server: % s \ n", buffer); // sleep (60); // close the socket close (sock ); sleep (60); return 0 ;}
Server. c
# Include <stdio. h> # include <string. h> # include <stdlib. h> # include <unistd. h> # include <arpa/inet. h> # include <sys/socket. h> # include <netinet/in. h> int main () {// create a socket int serv_sock = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP); // bind the socket with the IP address and port to struct sockaddr_in serv_addr; memset (& serv_addr, 0, sizeof (serv_addr); // each byte is filled with 0 serv_addr.sin_family = AF_INET; // use the IPv4 address serv_addr.sin_addr.s_addr = htonl (INA DDR_ANY); // The specific IP address serv_addr.sin_port = htons (1234); // port bind (serv_sock, (struct sockaddr *) & serv_addr, sizeof (serv_addr )); // enter the listening status and wait for the user to initiate the listen (serv_sock, 20); sleep (20); // receives the client request struct sockaddr_in clnt_addr; socklen_t clnt_addr_size = sizeof (clnt_addr ); int clnt_sock = accept (serv_sock, (struct sockaddr *) & clnt_addr, & clnt_addr_size); sleep (20); // send data char str [] = "Hello World! "; Write (clnt_sock, str, sizeof (str); sleep (20); // close socket close (clnt_sock); sleep (20); close (serv_sock ); sleep (20); "5.c" 36L, 1183C 1, 1 Top close (serv_sock); sleep (20); return 0 ;}