I am trying to learn to code sockets (in Python 3), I am simply trying to send a broadcast from a server and receive it from a client.
My problem is that whenever I try to send the packets to 255.255.255.255, it seems nothing is actually sent. I tried to find the packets with wireshark but except on the loopback interface, I can't find any.
I can successfully send a message between the two computers when manually inputting the IP, and I also see the packets in wireshark.
Here's the code for the client
import socket
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
sock.bind(("0.0.0.0", 5005))
while True:
# sock.sendto(bytes("hello", "utf-8"), ip_co)
data, addr = sock.recvfrom(1024)
print(data)
And here's the code for the server
import socket
from time import sleep
def main():
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP) # UDP
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
# sock.settimeout(2)
while True:
sock.sendto(bytes("test", "utf-8"), ("255.255.255.255", 5005))
sleep(1)
main()
Sorry if the code is ugly, I am very new to sockets and to python.