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 無需監聽客戶端的鏈接
文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。
轉載請注明本文地址:http://m.specialneedsforspecialkids.com/yun/40619.html
閱讀 3286·2021-11-24 09:38
閱讀 2159·2021-11-23 09:51
閱讀 1750·2021-10-13 09:39
閱讀 2625·2021-09-23 11:53
閱讀 1410·2021-09-02 15:40
閱讀 3660·2019-08-30 15:54
閱讀 1137·2019-08-30 13:04
閱讀 2567·2019-08-30 11:01