How to receive more than one UDP-packet on a single socket?

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
Posts: 42
Joined: Thu Nov 24, 2016 2:34 pm
Location: Hamburg

How to receive more than one UDP-packet on a single socket?

Post by ZKDMun » Mon Jun 18, 2018 2:57 pm

With an UDP-Socket a server can receive a UDP-packet during "doing some stuff", and later read the packet from socket.recv().

For example:

Code: Select all

>>> import network
>>> import socket

>>> ap_if = network.WLAN(network.AP_IF)

>>> s = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
>>> s.bind(("<IP_OF_SERVER>",<SOME_PORT>)

>>> doing_some_stuff() #calling a function here, which will need some time to execute!
#During doing_some_stuff() a connected client is sending a UDP-packet to the socket (s), with socket.sendto()

#after finishing doing_some_stuff() it is possible to call s.recv(bufsize), for receiving the UDP-packet which was sent during doing_some_stuff()!
>>> s.recv(1024)
b"someData" #received data
For now I try to find a solution for sending more than one UDP-packet to a single port during the server is doing some other stuff. Because at the moment if the client will send two or more UDP-packets to the server during his doing_some_stuff()-execution, the server will only be able to get the first received packet with s.recv(bufsize) - if I call the s.recv(bufsize) a second time, it will return b''.

Is it possible to receive more the one UDP-packet - how?

(one solution will be, that the server use more than one socket, with another port - and the client will send the next packet to the next socket and port - but is it possible to use the same socket?)

Post Reply