Processing of sticky packets in network programming (socket) and network programming socket

Source: Internet
Author: User

Processing of sticky packets in network programming (socket) and network programming socket

Server

  

Import socket, OS

Service = socket. socket ()
Service. bind ('localhost', 1024) # bind the port to be listened on
Service. listen () ### listening port
Con, adder = service. accept () # Wait for the other party's connection, generate an instance of the other party's connection in the region, and assign a value to con
While True:
Data = con. recv (1024). decode ('utf-8') # receive the value passed by the other party (the maximum value received is 2222 bytes) and assign a value
X = OS. popen (data). read ()
Con. send (str (len (x). encode ('utf-8 '))
# Time. sleep (0.5) ### avoid sticking packets, but there will be latency (not recommended)
Check_ack = con. recv (1024) # one interaction is performed between two send messages to prevent sticking to the package (recommended). At the same time, the client also responds.
Con. send (x. encode ('utf-8') # send data to the recipient
Service. close ()


Customer Service


 
Import socket
Client = socket. socket ()
Client. connect ("localhost", 1024 ))
While True:
Msg = input (':'). encode ('utf-8 ')
Client. send (msg)
Datasize = client. recv (1024). decode ('utf-8 ')
Client. send ("I have received it, you can pass the following content". encode () ### the client performs an automatic confirmation.
X = 0

While x! = Int (datasize ):
A = client. recv (1012). decode ('utf-8 ')
X + = len ()
Print ()
Else:
Print (datasize, x)
 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.