-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
28 lines (22 loc) · 920 Bytes
/
server.py
File metadata and controls
28 lines (22 loc) · 920 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#!/usr/bin/env python3
import socket
def start_udp_server(host="0.0.0.0", port=5005):
"""
Starts a UDP server that listens on the specified host and port.
"""
# Create a UDP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind((host, port))
print(f"UDP server listening on {host}:{port}")
while True:
# Wait for a message (up to 1024 bytes)
data, addr = sock.recvfrom(1024)
message = data.decode('utf-8')
print(f"Received message from {addr}: {message}")
# Process the message (you can integrate your robot control logic here)
# For example: if message == "MOVE_FORWARD": call_robot_move_forward()
# Optionally, send an acknowledgement or response back to the client
response = f"Ack: {message}"
sock.sendto(response.encode('utf-8'), addr)
if __name__ == "__main__":
start_udp_server()