Python Forum
Udp broadcast programming project - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Networking (https://python-forum.io/forum-12.html)
+--- Thread: Udp broadcast programming project (/thread-13611.html)



Udp broadcast programming project - oredri269 - Oct-23-2018

hello,

i got a project to create a UDP broadcast programming -

create a system that sends a broadcast of a message alert to all the systems that are on the network.
Each station that receives the message must transmit it to other stations on the networks it is connected to spread the message.

i created the client.py and the server.py , but i don't know how to make the station transmit the message alert.
its the first time i am creating a project in networking programming in python.

##client

import socket, time

SERVER_ADDRES = "127.0.0.1"
SERVER_PORT = 10000

#connect to server
client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # UDP
address = (SERVER_ADDRES,SERVER_PORT)
client.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
client.bind(("", 37020))
while True:
    data, address= client.recvfrom(1024)
    print(data)
    client.sendto(data, ('<broadcast>', 12345))
    break
##server

import socket
import time

SERVER_ADDRESS = "localhost"
SERVER_PORT = 10000

# create a socket
server = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
server.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
server.settimeout(0.2)
address = (SERVER_ADDRESS,SERVER_PORT)
print(f"server {SERVER_ADDRESS} at port {SERVER_PORT}")
server.bind(("",44444))
message = b"Red Alert"
while True:
    # broadcast
    server.sendto(message, ('<broadcast>', 37020))
    print("message sent!")
    time.sleep(1)
    break