recv在自己这端的缓冲区为空时,阻塞
recvfrom在自己这段的缓冲区为空时,就收一个空
tcp服务端客户端,不能实现并发效果,基于链接
tcp可以实现并发,现在技术不够
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
from socket import * import time ip_port=('192.168.1.6',8002) buffer_size=1024 udp_server=socket(AF_INET,SOCK_DGRAM) udp_server.bind(ip_port) while True: data,addr=udp_server.recvfrom(buffer_size) if not data: fmt='%Y-%m-%d %X' else: fmt=data.decode('utf-8') print(data) back_time=time.strftime(fmt) udp_server.sendto(back_time.encode('utf-8'),addr)
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
from socket import * import time ip_port=('192.168.1.6',8002) buffer_size=1024 udp_client=socket(AF_INET,SOCK_DGRAM) while True: msg=input('>>:') udp_client.sendto(msg.encode('utf-8'),ip_port) data,addr=udp_client.recvfrom(buffer_size) print(data)