代码区:
import socket
s =socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
s.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
s.setsockopt(socket.SOL_SOCKET,socket.SO_BROADCAST,1)
s.bind((‘‘,6969))
data, addr = s.recvfrom(2048)
print(" %s : %s" % (addr, data.decode()))
while True:
try:
data = input(">>:")
s.sendto(data.encode(), addr)
except ConnectionError as e:
break
s.close()
#7512
import socket
c =socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
c.setsockopt(socket.SOL_SOCKET,socket.SO_BROADCAST,1)
c.sendto(b"hello from client",(‘<broadcast>‘,6969))
while True:
# data =input(">>:")
# c.sendto(data.encode(),(‘<broadcast>‘,6969))
data, addr = c.recvfrom(1024)
print(addr,data.decode())
原文:https://www.cnblogs.com/gtq7512/p/11438141.html