I am trying to send UDP broadcast messages, but I have issue with the address to use. If I run:
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
s.sendto("hello broadcast".encode("ascii"), ("<broadcast>", 1500))
s.sendto("hello 11".encode("ascii"), ("192.168.1.11", 1500))
s.sendto("hello 255".encode("ascii"), ("192.168.1.255", 1500))
s.sendto("hello 255.255".encode("ascii"), ("192.168.255.255", 1500))
s.sendto("hello 255.255.255".encode("ascii"), ("192.255.255.255", 1500))
s.sendto("hello 255.255.255.255".encode("ascii"), ("255.255.255.255", 1500))
and log the traffic with Wireshark, I only see the packets for:
192.168.1.11
192.168.1.255
192.168.255.255
192.255.255.255
but not for:
<broadcast>
255.255.255.255
This happens when I run it from WSL1 (Windows Subsystem for Linux), using Python 3.6.9, and also when I run it from Windows 10 Powershell, using Python 3.7.2.
If I use WSL2, using python 3.8.2, I get the packets for:
192.168.1.11
192.168.255.255
192.255.255.255
but not for:
<broadcast>
192.168.1.255
255.255.255.255
This is really strange. The 'real' broadcast addresses are ignored.
It seems as if setting socket.SO_BROADCAST
is ignored in Windows 10.
If I comment out s.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
, I get errors when trying to send something on the broadcast addresses.
If I however use a Linux box (Python versions 2.7.17, 2.7.18, 3.6.9 and 3.8.5), I get the packets for:
<broadcast>
192.168.1.255
255.255.255.255
but not for:
192.168.1.11
192.168.255.255
192.255.255.255
This is what I would expect for broadcast. Why does it not work that way on Windows 10 and on WSL and how can I fix it?
I have a Windows executable that sends UDP broadcast messages and I can see that it sends to "255.255.255.255". So the limitation is not the machine.