[Script Development] Section 1 Python Socket Communication

Overview of Communication Protocols

basic concept

通信协议是指双方实体完成通信或服务所必须遵循的规则和约定.
Multiple data communication systems in different geographic locations interconnected through communication channels and devices must have a common language in order to enable them to work together to achieve information exchange and resource sharing. What, how, and when to communicate must follow certain mutually acceptable rules. This rule is the communication protocol.

TCP/IP Reference Model

Network models generally have seven-layer model, five-layer model and four-layer model, among which the four-layer model refers to the TCP/IP reference model (TCP/IP protocol suite).
The four-layer model is from bottom to top: network interface layer, network layer, transport layer and application layer.

Layer related agreement
application layer DNS、FTP、TELNET、HTTP、SMTP等
transport layer TCP、UDP
Network layer IP, ICMP, ARP, etc.
network interface layer The medium through which the content of the protocol is transmitted

TCP protocol

principle

Transmission Control Protocol (TCP, Transmission Control Protocol) is a transmission protocol specially designed to provide reliable end-to-end byte streams on unreliable Internet networks.

An internetwork is very different from a single network because different parts of the internetwork may have vastly different topologies, bandwidths, latencies, packet sizes, and other parameters. The design goal of TCP is to be able to dynamically adapt to these characteristics of the Internet, and to be robust in the face of various failures.

Reliable, pipe-like connections are often required between application layers of different hosts, but the IP layer does not provide such a flow mechanism, but provides unreliable packet switching.

The application layer sends a data stream represented by 8-bit bytes for inter-network transmission to the TCP layer, and then TCP divides the data stream into segments of appropriate length (usually determined by the data link layer of the network to which the computer is connected) Maximum Transmission Unit (MTU) limitation). After that, TCP passes the result packet to the IP layer, and it transmits the packet to the TCP layer of the receiving end entity through the network. In order to ensure that no packet loss occurs, TCP gives each packet a sequence number, and the sequence number also ensures that the packets transmitted to the receiving end entity are received in order. The receiving entity then sends back a corresponding acknowledgment (ACK) for the successfully received packet; if the sending entity does not receive an acknowledgment within a reasonable round-trip delay ( RTT ), then the corresponding packet is assumed to have been received Lost ones will be retransmitted. TCP uses a checksum function to check data for errors; checksums are calculated both when sending and when receiving.

Every machine that supports TCP has a TCP transport entity. The TCP entity can be a library process, a user process, or part of the kernel. In all these cases, it manages the TCP flow, and the interface with the IP layer. The TCP transport entity accepts the user data stream of the local process and divides them into segments of no more than 64KB (actually removing the IP and TCP headers, usually no more than 1460 data bytes), and each segment is in the form of a separate IP datagram send. When datagrams containing TCP data arrive at a machine, they are handed off to the TCP transport entity, which reconstructs the original byte stream. For simplicity, we sometimes just use "TCP" to refer to the TCP transport entity (a piece of software) or the TCP protocol (a set of rules). According to the context semantics, you should be able to deduce its actual meaning very clearly. For example, in the sentence "the user hands over data to TCP", it is clear that this refers to the TCP transport entity. [2]

The IP layer does not guarantee that the datagram will be delivered correctly to the receiver, nor does it indicate how fast the datagram will be sent. It is TCP that is responsible for sending datagrams fast enough to use network capacity without causing network congestion: and, after TCP times out, to retransmit undelivered datagrams. Even if the datagrams are delivered correctly, there may be out-of-order problems. This is also the responsibility of TCP. It must reassemble the received datagrams into the correct order. In short, TCP must provide good performance in reliability, which is what most users expect and IP does not provide.

features

TCP is a wide area network-oriented communication protocol, the purpose of which is to provide a communication method between two communication endpoints with the following characteristics when communicating across multiple networks:

(1) Flow-based approach;

(2) Connection-oriented;

(3) Reliable communication methods;

(4) When the network condition is not good, try to reduce the bandwidth overhead of the system due to retransmission;

(5) Communication connection maintenance is oriented to the two endpoints of communication, regardless of intermediate network segments and nodes.

