Skip to content

UDP server limited to receiving <=1472 byte packets #222

@anecdata

Description

@anecdata

Adafruit CircuitPython 10.0.3 on 2025-10-17; Adafruit PyPortal with samd51j20 with ESP32SPI latest v9.0.4

Helping with a ESP32SPI UDP server issue on Discord, ran into a limit on the size of packets that can be received, at a max of 1472. It should allow much higher, something approaching 65535. This may be due to underlying code sharing some functions between TCP and UDP.

CircuitPython server code...
import time
import os
import board
import digitalio
import adafruit_connection_manager
import adafruit_esp32spi.adafruit_esp32spi_socketpool as socketpool
from adafruit_esp32spi import adafruit_esp32spi

PORT = 5000

spi = board.SPI()
# edit pins to match board
esp32_cs = digitalio.DigitalInOut(board.ESP_CS)
esp32_reset = digitalio.DigitalInOut(board.ESP_RESET)
esp32_ready = digitalio.DigitalInOut(board.ESP_BUSY)
radio = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset, debug=0)

pool = adafruit_connection_manager.get_radio_socketpool(radio)

print(f'Connecting to wifi... ', end='')
radio.connect(os.getenv("WIFI_SSID"), os.getenv("WIFI_PASSWORD"))
print(f'{radio.ipv4_address}')

print("Create UDP Server Socket")
s = pool.socket(type=pool.SOCK_DGRAM)
radio.start_server(PORT, s._socknum, conn_mode=1)  # UDP_MODE = const(1)
while True:
    numbytes_avail = radio.socket_available(s._socknum)
    if numbytes_avail:
        print(f'{time.monotonic():.03f} {numbytes_avail}', end=" ")
        bytes_read = radio.socket_read(s._socknum, numbytes_avail)
        print(f'{bytes_read[:10]}...{bytes_read[(numbytes_avail - 10):]}')
CPython client code...
#!/usr/bin/env python3
import time
import socket
import random

# edit host and port to match server
HOST = "192.168.6.39"
PORT = 5000
PKTSIZE = 1472
INTERVAL = 1

choices = iter(b'0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ')
while True:
    outbuf = bytearray()
    element = next(choices, 255)
    for _ in range(PKTSIZE):
        outbuf.append(element)

    with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s:
        size = s.sendto(outbuf, (HOST, PORT))
        print(f'{time.monotonic():.03f} sent {size} bytes of {chr(element)} to {HOST}:{PORT}')
        time.sleep(INTERVAL)

The behavior if the sent size is >1472 is that the server seems to loop through socket_available and socket_read without reading any more bytes (control-C exits).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions