Server.py
import socket port = 8080 host = "localhost" ADDR = (host, port) udpServer = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) udpServer.bind(ADDR) while True: data, addr = udpServer.recvfrom(1024) replay = str(data) + "....ok" udpServer.sendto(replay.encode(), addr) print(replay) udpServer.close()Client.py
HOST = "localhost" PORT = 8080 BUFSIZE = 1024 ADDR = (HOST, PORT) udpCliSock = socket(AF_INET, SOCK_DGRAM) while True: data = input(">") if not data: break udpCliSock.sendto(data.encode(), ADDR) data, ADDR = udpCliSock.recvfrom(BUFSIZE) if not data: break print("server replay...:",data) udpCliSock.close()
udp 和 tcp 的最大区别是server 无需监听客户端的链接
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/40619.html
阅读 3265·2021-11-24 09:38
阅读 2147·2021-11-23 09:51
阅读 1736·2021-10-13 09:39
阅读 2608·2021-09-23 11:53
阅读 1392·2021-09-02 15:40
阅读 3646·2019-08-30 15:54
阅读 1121·2019-08-30 13:04
阅读 2550·2019-08-30 11:01