In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
Editor to share with you python how to solve the sticky package problem, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to understand it!
1. Know the size of the data sent and set the size of the reception, so that you can receive all the data exactly. The sticky packet problem is due to the combination of two small packets sent by tcp's optimization algorithm.
This usually happens when several send () are used in succession.
This is the program that remotely executes the cmd command and returns the result. The server side code import structimport socketsk = socket.socket () sk.bind (('127.0.0.1) sk.bind ((' 127.0.0.1)) sk.listen () conn,addr = sk.accept () while True: cmd = input ('> >') conn.send (bytes (cmd) Encoding='utf-8') num = conn.recv (1024). Decode ('utf-8') # data length calculated by the client end conn.send (bytes (' ok',encoding='utf-8')) # when sending a confirmation to prevent sending num, merge ret = conn.recv (num) print (ret.decode ('gbk') conn.close () sk.close () with the following send content
2. Use struct module to solve the adhesion phenomenon.
Solution to the sticky package phenomenon of # tcp Code on server structimport structimport socketsk = socket.socket () sk.bind (('127.0.0.1) sk.bind ((' 127.0.0.1)) sk.listen () conn,addr = sk.accept () while True: cmd = input ('> >') conn.send (bytes (cmd) Encoding='utf-8') # num = conn.recv (1024). Decode ('utf-8') num = conn.recv (1024) # receive data num = struct.unpack (' iMagnum) [0] # to unpack The result of unpacking is that a tuple type takes the first data # conn.send (bytes ('ok',encoding='utf-8')) ret = conn.recv (num) print (ret.decode (' gbk')) conn.close () sk.close () above is all the contents of the article "how to solve the sticky package problem with python". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
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.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.