Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to solve the sticky packet problem of Python data Transmission

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "how to solve the sticky packet problem of Python data transmission". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn how to solve the sticky packet problem of Python data transmission.

The principle of 1.socket sticky package problem

Sticky package: there is no clear dividing line between data and data, so that the data can not be read correctly.

If the application data wants to send data, it must give the data to the operating system, and the operating system needs to provide data transmission services for all applications at the same time, which means that it is impossible to send the application data right away, so it is necessary to provide a buffer for the program to store data temporarily.

When the data is sent quickly and two pieces of data are in the buffer, the operating system may send the two data to the receiver. There is no dividing line between the data, and the receiver will mistake it for a piece of data.

2.UDP protocol

UDP is based on data packet when sending and receiving data, that is, a packet is sent one by one. There is a clear boundary between the packet and the packet, and it is also an independent packet when it reaches the buffer of the other side. The problems with this approach:

The length of data sent by ① varies with each operating system. If the data exceeds the limit, it cannot be sent.

When the ② receiver receives the data, if the cache capacity provided by the application is less than the length of the packet, the data will be lost, and the buffer size can not be infinite.

This means that UDP will not have sticky packet problems, but will lose data and be unreliable.

3.TCP protocol

TCP adds a set of verification rules to ensure data integrity. Data exceeding the maximum length of TCP packets is divided into multiple TCP packets, and each TCP packet is assigned a sequence number when transmitting data. After receiving TCP packets, the receiver reassembles the packets sequentially. The reorganized data is all binary data, and there is no obvious demarcation between the binary data received each time. Based on this working mechanism, the sticky package problem occurs in TCP in three cases:

① when a single packet is small, the receiver may read the data of multiple packets at one time

② when the overall data is large, the receiver may read only part of the contents of one packet at a time

③ in addition, in order to improve efficiency, TCP protocol adds an optimization mechanism, which will merge the data with small data and short transmission interval, and this mechanism will also cause the sender to glue two packets together.

In other words, TCP transmits data reliably, but it will stick to the packet.

4. Sticky packet appeared by the sender

Server side:

From socket import * server_socket = socket (AF_INET,SOCK_STREAM) server_socket.bind ((', 8080)) server_socket.listen (5) new_socket,client_addr = server_socket.accept () data1 = new_socket.recv (1024) data2 = new_socket.recv (1024) print ("first data received:" data1) print ("second data received:", data2) new_socket.close () server_socket.close ()

Client:

From socket import * client_socket = socket (AF_INET,SOCK_STREAM) client_socket.connect ('10.175.193.126) client_socket.send (' hello'.encode ('utf-8')) client_socket.send (' word'.encode ('utf-8')) client_socket.close ()

The data received by the server:

Because the interval between two pieces of data sent by the client is too short and the packet is too small, it is mistaken for a piece of data by the server.

5. Sticky packets appeared by the receiver

Server side:

From socket import * import time server_socket = socket (AF_INET,SOCK_STREAM) server_socket.bind ((', 8080)) server_socket.listen (5) new_socket,client_addr = server_socket.accept () print ("connection successful!" , client_addr) data1 = new_socket.recv (3) # receive only three bytes at a time, receive incomplete time.sleep (6) print ("first data received:", data1) data2 = new_socket.recv (10) # receive the first unreceived data, if space is available, it will continue to receive new data print ("second data received:", data2) new_socket.close () server_socket.close ()

Client:

When from socket import * # causes the client to send multiple packets through the time module, the time interval becomes longer import time client_socket = socket (AF_INET,SOCK_STREAM) client_socket.connect (('10.175.193.126 packets 8080)) client_socket.send (' hello'.encode ('utf-8')) time.sleep (5) # makes the current thread sleep for 5 seconds client_socket.send (' word'.encode ('utf-8')) client_socket.close ()

The data received by the server:

6. The cause of formation of sticky bag

Sticky packets appear on the ① server side: the receiver does not know the boundaries between messages and does not know how many bytes of data a message needs to extract.

Sticky packets appear on the ② client: when TCP sends packets with less data and a short interval, it will merge several packets together.

At this point, I believe that everyone on the "Python data transmission sticky packet problem how to solve" have a deeper understanding, might as well to the actual operation of it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report