From 42ec7df723ece5c2c4400d8db24e3a2ac6b826d0 Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Wed, 25 Apr 2018 09:03:31 +0200 Subject: wip --- host/udp-client.py | 31 +++++++++++++++++++++++++++++++ host/udp-server.py | 18 ++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100755 host/udp-client.py create mode 100755 host/udp-server.py (limited to 'host') 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)) + -- cgit v1.2.3