recv与recvfrom的区别及基于udp实现ntp服务
作者:互联网
recv在自己这端的缓冲区为空时,阻塞
recvfrom在自己这段的缓冲区为空时,就收一个空
tcp服务端客户端,不能实现并发效果,基于链接
tcp可以实现并发,现在技术不够
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)ntp_server
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)ntp_client
标签:ip,udp,ntp,server,recvfrom,time,data,port 来源: https://www.cnblogs.com/jintian/p/10970862.html