UIPEthernet as soon as it sends it cant receive anymore

I'm using the UIPEthernet library to send and receive data via UDP on the same port, however, this sketch it's only able to receive until a package is sent, after which, it can't receive anymore!

#include <UIPEthernet.h>
#include <UIPUdp.h>  // If using UDP

#define UDP_TX_PACKET_MAX_SIZE 512

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 31, 100);
IPAddress subnet(255, 255, 255, 0);
unsigned int UDP_PORT = 5005;

EthernetUDP Udp;

void setup() {
    Serial.begin(9600);
    Ethernet.begin(mac, ip, subnet);
    Udp.begin(UDP_PORT);
    Serial.println("UDP Listener Ready");
}

void loop() {
    int packetSize = Udp.parsePacket();
    if (packetSize > 0) {
        char packet[UDP_TX_PACKET_MAX_SIZE];
        int len = Udp.read(packet, min(packetSize, UDP_TX_PACKET_MAX_SIZE - 1));
        packet[len] = '\0';
        Serial.print("Received: ");
        Serial.println(packet);
    }

    static unsigned long lastSend = 0;
    if (millis() - lastSend > 20000) {
        lastSend = millis();
        Udp.beginPacket(IPAddress(192,168,31,255), 5005);
        Udp.write("Hello from Nano!");
        Udp.endPacket();
        Serial.println("Broadcast Sent");
    }
}

This is the output, where after Broadcast Sent no more data is received:

UDP Listener Ready
Received: {"m": 5, "i": 3921986557}
Received: {"m": 5, "i": 3921986557}
Broadcast Sent
Broadcast Sent
Broadcast Sent
Broadcast Sent
Broadcast Sent
Broadcast Sent
Broadcast Sent
Broadcast Sent
Broadcast Sent

I already tried this after Udp.write and it made no difference!

    Udp.flush(); // Clear any remaining data
    Udp.begin(UDP_PORT); // Refresh socket

Any suggestion how to use this library to receive AND send data on a single UDP port?

Thanks

I'm not sure you can use one UDP instance for both send and receive. Try create a new one. About the port number, that shouldn't make any difference since that's the listening ports on both sides (server), meanwhile the client connect through another port.

Example:
ClientsIP:43702 -> ServerIP:5005

This now works, the only real difference is the broadcast IP that is now 255.255.255.255 instead of 192.168.31.255!

#include <UIPEthernet.h>
#include <UIPUdp.h>  // If using UDP

#define UDP_TX_PACKET_MAX_SIZE 512

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 31, 100);
IPAddress subnet(255, 255, 255, 0);
IPAddress target(192, 168, 31, 22);
unsigned int UDP_PORT = 5005;

EthernetUDP Udp;

void setup() {
    Serial.begin(9600);
    Ethernet.begin(mac, ip, subnet);
    Udp.begin(UDP_PORT);
    Serial.println("UDP Listener Ready");
}

void loop() {
    int packetSize = Udp.parsePacket();
    if (packetSize > 0) {
        char packet[UDP_TX_PACKET_MAX_SIZE];
        int len = Udp.read(packet, min(packetSize, UDP_TX_PACKET_MAX_SIZE - 1));
        packet[len] = '\0';
        Serial.print("Received: ");
        Serial.println(packet);
    }

    static unsigned long lastSend = 0;
    if (millis() - lastSend > 20000) {
        lastSend = millis();
        if (random(2) % 2 == 0) {
            Udp.beginPacket(IPAddress(255,255,255,255), UDP_PORT);
            Udp.write("Broadcasted Hello!");
            Udp.endPacket();
            Serial.println("Broadcast Sent");
        } else {
            Udp.beginPacket(target, UDP_PORT);
            Udp.write("Unicasted Hello!");
            Udp.endPacket();
            Serial.println("Unicast Sent");
        }
        // Udp.flush(); // Clear any remaining data
        // Udp.begin(UDP_PORT); // Refresh socket
    }
}