Compare commits
2 Commits
9e80f589a7
...
4d9e72b70c
Author | SHA1 | Date |
---|---|---|
|
4d9e72b70c | |
|
aa542bf1ab |
|
@ -0,0 +1,13 @@
|
|||
import socket
|
||||
|
||||
def start_server(ip, port):
|
||||
server_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||
server_socket.bind((ip, port))
|
||||
print(f"UDP server listening on {ip}:{port}")
|
||||
|
||||
while True:
|
||||
data, addr = server_socket.recvfrom(1024) # buffer size is 1024 bytes
|
||||
print(f"received message: {data} from {addr}")
|
||||
|
||||
if __name__ == "__main__":
|
||||
start_server("localhost", 8888)
|
|
@ -0,0 +1,29 @@
|
|||
import socket
|
||||
import time
|
||||
|
||||
# UDP Configuration
|
||||
ip = "localhost" # Change this to the IP address of your computer or the network address to multicast
|
||||
port = 8888 # Ensure this matches the port number in your Unity script
|
||||
|
||||
# Create the UDP socket
|
||||
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||
|
||||
print("Starting UDP sending")
|
||||
while True:
|
||||
# Create some mock data
|
||||
mock_data = {
|
||||
"temperature": 72.4,
|
||||
"humidity": 45.3,
|
||||
"battery": 95.2,
|
||||
}
|
||||
|
||||
# Convert the mock data to a string and then to bytes
|
||||
message = str(mock_data)
|
||||
message_bytes = message.encode('utf-8')
|
||||
|
||||
# Send the mock data
|
||||
sock.sendto(message_bytes, (ip, port))
|
||||
print(message_bytes)
|
||||
|
||||
# Wait for a bit before sending the next packet
|
||||
time.sleep(1)
|
Loading…
Reference in New Issue