一、聊天室
基于 UDP 的聊天室
- Server 端:
import socket
sk = socket.socket(type=socket.SOCK_DGRAM) #DGRAM datagram 数据报文
sk.bind(('127.0.0.1',8080))
while True:
msg,addr = sk.recvfrom(1024)
print(addr)
print(msg.decode('utf-8'))
info = input('>>>').encode('utf-8')
sk.sendto(info,addr)
sk.close()
- Client1:
import socket
sk = socket.socket(type=socket.SOCK_DGRAM)
ip_port = ('127.0.0.1',8080)
while True:
info = input('cjz:')
info = ('