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
|
host, port = args.host, args.port
|
||||||
|
|
||||||
if socket_type == 'server':
|
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.bind((host, port))
|
||||||
sock.listen(8)
|
|
||||||
|
|
||||||
print("Server listening...")
|
print("Server listening...")
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
conn, addr = sock.accept()
|
data, addr = sock.recvfrom(1024)
|
||||||
print(f"Accept connection from {addr}")
|
print(f"Accept connection from {addr}")
|
||||||
|
|
||||||
thread = threading.Thread(target=handle_client, args=(conn, ))
|
sock.sendto(host.encode(), addr)
|
||||||
thread.start()
|
|
||||||
print('RUNNING THREAD')
|
# thread = threading.Thread(target=handle_client, args=(conn, ))
|
||||||
|
# thread.start()
|
||||||
|
|
||||||
else:
|
else:
|
||||||
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||||
sock.connect((host, port))
|
sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
|
||||||
|
sock.settimeout(2)
|
||||||
|
|
||||||
|
sock.sendto('discover'.encode(), ('255.255.255.255', port))
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
data = sock.recv(1024)
|
data, addr = sock.recvfrom(1024)
|
||||||
print(data.decode())
|
print(data.decode(), addr)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user