In order to meet these characteristics of the TCP protocol, the TCP protocol has made the following regulations:

  • Data Fragmentation: Fragmentation of user data at the sending end, reorganization at the receiving end, TCP determines the size of the fragmentation and controls fragmentation and reassembly;

  • Arrival confirmation: when the receiving end receives the fragmented data, it sends an acknowledgment to the sending end according to the sequence number of the fragmented data;

  • Timeout retransmission: The sender starts the timeout timer when sending fragments. If the corresponding confirmation is not received after the timer expires, the fragments are resent;

  • Sliding window: The size of the receiving buffer space of each party in the TCP connection is fixed, and the receiving end only allows the other end to send the data that the receiving end buffer can accept. TCP provides flow control on the basis of sliding windows to prevent faster hosts from causing slower Host buffer overflow;

  • Out-of-order processing: TCP fragments transmitted as IP datagrams may arrive out of order, and TCP will reorder the received data and deliver the received data to the application layer in the correct order;

  • Repeated processing: TCP fragments transmitted as IP datagrams will be repeated, and the receiving end of TCP must discard the repeated data;

  • Data checksum: TCP will maintain the checksum of its header and data, which is an end-to-end checksum to detect any changes in the data during transmission. If there is an error in the checksum of the received fragment, TCP will discard the fragment, and does not confirm receipt of the segment, causing the peer to time out and resend.

three handshake

The so-called three-way handshake is the establishment of a TCP connection. This connection must be opened actively by one party and passively opened by the other party. The following is an illustration of the client actively initiating a connection:
insert image description here
the client that actively opens the connection before the handshake ends the CLOSED phase, and the passively opened server also ends the CLOSED phase and enters the LISTEN phase. Then start the "three-way handshake":
(1) First, the client sends a TCP message to the server, in which: the
flag bit is SYN, which means "request to establish a new connection"; the sequence number is Seq=X (X is generally 1); then The client enters the SYN-SENT phase.
(2) After the server receives the TCP message from the client, it ends the LISTEN phase. And return a section of TCP message, in which: the flag is SYN and ACK, which means "confirm that the sequence number of the message Seq of the client is valid, the server can normally receive the data sent by the client, and agree to create a new connection" (that is, tell the client, the server Received your data); the serial number is Seq=y; the confirmation number is Ack=x+1, which means that the serial number Seq of the client is received and its value is added 1 as the value of its own confirmation number Ack; then the server enters SYN-RCVD stage.
(3) After the client receives the TCP message from the server confirming the receipt of the data, it is clear that the data transmission from the client to the server is normal, and the SYN-SENT stage is ended. And return the last TCP message. Among them: the flag is ACK, which means "confirmation of receiving the signal from the server agreeing to connect" (that is, tell the server, I know you have received the data I sent); the sequence number is Seq=x+1, which means receiving the confirmation from the server Ack, and use its value as its own serial number value; the confirmation number is Ack=y+1, indicating that it has received the server-side serial number Seq, and its value plus 1 is used as the value of its own confirmation number Ack; then the client enters ESTABLISHED stage. After the server receives the TCP message "acknowledging receipt of server data" from the client, it is clear that the data transmission from the server to the client is normal. End the SYN-SENT stage and enter the ESTABLISHED stage.

In the TCP message transmitted by the client and the server, the values ​​of the confirmation number Ack and the sequence number Seq of both parties are calculated on the basis of each other's Ack and Seq values, which ensures the continuity of TCP message transmission . Once the TCP message sent by a certain party is lost, the "handshake" cannot be continued, thus ensuring the successful completion of the "three-way handshake".
Afterwards, the client and server perform normal data transmission. This is the "three-way handshake" process.

waved four times

The so-called four-time wave is the release (release) of the TCP connection. The release of the connection must be actively released by one party and passively released by the other party. The following is an illustration of the client actively initiating the release of the connection:

(1) First, the client wants to release the connection, and sends a TCP message to the server, in which:

