From f1cfc448de146f40a73b2a6c1ed5c3fe29dcd7fd Mon Sep 17 00:00:00 2001 From: IhorNehrutsa Date: Thu, 21 Dec 2023 09:43:39 +0200 Subject: [PATCH] examples/network: Add UDP multicast client and server examples. Signed-off-by: IhorNehrutsa --- examples/network/udp_multicast_client.py | 63 ++++++++++++++++++++++++ examples/network/udp_multicast_server.py | 59 ++++++++++++++++++++++ 2 files changed, 122 insertions(+) create mode 100755 examples/network/udp_multicast_client.py create mode 100755 examples/network/udp_multicast_server.py diff --git a/examples/network/udp_multicast_client.py b/examples/network/udp_multicast_client.py new file mode 100755 index 0000000000000..0d9db65096157 --- /dev/null +++ b/examples/network/udp_multicast_client.py @@ -0,0 +1,63 @@ +# udp_multicast_client.py:: + +import network, socket, errno, struct, time + +print("connect to AP") + +SSID = 'SSID' +PASSWORD = 'PASSWORD' + +wlan = network.WLAN(network.STA_IF) +wlan.ifconfig(('172.16.55.55', '255.255.255.0', '172.16.55.1', '172.16.55.1')) # class B network +wlan.active(True) +wlan.connect(SSID, PASSWORD) +while not wlan.isconnected(): + pass + +print("udp_multicast_client.py") +print("client send GET and receive ACK from the server") + +def inet_aton(str_addr): + return bytes(map(int, str_addr.split("."))) + +#TIMEOUT = None # block +#TIMEOUT = 5 # s +TIMEOUT = 0 # non blocking + +MULTICAST_IP = '224.0.0.111' +PORT = 5555 +sockaddr = (MULTICAST_IP, PORT) + +wlan = network.WLAN(network.STA_IF) +client_ip = wlan.ifconfig()[0] + +skt = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # UDP +skt.settimeout(None) +skt.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) +skt.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, struct.pack(">4s4s", inet_aton(MULTICAST_IP), inet_aton(client_ip))) # join to the multicast address +skt.bind(sockaddr) # not skt.connect(sockaddr) +skt.settimeout(TIMEOUT) + +t = 0 +while True: + try: + if time.time() - t >= 3: + t = time.time() + + mac = wlan.config('mac') + str_to_send = f"client_ip:{client_ip}\t mac:{'-'.join(map('{:02x}'.format, mac))}" + skt.sendto(str_to_send, sockaddr) + print(f'GET to {sockaddr}\t sent "{str_to_send}"') + + received, addr = skt.recvfrom(1024) + if received: + print(f'ACK from {addr}\t received "{received.decode()}"') + except OSError as e: + if e.args[0] in (errno.EAGAIN, errno.ETIMEDOUT): + pass + else: + try: + skt.close() + except: + pass + raise(e) diff --git a/examples/network/udp_multicast_server.py b/examples/network/udp_multicast_server.py new file mode 100755 index 0000000000000..626645268c55a --- /dev/null +++ b/examples/network/udp_multicast_server.py @@ -0,0 +1,59 @@ +# udp_multicast_server.py:: + +import network, socket, errno, struct, time + +print("connect to AP") + +SSID = 'SSID' +PASSWORD = 'PASSWORD' + +wlan = network.WLAN(network.STA_IF) +wlan.ifconfig(('192.168.44.44', '255.255.255.0', '192.168.44.1', '192.168.44.1')) # class C network +wlan.active(True) +wlan.connect(SSID, PASSWORD) +while not wlan.isconnected(): + pass + +print("udp_multicast_server.py") +print("server receive GET and send ACK to the client") + +def inet_aton(str_addr): + return bytes(map(int, str_addr.split("."))) + +#TIMEOUT = None # block +#TIMEOUT = 5 # s +TIMEOUT = 0 # non blocking + +MULTICAST_IP = '224.0.0.111' +PORT = 5555 +sockaddr = (MULTICAST_IP, PORT) + +wlan = network.WLAN(network.STA_IF) +server_ip = wlan.ifconfig()[0] + +skt = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # UDP +skt.settimeout(None) +skt.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) +skt.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, struct.pack(">4s4s", inet_aton(MULTICAST_IP), inet_aton(server_ip))) # join to the multicast address +skt.bind(sockaddr) +skt.settimeout(TIMEOUT) + +while True: + try: + received, addr = skt.recvfrom(1024) + if received: + print(f'GET from {addr}\t received "{received.decode()}"') + + mac = wlan.config('mac') + str_to_send = f"server_ip:{server_ip}\t mac:{'-'.join(map('{:02x}'.format, mac))}" + skt.sendto(str_to_send, sockaddr) + print(f'ACK to {sockaddr}\t sent "{str_to_send}"') + except OSError as e: + if e.args[0] in (errno.EAGAIN, errno.ETIMEDOUT): + pass + else: + try: + skt.close() + except: + pass + raise(e)