The three-way handshake between the C/S architecture and the B/S architecture and the TCP/IP wave four times

1. C/S architecture and B/S architecture

(1), client (client) and server (server) Two different processes, data exchange.
(2), Browser (website) and server (server) conduct data interaction through IP addresses.
2. INADDR_ANY
represents the local address (localhost), the default is (127.0.0.1), the address used for testing.
The port for server communication and the port for client are not necessarily the same.

Server and client
Server :
1. Establish a socket communication, which is equivalent to the role of the (open) function
2. Fill the structure on the server side: struct sockaddr_in (similar)
3. Bind the network structure information to this network communication fd (by The file descriptor obtained in the first step)
4. Listen to this ip and port: listen
5. Wait for the connection from the client: accept is a blocking function, and the return value is a file descriptor (create a fd for connection communication)
6. As for who Send data first, there is no rule. Both (C/S) are possible, but pay attention to one thing: once it is determined that one party sends data, the other party must receive data
Client :
1. Establish a socket communication, which is equivalent to (open)
2. The structure of the client Fill in: struct sockaddr_in, the client's port (port) has a little relationship with the server's reputation. Fill in the
server 's structure: struct sockaddr_in
3. Bind the network structure information to this network communication fd (file descriptor obtained from the first step) (optional)
4. The client connects to your server connect (fd, the structure of the server)

2. The three-way handshake of TCP/IP waves four times

Three-way handshake:

First, both the client and the server are in an unconnected state, and the client actively requests the server to establish a connection:
the client sets SYN=1 in the message segment and selects a seq=x, (that is, the sequence number of the request message Send this message to the server for x). At this point, the client enters the synchronized sent state (SYN-SEND). The SYN segment cannot carry data, but consumes a sequence number.
After the server receives the request message, if it agrees to establish a connection, it will reply to the message with SYN=1, ACK=1, and select a seq = y, and the confirmation number in the message is x+1, and the sequence number is y. This When the server enters the synchronization received state (SYN-RCVD)
, after the client receives the synchronization confirmation from the server, it sends the confirmation confirmation to the server. Set ACK=1, the confirmation number is y+1, and the sequence number of the message header is x+1. After sending the message, the client enters the connected state (ESTABLISHED).
After the server receives the confirmation from the client, it also enters the connected state.
write picture description here

waved four times

Now assume that both the client and the server are in the connection establishment state, and the client actively disconnects:

1. The client sends a FIN message to the server: FIN=1, sequence number seq=the last byte sequence number transmitted last+1=u, after sending, the client enters the FIN-WAIT-1 state.
2. After the server receives the message, it sends an acknowledgment message: let ACK=1, the acknowledgment sequence number ack = u+1, its own message sequence number seq=v, after sending, the server enters the CLOSE-WAIT state.
3. At this time, the TCP connection enters the connection half-closed state, and the server may also send some data to the client.
4. After the client receives the confirmation from the server, it enters the FIN-WAIT-2 state. Waiting for the server to send a connection release message.
5. If the server has no data to send, release the TCP connection and send a message to the client: let FIN=1, ACK=1, confirmation number ack=u+1, and its own sequence number seq = w (w may be equal to v may also be greater than v), the server enters the LAST-ACK state.

6. After the client receives the connection release message from the server, it sends an acknowledgment to the message, let ACK=1, the confirmation number ack=w+1, its own sequence number seq=u+1, after sending this message, wait for After 2 msl time, enter the CLOSED state.

7. After the server receives the confirmation from the client, it also enters the CLOSED state and revokes the transmission control block.

Client state change: Not connected—–>SYN-SEND—–>ESTABLISHED—–>FIN-WAIT-1—–>FIN-WAIT-2—–>TIME-WAIT—–>CLOSED
Server state change: Not connected— –>SYN-RCVD—–>ESTABLISHED—–>CLOSE-WAIT—–>LAST-ACK—–>CLOSED
write picture description here

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325919309&siteId=291194637