python—day27

UDP protocol:

There is no sticky packet phenomenon. No matter whether your server is turned on or not, the customer service terminal will immediately close the recycling resources after sending the message, so it is prone to data loss;

 

 1 import socket
 2 ip_port=('127.0.0.1',9000)
 3 BUFSIZE=1024
 4 server=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
 5 
 6 while True:
 7     msg=input('>>: ').strip()
 8     if not msg:continue
 9 
10     server.sendto(msg.encode('utf-8'),ip_port)
11 
12     msg,addr=server.recvfrom(BUFSIZE)
13     print(msg.decode('utf-8'),addr)

 

 

 1 import socket
 2 ip_port=('127.0.0.1',9000)
 3 BUFSIZE=1024
 4 client=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
 5 
 6 client.bind(ip_port)
 7 
 8 while True:
 9     msg,addr=udp_server_client.recvfrom(BUFSIZE)
10     print(msg,addr)
11 
12     client.sendto(msg.upper(),addr)

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326857954&siteId=291194637