TCP three-way handshake and waved briefly four times

TCP, Transmission Control Protocol (English: Transmission Control Protocol, abbreviated as TCP) is connection-oriented, reliable transport layer protocol based on a stream of bytes defined by the IETF RFC 793.

TCP

TCP communication to go through to create a connection, data transmission, terminates the connection three steps.

TCP is connection-oriented communication and reliable. Communication must be established before the two sides formally communicate one to one reliable connection and distribution of certain system kernel resources, double the data connection through this, and when the communication ended after the twin-engine must be disconnected in order to free up system resources.

TCP uses to send response mechanism, each successful transmission are dependent on the transmission and response. If no response is received at a certain time, it will be re-issued. And to packet loss, each packet has a sequence number, but also ensures that the serial number sequentially transmitted to the receiving end of the packet reception entity.

TCP client implementation code:

from socket import *

# 创建socket
tcp_client_socket = socket(AF_INET, SOCK_STREAM)

# 目的信息
server_ip = input("请输入服务器ip:")
server_port = int(input("请输入服务器port:"))

# 链接服务器
tcp_client_socket.connect((server_ip, server_port))

# 提示用户输入数据
send_data = input("请输入要发送的数据:")

tcp_client_socket.send(send_data.encode("gbk"))

# 接收对方发送过来的数据,最大接收1024个字节
recvData = tcp_client_socket.recv(1024)
print('接收到的数据为:', recvData.decode('gbk'))

# 关闭套接字
tcp_client_socket.close()
复制代码

TCP service side code:

from socket import *

# 创建socket
tcp_server_socket = socket(AF_INET, SOCK_STREAM)

# 本地信息
address = ('', 7890)

# 绑定
tcp_server_socket.bind(address)

# 使用socket创建的套接字默认的属性是主动的,使用listen将其变为被动的,这样就可以接收别人的链接了
tcp_server_socket.listen(128)

# 如果有新的客户端来链接服务器,那么就产生一个新的套接字专门为这个客户端服务
# client_socket用来为这个客户端服务
# tcp_server_socket就可以省下来专门等待其他新客户端的链接
client_socket, clientAddr = tcp_server_socket.accept()

# 接收对方发送过来的数据
recv_data = client_socket.recv(1024)  # 接收1024个字节
print('接收到的数据为:', recv_data.decode('gbk'))

# 发送一些数据到客户端
client_socket.send("thank you !".encode('gbk'))

# 关闭为这个客户端服务的套接字,只要关闭了,就意味着为不能再为这个客户端服务了,如果还需要服务,只能再次重新连接
client_socket.close()
复制代码

TCP three-way handshake

When the client to keep the server communication when it will be ready resources through three-way handshake.

Hereinafter to differentiate SYN and ACK request (SYN) and the response (ACK). J, K represents a random identification only, and not the actual transmission J, K.

  1. And the first client sends a request with a data packet to the server with the random labeled SYN J. To add a marker response on the front SYN.

  2. After the service being terminated, revc will de-clog, and assign a new socket to send and receive data. Because TCP requires each request must have a response, so the server will send the client over the random mark plus a J + 1 is sent to the client tells the server that it has a ready resource. To add a marker response in the previous ACK.

  3. At this time, the server may also need to verify that the client is reliable, and the client also needs to prepare resources, so the server sends the same packet with a random-labeled (SYN K) to the client.

  4. When the server receives the client packet send confirmations packets to the server, the server sends it over to a plus K (ACK K + 1) is sent to the server.

Because the server response and the server determines that the client is sent to the client, and there is no delay or intermediate without hair, the service client with an acknowledgment packet transmitted just respective client server to a client connected to a packet a. Two steps combined into one step, so called three-way handshake.

When this three-way handshake is completed the parties will confirm that they are reliable, they can communicate.

TCP four wave

When the end of both the client and server communications, in order to ensure the release of system resources, the two sides need to disconnect the fourth wave, the release of resources. Since the socket socket is full duplex, i.e. the transceiver can be performed simultaneously, so when a socket is closed off when the need for two transmit and receive channels.

Sending data packets with the same three-way handshake is sent as a random mark, be added to an operation at the time of the response.

  1. Disconnect usually first initiated by the client, when the client calls close the socket connection is closed will send packets to the server.

  2. Server receives the client packet when it will return to the customer a packet end, to confirm receipt of the client closes the connection request.

  3. recv service side of the de-clog, will give the client sends a packet to inform the client socket will be closed when the server sends the call to the close of time. But this time the server resources will remain until you receive confirmation packet client.

  4. When the client receives the packet to give the server sends an acknowledgment packet, then the client resources will be retained for a period of time.

Under normal circumstances, the client and server will receive twice the response, thereby closing the two channels socket of the transceiver.

Server resource reservation because the time-out if the server TCP packets sent have not received a response from the client, then the packet is sent to the client again. And also because the client resource reservation time if the server does not receive the packet back to the sending data over again, so they need to receive. If a shutdown request initiated by the client, the client's resources usually reserved 2msl is time to accept two data packets, increasing the probability of receiving the service end retransmission packet, if outdated will close the socket to release resources.

Four waved off request in the server sent to the client's response to the client and sends an acknowledgment packet off, not like three-way handshake is as close together because the server calls the close timing uncertain, and if the client first launched close, so long the client will wait twice as much data packet transmission. The server is bound port, without releasing resources can not be served again, and the client is a random port will not be affected by this restriction.

to sum up

When communicating via TCP handshake first ready resources through three, and then to communicate. There is also a communication process to back, each request corresponding to the primary response. When the end of the communication will be waved through four close connection, free system resources.

Guess you like

Origin blog.csdn.net/weixin_33692284/article/details/91376945