-
-
Save mcagriaksoy/48d9ea73f9432d17c9cdb1c0e67556a7 to your computer and use it in GitHub Desktop.
Python udp broadcast client server example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import socket | |
client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # UDP | |
client.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1) | |
client.bind(("", 37020)) | |
while True: | |
data, addr = client.recvfrom(1024) | |
print("received message: %s"%data) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import socket | |
import time | |
server = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP) | |
server.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1) | |
# Set a timeout so the socket does not block | |
# indefinitely when trying to receive data. | |
server.settimeout(0.2) | |
server.bind(("", 44444)) | |
message = b"your very important message" | |
while True: | |
server.sendto(message, ('<broadcast>', 37020)) | |
print("message sent!") | |
time.sleep(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
data, addr = sock.recvfrom(1024) -----> data, addr = client.recvfrom(1024)