Python中的简单实现UDP协议没有粘包问题

服务端:

 1 import socket
 2 
 3 server=socket.socket(socket.AF_INET,socket.SOCK_DGRAM) #数据报协议-》udp
 4 server.bind(('127.0.0.1',8080))
 5 
 6 data,client_addr=server.recvfrom(1) #b'hello'==>b'h'
 7 print('第一次:',client_addr,data)
 8 
 9 data,client_addr=server.recvfrom(1024) #b'world' =>b'world'
10 print('第二次:',client_addr,data)
11 #
12 # data,client_addr=server.recvfrom(1024)
13 # print('第三次:',client_addr,data)
14 
15 server.close()

客户端:

1 import socket
2 
3 client=socket.socket(socket.AF_INET,socket.SOCK_DGRAM) #数据报协议-》udp
4 
5 client.sendto('hello'.encode('utf-8'),('127.0.0.1',8080))
6 client.sendto('world'.encode('utf-8'),('127.0.0.1',8080))
7 # client.sendto(''.encode('utf-8'),('127.0.0.1',8080))
8 
9 client.close()

 

posted @ 2018-07-10 15:46  胡颖爱周雅  阅读(458)  评论(0编辑  收藏  举报