circuitpython/tests/multi_net/udp_data.py
Damien George 8fcdb5490c extmod/modlwip: Fix close and clean up of UDP and raw sockets.
The correct callback-deregister functions must be called dependent on the
socket type, otherwise resources may not be freed correctly.

Signed-off-by: Damien George <damien@micropython.org>
2021-08-13 23:46:11 +10:00

36 lines
1.0 KiB
Python

# Simple test of a UDP server and client transferring data
import socket
NUM_NEW_SOCKETS = 4
NUM_TRANSFERS = 4
PORT = 8000
# Server
def instance0():
multitest.globals(IP=multitest.get_network_ip())
multitest.next()
for i in range(NUM_NEW_SOCKETS):
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.bind(socket.getaddrinfo("0.0.0.0", PORT)[0][-1])
multitest.broadcast("server ready")
for j in range(NUM_TRANSFERS):
data, addr = s.recvfrom(1000)
print(data)
s.sendto(b"server to client %d %d" % (i, j), addr)
s.close()
# Client
def instance1():
multitest.next()
ai = socket.getaddrinfo(IP, PORT)[0][-1]
for i in range(NUM_NEW_SOCKETS):
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
multitest.wait("server ready")
for j in range(NUM_TRANSFERS):
s.sendto(b"client to server %d %d" % (i, j), ai)
data, addr = s.recvfrom(1000)
print(data)
s.close()