aboutsummaryrefslogtreecommitdiff
path: root/host
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2018-04-25 09:03:31 +0200
committerTrygve Laugstøl <trygvis@inamo.no>2018-04-25 09:03:31 +0200
commit42ec7df723ece5c2c4400d8db24e3a2ac6b826d0 (patch)
treeb60357f2fd4cae75fa12816fe22284d454ca39b9 /host
parentfc8c037b80dea764393b79cba85a83004bd0aef6 (diff)
downloadiot-workshop-ndc-2018-42ec7df723ece5c2c4400d8db24e3a2ac6b826d0.tar.gz
iot-workshop-ndc-2018-42ec7df723ece5c2c4400d8db24e3a2ac6b826d0.tar.bz2
iot-workshop-ndc-2018-42ec7df723ece5c2c4400d8db24e3a2ac6b826d0.tar.xz
iot-workshop-ndc-2018-42ec7df723ece5c2c4400d8db24e3a2ac6b826d0.zip
wip
Diffstat (limited to 'host')
-rwxr-xr-xhost/udp-client.py31
-rwxr-xr-xhost/udp-server.py18
2 files changed, 49 insertions, 0 deletions
diff --git a/host/udp-client.py b/host/udp-client.py
new file mode 100755
index 0000000..5e2b132
--- /dev/null
+++ b/host/udp-client.py
@@ -0,0 +1,31 @@
+#!/usr/bin/env python3
+
+import socket, asyncoro
+
+ip = "127.0.0.1"
+ip = "192.168.10.151"
+port = 5005
+msg = "Hello, World!"
+
+print("UDP target IP: {}".format(ip))
+print("UDP target port: {}".format(port))
+print("message: {}".format(msg))
+
+sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
+
+start = None
+
+def client(coro=None):
+ buffer_size = 64 * 1024
+ print("Waiting for packet")
+ msg, (ip, port) = yield sock.recvfrom(buffer_size)
+ end = time.process_time()
+ print("Received '{}' from {}:{}".format(msg, ip, port))
+ print("RTT: {:.3f} ms".format((end - start) * 1000))
+
+asyncoro.Coro(client)
+import time
+time.sleep(0.1)
+print("Sending")
+start = time.process_time()
+sock.sendto(msg.encode("utf-8"), (ip, port))
diff --git a/host/udp-server.py b/host/udp-server.py
new file mode 100755
index 0000000..79bbe73
--- /dev/null
+++ b/host/udp-server.py
@@ -0,0 +1,18 @@
+#!/usr/bin/env python3
+
+import socket
+
+UDP_IP = "0.0.0.0"
+UDP_PORT = 5005
+
+sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
+sock.bind((UDP_IP, UDP_PORT))
+
+buffser_size = 64 * 1024
+
+while True:
+ data, (remote_ip, remote_port) = sock.recvfrom(buffser_size)
+ s = data.decode("utf-8", "ignore")
+ print("=> {}:{}: size={}, packet={}".format(remote_ip, remote_port, len(data), s))
+ sock.sendto(data, (remote_ip, remote_port))
+