3
votes

I am getting this error trying to broadcast a UDP packet in Windows 7.

import socket
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.sendto("hello".encode("ascii"), ("<broadcast>", 5868))

If I change to 255.255.255.255, same deal. It works fine if I put a specific IP address (including 127.0.0.1). Same exact method (i.e., sending a packet to 255.255.255.255) works from .NET.

This is not affected by the Windows firewall (tried turning it off). I'm administrator and UAC is off and even with "run as administrator" it doesn't work. Somehow, I don't think it's an actual permissions problem.

1

1 Answers

5
votes

You seem to be missing an

s.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)

after the socket creation. It enables the socket to broadcast.

Does it help?