Add a client implementation

This commit is contained in:
Santiago Lo Coco 2024-10-30 21:32:07 +01:00
parent c9a71da411
commit 5584b459cd
1 changed files with 19 additions and 0 deletions

19
client.py Normal file
View File

@ -0,0 +1,19 @@
import socket
import struct
multicast_address = '224.0.0.251'
multicast_port = 5353
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind(('', multicast_port))
group = socket.inet_aton(multicast_address)
mreq = struct.pack('4sL', group, socket.INADDR_ANY)
sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)
print(f"Listening for messages on {multicast_address}:{multicast_port}")
while True:
data, addr = sock.recvfrom(1024)
print(f"Received message: {data.decode()} from {addr}")