The flag bit is FIN, which means "request to release the connection"; the sequence number is Seq=U; then the client enters the FIN-WAIT-1 stage, which is the half-closed stage. And stop sending data in the direction from the client to the server, but the client can still receive the data transmitted from the server. Note: What is not sent here is the data transmitted during normal connection (non-confirmation message), not all data, so the client can still send ACK confirmation message.

(2) After the server receives the TCP message sent from the client, it confirms that the client wants to release the connection, then the server ends the ESTABLISHED stage, enters the CLOSE-WAIT stage (half-closed state) and returns a TCP message, in:

The mark bit is ACK, which means "received the request to release the connection sent by the client"; the sequence number is Seq=V; the confirmation number is Ack=U+1, which means that the sequence number is changed on the basis of receiving the client message. Add 1 to the value of Seq as the value of the confirmation number Ack of this segment of the message; then the server starts to prepare to release the connection from the server to the client. After the client receives the TCP message sent from the server, it confirms that the server has received the release connection request from the client, and then the client ends the FIN-WAIT-1 phase and enters the FIN-WAIT-2 phase

The first "two waves" not only let the server know that the client wants to release the connection, but also let the client know that the server understands its request to release the connection. Therefore, it can be confirmed that the connection from the client to the server is closed

(3) After the server sends the ACK confirmation message, after the CLOSED-WAIT stage, it is ready to release the connection from the server to the client, and sends a TCP message to the client again, in which:

The flag bits are FIN, ACK, which means "the connection is ready to be released". Note: The ACK here is not a confirmation message to confirm receipt of the server-side message. The sequence number is Seq=W; the confirmation number is Ack=U+1; it means that on the basis of receiving the client message, add 1 to its sequence number Seq value as the value of the confirmation number Ack of this segment of the message. Then the server ends the CLOSE-WAIT stage and enters the LAST-ACK stage. And stop sending data in the direction from the server to the client, but the server can still receive data transmitted from the client.

(4) The client receives the TCP message sent from the server, confirms that the server is ready to release the connection, ends the FIN-WAIT-2 stage, enters the TIME-WAIT stage, and sends a message to the server ,in:

The flag bit is ACK, which means "received a signal that the server is ready to release the connection". The sequence number is Seq=U+1; it means that on the basis of receiving the server-side message, the value of its confirmation number Ack is used as the value of the sequence number of this segment of the message. The confirmation number is Ack=W+1; it means that on the basis of receiving the server-side message, the sequence number Seq value is used as the value of the confirmation number of this segment of the message. Then the client starts to wait for 2MSL in the TIME-WAIT phase

After receiving the TCP message from the client, the server ends the LAST-ACK phase and enters the CLOSED phase. This officially confirms the closure of the connection in the server-to-client direction.

After the client waits for 2MSL, it ends the TIME-WAIT stage and enters the CLOSED stage, thus completing the "four waved".

The last "wave twice" not only let the client know that the server is ready to release the connection, but also let the server know that the client knows that it is ready to release the connection. Therefore, it can be confirmed that the connection from the server to the client is closed, thus completing the "four waved".

Like "three waves", in the TCP message transmitted by the client and the server, the values ​​of the confirmation number Ack and the sequence number Seq of both parties are calculated on the basis of each other's Ack and Seq values, which ensures that The continuity of TCP message transmission, once the TCP message sent by a certain party is lost, it is impossible to continue "waving", so as to ensure the smooth completion of "four times waving".

UDP protocol

principle

UDP is the abbreviation of User Datagram Protocol, the Chinese name is User Datagram Protocol, which is a connectionless transport layer protocol in the OSI (Open System Interconnection, Open System Interconnection ) reference model , providing transaction-oriented simple and unreliable information transmission services , IETF RFC 768 [1] is the official specification of UDP. The protocol number of UDP in IP packets is 17.

The UDP protocol, like the TCP protocol, is used to process data packets. In the OSI model, both are located at the transport layer , which is the upper layer of the IP protocol. UDP has the disadvantages of not providing data packet grouping, assembling and sorting of data packets, that is to say, after the message is sent, it is impossible to know whether it has arrived safely and completely. UDP is used to support network applications that need to transfer data between computers . Numerous client/server network applications , including network video conferencing systems, need to use the UDP protocol. The UDP protocol has been used for many years since its inception. Although its original brilliance has been overshadowed by some similar protocols, even today UDP is still a very practical and feasible network transport layer protocol.

