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 的最大區(qū)別是server 無需監(jiān)聽客戶端的鏈接
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://specialneedsforspecialkids.com/yun/40619.html
閱讀 3267·2021-11-24 09:38
閱讀 2148·2021-11-23 09:51
閱讀 1737·2021-10-13 09:39
閱讀 2610·2021-09-23 11:53
閱讀 1393·2021-09-02 15:40
閱讀 3647·2019-08-30 15:54
閱讀 1121·2019-08-30 13:04
閱讀 2551·2019-08-30 11:01