-
-
Save cry/9e435d54cbe95fe9fddc2e0596409265 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 = sock.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
hi,
There is a small issue in client.py.