Many applications only support UDP, such as: multimedia data flow, do not generate any additional data, and do not retransmit even if it is known that there is a damaged packet. When emphasizing transmission performance rather than transmission integrity, such as: audio and multimedia applications, UDP is the best choice. UDP is also a good choice when the data transmission time is so short that the previous connection process becomes the main body of the entire traffic.

features

UDP is a connectionless protocol , the source and the destination do not establish a connection before transmitting the data, it simply grabs the data from the application when it wants to transmit, and throws it on the network as fast as possible. At the sending end, the speed at which UDP transmits data is only limited by the speed at which the application generates data, the capabilities of the computer, and the transmission bandwidth; Read a message segment.

Since the transmission of data does not establish a connection, there is no need to maintain the connection state, including the state of sending and receiving, so one server can transmit the same message to multiple clients at the same time.

The header of the UDP packet is very short, only 8 bytes. Compared with the 20-byte packet of TCP, the additional overhead of UDP is very small.

Throughput is not regulated by the congestion control algorithm, only limited by the rate at which the application software generates data, the transmission bandwidth, and the performance of the source and end hosts.

UDP is packet-oriented. The sender's UDP sends the message to the application program, and after adding the header, it is delivered to the IP layer. The boundaries of these packets are neither split nor merged, but the boundaries of these packets are preserved, so the application needs to choose an appropriate packet size.

Although UDP is an unreliable protocol, it is an ideal protocol for distributing information. For example, reporting on the stock market, displaying airline information, and more on the screen. UDP is also used in Routing Information Protocol RIP (Routing Information Protocol) to modify the routing table. In these applications, if a message is lost, it will be replaced by a new message after a few seconds. UDP is widely used in multimedia applications.

protocol comparison

TCP is a connection-oriented transmission control protocol, and UDP provides a connectionless datagram service; TCP has high reliability, ensuring the correctness of transmitted data without loss or disorder; UDP does not establish a connection before transmitting data, which is wrong The datagram is inspected and modified without waiting for the response of the other party, so there will be packet loss, duplication, and disorder, and the application needs to be responsible for all the work in terms of transmission reliability;

UDP has better real-time performance and higher work efficiency than TCP protocol; UDP segment structure is simpler than TCP segment structure, so the network overhead is also small. The TCP protocol can ensure that the receiving end receives the byte stream sent by the sending end without error, and provides reliable communication services for applications. Communication systems that require high reliability often use TCP to transmit data. For example, HTTP uses TCP for data transmission.

protocol communication

socketCommunication

Work icon:
![[Pasted image 20220303101552.png]]

Server side: Initialize Socket–>Bind port–>Port monitoring–>Call accept blocking–>Wait for connection
Client: Initialize Socket–>Connect to server–>Data interaction–>Communication ends

UDP Socket simple communication

# -*- coding: utf-8 -*-  
# @Time    : 2022/3/3 10:33 上午  
# @Author  : Qixi  
# @File    : Client.py  
  
import socket  
# 初始化客户端  
client = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)  
# 和服务端保持一致  
client.sendto(input("输入:").encode('GBK'),('localhost',1234))  
recv_data,ip_port = client.recvfrom(1024)  
print("来自服务端的消息:{} ".format(recv_data.decode('GBK')))  
print("服务端IP即端口",ip_port)  
  
client.close()
# -*- coding: utf-8 -*-  
# @Time    : 2022/3/3 10:21 上午  
# @Author  : Qixi  
# @File    : Server.py  
  
# 1、导入模块  
import socket  
# 2、创建套接字  
server = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)  
# 3、绑定服务端的IP和端口  
server.bind(("localhost",1234))  
# 4、接收客户端的信息  
recv_data,ip_port = server.recvfrom(1024)  
# 5、解码接收到的信息并显示  
recv_str = recv_data.decode("GBK")  
print("接收到客户端发送的数据: {}".format(recv_str))  
print("客户端IP及端口",ip_port)  
# 6、回复信息给客户端  
server.sendto("服务端收到信息啦!".encode("GBK"),ip_port)  
# 7、关闭套接字  
server.close()

