feat: UDP broadcast test
This commit is contained in:
parent
9b3ca71b70
commit
f253a0d9df
@ -27,27 +27,30 @@ if __name__ == '__main__':
|
||||
host, port = args.host, args.port
|
||||
|
||||
if socket_type == 'server':
|
||||
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||
sock.bind((host, port))
|
||||
sock.listen(8)
|
||||
|
||||
print("Server listening...")
|
||||
|
||||
while True:
|
||||
conn, addr = sock.accept()
|
||||
data, addr = sock.recvfrom(1024)
|
||||
print(f"Accept connection from {addr}")
|
||||
|
||||
thread = threading.Thread(target=handle_client, args=(conn, ))
|
||||
thread.start()
|
||||
print('RUNNING THREAD')
|
||||
sock.sendto(host.encode(), addr)
|
||||
|
||||
# thread = threading.Thread(target=handle_client, args=(conn, ))
|
||||
# thread.start()
|
||||
|
||||
else:
|
||||
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
sock.connect((host, port))
|
||||
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||
sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
|
||||
sock.settimeout(2)
|
||||
|
||||
sock.sendto('discover'.encode(), ('255.255.255.255', port))
|
||||
|
||||
while True:
|
||||
data = sock.recv(1024)
|
||||
print(data.decode())
|
||||
data, addr = sock.recvfrom(1024)
|
||||
print(data.decode(), addr)
|
||||
|
||||
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user