mirror of
https://github.com/TheGreyDiamond/Enlight.git
synced 2025-12-16 23:10:45 +01:00
Added test modules
This commit is contained in:
19
Code/SingleModuleTesting/broadcastClient.py
Normal file
19
Code/SingleModuleTesting/broadcastClient.py
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
import socket
|
||||||
|
|
||||||
|
client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP) # UDP
|
||||||
|
|
||||||
|
# Enable port reusage so we will be able to run multiple clients and servers on single (host, port).
|
||||||
|
# Do not use socket.SO_REUSEADDR except you using linux(kernel<3.9): goto https://stackoverflow.com/questions/14388706/how-do-so-reuseaddr-and-so-reuseport-differ for more information.
|
||||||
|
# For linux hosts all sockets that want to share the same address and port combination must belong to processes that share the same effective user ID!
|
||||||
|
# So, on linux(kernel>=3.9) you have to run multiple servers and clients under one user to share the same (host, port).
|
||||||
|
# Thanks to @stevenreddie
|
||||||
|
# client.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
|
||||||
|
|
||||||
|
# Enable broadcasting mode
|
||||||
|
client.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
|
||||||
|
|
||||||
|
client.bind(("", 37020))
|
||||||
|
while True:
|
||||||
|
# Thanks @seym45 for a fix
|
||||||
|
data, addr = client.recvfrom(1024)
|
||||||
|
print("received message: %s" % data)
|
||||||
23
Code/SingleModuleTesting/broadcastServer.py
Normal file
23
Code/SingleModuleTesting/broadcastServer.py
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
import socket
|
||||||
|
import time
|
||||||
|
|
||||||
|
server = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
|
||||||
|
|
||||||
|
# Enable port reusage so we will be able to run multiple clients and servers on single (host, port).
|
||||||
|
# Do not use socket.SO_REUSEADDR except you using linux(kernel<3.9): goto https://stackoverflow.com/questions/14388706/how-do-so-reuseaddr-and-so-reuseport-differ for more information.
|
||||||
|
# For linux hosts all sockets that want to share the same address and port combination must belong to processes that share the same effective user ID!
|
||||||
|
# So, on linux(kernel>=3.9) you have to run multiple servers and clients under one user to share the same (host, port).
|
||||||
|
# Thanks to @stevenreddie
|
||||||
|
# server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
|
||||||
|
|
||||||
|
# Enable broadcasting mode
|
||||||
|
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)
|
||||||
|
message = b"your very important message"
|
||||||
|
while True:
|
||||||
|
server.sendto(message, ("<broadcast>", 37020))
|
||||||
|
print("message sent!")
|
||||||
|
time.sleep(1)
|
||||||
Reference in New Issue
Block a user