insert image description here

TCP Socket implements chat

# -*- coding: utf-8 -*-  
# @Time    : 2022/3/3 10:33 上午  
# @Author  : Qixi  
# @File    : Client.py  
  
import socket  
# 初始化客户端  
client = socket.socket(socket.AF_INET,socket.SOCK_STREAM)  
# 和服务端保持一致  
client.connect(('localhost',1234))  
while True:  
    msg = input("输入待发送数据: ")  
    client.send(msg.encode('utf-8'))  
    data = client.recv(1024)  
    print("收到来自服务器的数据:",data.decode())  
client.close()
# -*- coding: utf-8 -*-  
# @Time    : 2022/3/3 10:21 上午  
# @Author  : Qixi  
# @File    : Server.py  
  
import socket  
# 初始化服务端  
server = socket.socket(socket.AF_INET,socket.SOCK_STREAM)  
# 绑定监听端口  
server.bind(('localhost',1234))  
server.listen(5)  
while True:  
    conn,addr = server.accept()  
    # print(conn,addr)  
 print("通信成功连接!!")  
    while True:  
        try:  
            data = conn.recv(1024)  
            print("收到来自客户端的数据:{}".format(data.decode()))  
            sdata = input("输入:")  
            conn.send(sdata.encode('utf-8'))  
        except Exception as e:  
            print("连接关闭!")  
            break  
 conn.close()

insert image description here

TCP Socket implements command execution

# -*- coding: utf-8 -*-  
# @Time    : 2022/3/3 10:33 上午  
# @Author  : Qixi  
# @File    : Client.py  
  
import socket  
import os  
# 初始化客户端  
import sys  
  
client = socket.socket(socket.AF_INET,socket.SOCK_STREAM)  
# 和服务端保持一致  
client.connect(('localhost',1234))  
while True:  
    data = client.recv(1024)  
    data = data.decode('utf-8')  
    print("收到来自服务器命令:",data)  
    if data == "exit":  
        sys.exit(0)  
    else:  
        msg = os.popen(data).read()  
        client.send(msg.encode('utf-8'))  
client.close()
# -*- coding: utf-8 -*-  
# @Time    : 2022/3/3 10:21 上午  
# @Author  : Qixi  
# @File    : Server.py  
  
import socket  
# 初始化服务端  
import sys  
  
server = socket.socket(socket.AF_INET,socket.SOCK_STREAM)  
# 绑定监听端口  
server.bind(('localhost',1234))  
server.listen(5)  
while True:  
    conn,addr = server.accept()  
    # print(conn,addr)  
 print("主机上线啦!!")  
    while True:  
        try:  
            sdata = input("输入命令:")  
            if sdata == 'quit':  
                sys.exit(0)  
            else:  
                conn.send(sdata.encode('utf-8'))  
                data = conn.recv(1024)  
                print("收到来自客户端的数据:{}".format(data.decode('utf-8')))  
        except Exception as e:  
            print("连接关闭!")  
            break  
conn.close()

Running effect: support basic command echo, support exit. The server enters the command, and the client accepts the command and executes it
insert image description hereinsert image description here

TCP/UDP Socket programming difference

Server

  • UDP does not need to enable monitoring
  • TCP uses the accept method to receive information, and UDP uses the recvfrom method to receive information
  • The TCP steps are more complicated

client

  • UDP socket attribute is optional
  • TCP sends data using send, recv or read, write methods
  • UDP uses the sendto method to send data

reference

https://baike.baidu.com/item/UDP
https://baike.baidu.com/item/TCP/33012
https://baijiahao.baidu.com/s?id=1654225744653405133
https://www.runoob.com/python/python-socket.html
https://www.cnblogs.com/zhangyingai/p/7097922.html
https://www.cnblogs.com/zhangyingai/p/7097922.html
https://www.cnblogs.com/LUO77/p/5801977.html

Guess you like

Origin blog.csdn.net/weixin_54430466/article/details/123270101
Recommended