I have a class that needs to broadcast its existence over the local network
In the __init__()
, I make the following calls:
self._interfaces = socket.getaddrinfo(host=socket.gethostbyname(socket.gethostname()), port=None, family=socket.AF_INET)
self._allIps = [ip[-1][0] for ip in self._interfaces]
self._running = True
And in the thread that takes care of sending the broadcast on a loop, I run the following code:
while self._running:
for ip in self._allIps:
if self._logger:
self._logger.log("[Client] broadcasting ID", Verbosity.Trace)
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP) # UDP
sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
sock.bind((ip, 0))
sock.sendto(msg, ("255.255.255.255", 5777))
sock.close()
sleep(2)
Following the suggestions in this stack article
When I run this on a windows machine it works just fine and there is a valid ip in self._allIps but When I run this on my raspberry pi self._allIps will be a list containing "127.0.0.1" 3 times
Why does it not return the actual ip of the pi and why don't I get a proper broadcast message over the local network?