Unable to Send UDP Packets on Belkin Router

I have started testing my sketch on a number of routers, and have found that I am unable to send UDP packets on a Belkin F5D8233. I made a simple sketch which replicates my issue. The code for the simple sketch is below:

EthernetUDP Udp;
IPAddress ip(192, 168, 2, 145);
IPAddress dnsIP(192, 168, 2, 1);
char packetBuffer[17];

void setup() {
  Serial.begin(9600);
  while(!Serial);
  byte mac[] = { };
  //EthernetDHCP.begin(mac, 1);
  Ethernet.begin(mac, ip, dnsIP, dnsIP);
  //while(!pollDhcp());
  Serial.println("Networking done.");
  Udp.begin(5000);
}

void loop() {
  int packetSize = Udp.parsePacket();
  if(packetSize != 0) {
    Serial.print("Packet size: ");
    Serial.println(packetSize, DEC);
    if(packetSize > 16) {
      Serial.println("Packet Size Too Big");
    }
    Udp.read(packetBuffer, packetSize);
    Serial.println(packetBuffer);
    
    Serial.print("Begin: ");
    Serial.println(Udp.beginPacket(Udp.remoteIP(), Udp.remotePort()), DEC);
    Serial.print("Write: ");
    Serial.println(Udp.write("Data"), DEC);
    Serial.print("End: ");
    Serial.println(Udp.endPacket(), DEC);
  }
}

I am able to receive the packet sent to the Arduino, but it never sends anything back. I have verified this with wireshark. Udp.beginPacket() returns 1, and Udp.write("Data") returns 4 as expected. However, Udp.endPacket() returns 0. The code has worked on a number of other routers, so I am not sure what could cause this. Could anyone provide some insight into what might be causing this issue, and what I could do to try to troubleshoot it?

Thanks in advance for help!

Is the sending device on the same localnet as the Arduino? Does it have a 192.168.2.x ip?

Yes, I can send a UDP packet to the Arduino, and the Arduino receives it successfully. I have also verified that the IP address and port that the Arduino is trying to send to is correct.

I also just verified that I am able to send UDP Packets from a non-Arduino client to the listenering UDP Socket that the Arduino is trying to send to.

Here is the manual for your router if you do not have a dead tree copy:
http://cache-www.belkin.com/support/dl/f5d8233-4_v1_manual.pdf

I'm not going to read the entire manual, but starting on page 74, it explains how to set static ips, and mentions this information must be entered into the router, but I did not read far enough into it to find out where. I suggest you do, or maybe you should try using dhcp.

Are you using dhcp on the other devices that do work? Or are they also assigned a static ip?

I'm not going to read the entire manual, but starting on page 74, it explains how to set static ips, and mentions this information must be entered into the router, but I did not read far enough into it to find out where

Interesting, I think they are saying that if you want a static ip on the "DMZ" of this router, then additional configuration is required. There is nothing I can find that references setting up a static ip to work on the LAN. There isn't anywhere to enter that information into the Web Admin Console either.

Nonetheless, I have tried this using DHCP and Static, and both have the same issue of not being able to send UDP packets. Also, in both cases I am able to establish a TCP connection with Arduino, so it should be on the network.

I'm not sure why a Belkin router would interfere with a localnet device-to-device communication. You should be able to do this without a router by connecting the computer to the Arduino directly or through a simple switch.

I use hotspots and the brand I have (Mikrotik) use arp poisoning to perform a 1:1 NAT which interferes with localnet communication, but that would also interfere with tcp as well as udp.

Have you tried the UdpNtpClient example sketch? Does that also fail?

I just ran the UdpNtpClient sketch, and it works as expected. I will post the code my Arduino is connecting to (written in Java). The socket is successfully binded to the port, I can netcat to it and it receives the packet successfully (from another machine to verify no firewall issues), as well as use another java client to send it a packet, and it is showing as listening when I run netstat.

        DatagramSocket serverSocket = new DatagramSocket(9870);
        byte[] receiveData = new byte[4];
        while(true)
           {
               /* Uncomment to be compatible with code I posted in the first post
               byte[] bytes = { (byte)192, (byte)168, (byte)2, (byte)145 };
               InetAddress IPAddress = InetAddress.getByAddress(bytes);
              int port = 5000;
              String toSend = "time";
              sendData = toSend.getBytes();
              DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, port);
              serverSocket.send(sendPacket);*/
              DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
              serverSocket.receive(receivePacket);
              String value = new String( receivePacket.getData());
              System.out.println("RECEIVED: " + value);
           }

FYI, when the Arduino tries to send the udp packet, it times out.

I used Netcat to test my udp sketch, and it did fine. I don't know why the Belkin would not work unless it has its ethernet ports on a bridge with firewall filters running on the bridge.

Maybe you should try "cheating". Have you tried listening on udp port 123 instead of 5000 on the Arduino?

edit: Make the Belkin think you are a NTP server.