diff --git a/sys/net/application_layer/nanocoap/sock.c b/sys/net/application_layer/nanocoap/sock.c index 73aa3db89ef5..0b3db123b777 100644 --- a/sys/net/application_layer/nanocoap/sock.c +++ b/sys/net/application_layer/nanocoap/sock.c @@ -195,6 +195,12 @@ static uint32_t _deadline_left_us(uint32_t deadline) return deadline - now; } +static void _sock_flush(nanocoap_sock_t *sock) +{ + void *payload, *ctx = NULL; + while (_sock_recv_buf(sock, &payload, &ctx, 0) > 0 || ctx) {} +} + ssize_t nanocoap_sock_request_cb(nanocoap_sock_t *sock, coap_pkt_t *pkt, coap_request_cb_t cb, void *arg) { @@ -223,6 +229,9 @@ ssize_t nanocoap_sock_request_cb(nanocoap_sock_t *sock, coap_pkt_t *pkt, .iol_len = coap_get_total_len(pkt), }; + /* clear out stale responses from previous requests */ + _sock_flush(sock); + while (1) { switch (state) { case STATE_REQUEST_SEND: