Udp problem: endpacket return 0

Hi Everyone,
I've load the following sketch for testing UPD comunication to an Arduino Uno Rev.3 with Ethernet shield (HanRun HR911105A 14/31).

#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>   
EthernetUDP Udp;

static byte macAddr[] = { 0xDE, 0xAD, 0xEE, 0xEF, 0xFE, 0xED };

IPAddress ip(192, 168, 0, 10);

unsigned int localPort = 9696;      // local port to listen on

// buffers for receiving and sending data
char packetBuffer[UDP_TX_PACKET_MAX_SIZE];  //buffer to hold incoming packet,

void setup() {

  Serial.begin(9600);
  Ethernet.begin(macAddr,ip);
  Udp.begin(localPort);
   
}

void loop() {

  int packetSize = Udp.parsePacket();
  if (packetSize) {

    IPAddress remote = Udp.remoteIP();
    
    // read the packet into packetBufffer
    Udp.read(packetBuffer, packetSize);
    Serial.println("Contents:");
    Serial.println(packetBuffer);

    // send a reply to the IP address and port that sent us the packet we received
    Serial.print("BeginPacket:");
    Serial.println(Udp.beginPacket(Udp.remoteIP(), 10023));
    Serial.print("write:");
    Serial.println(Udp.write('X'));
    Serial.print("Sent:");
    Serial.println(Udp.endPacket());
  }
    delay(10);  
}

The code listen on port 9696 for any incoming packet, print it to serial monitor, and replay on remote port 10023 with 'X' and print the return code for the beginPacket, wirite and end packet.

The sketch work perfectly for recinving the packet, but when it replay the endPacket return always 0 and I don't recive nothing back.
the serial monitor display this

Contents:
zzzzz
BeginPacket:1
write:1
Sent:0
Contents:
zzzzz
BeginPacket:1
write:1
Sent:0
Contents:
zzzzz
BeginPacket:1
write:1
Sent:0

beginPacket and write are ok, the end packet fail. Why? What it could be the cause?
The firewall is disabled, pc and arduino are on the same net. The program (wrote in VB6) that should recive the response 'X'on port 10023 it's ok because I've tested with another program (always wrote in VB6 that write on remoteport 10023 and listen on 9696) and it could send and recive without problem also between two different machines on the same network.

Thanks for any suggestion.

That indicates the device next in line to the destination refused to take the packet or the ethernet shield was unable to establish communication with that device. Are you certain the network settings are correct, like netmask and gateway?

SurferTim:
That indicates the device next in line to the destination refused to take the packet or the ethernet shield was unable to establish communication with that device. Are you certain the network settings are correct, like netmask and gateway?

As you can see, Ethernet.begin is only with MAC and IP. My pupose is to implement (if the UDP comunication works) an easy to install device that have no need to pre-set with network parameter, but all the necessary information are given by a server that listen for the arrival of a new device and send the set up information.
So at the beginning I prefer to don't sepcify a subnet and getway and DNS, all will be sent by UDP broadcast messages. Now I've achived to sent in broadcast a packet and to be read by arduino, but I cannot send a response packet. Another thing that I'd like to achive is to send from the Arduino an UDP broadcast packet, but till I could not sent normal UDP packet I think that could not be possible.
Do you think that I need to set a defautl subnet mask anyway?

I've done another test: I've set a fixed valid Ip address to arduino, my gateway address, the dns 8.8.8.8 (google dns) and the same subnet mask of my network (255.255.255.0) and used the full Ethernet.begin method with all the parameters and the situation is the same; I can recive broadcast messages but endpacket fail to send. I've also try to ping from my computer the arduino Ip but it doesn't ping back.
I've tested the Ethernet.localIP after the Ethernet.begin method and it give me back the right adress.

Try displaying the remote variable on the serial monitor. What does that show?

SurferTim:
Try displaying the remote variable on the serial monitor. What does that show?

remoteIp and remotePort display the right IP and port of the pc that sends the packet.

Then you have a routing or firewall problem.

SurferTim:
Then you have a routing or firewall problem.

Firewall is disabled and no other firewall are on my net nor on the router. Routing problem? I don't know, because I've also try with a cross-cable connecting directly the Arduino with my pc and the result is the same. Remeber that I've also try the UDP connection between 2 pc on my net with program written in VB6 and the UDP broadcast and unicast comunication works perfectly.
I'm thinking about at some kind of problem maybe with the MAC address or some more low level protocol problem of the Ethernet shield or maybe an hardware problem of the etherent shield. I will try a TCP connection to test if the shield is ok.
Any more suggestion is apreciated.

Thanks a lot.

I'm currently running tests with UDP broadcasts with a Linux box as the "server" using a Due/ethernet shield and RPi as the "clients". It is working fine. Unicast packets are not a problem. edit: I was using a Mega 2560, but it wouldn't keep up at 30 packets/second.

edit: I would change this as a test. Maybe your PC is not listening on this port.

//change this
    Serial.println(Udp.beginPacket(Udp.remoteIP(), 10023));
// to this
    Serial.println(Udp.beginPacket(Udp.remoteIP(), Udp.remotePort()));

Some computer OS use connection tracking to allow inbound packets. Unfortunately, if you are sending to 192.168.0.255, the connection tracking will allow a response from that IP, but maybe not your ethernet shield IP. Just a thought.

Disabling the firewall is not a good idea. Get used to allowing the protocol/port through it.

SurferTim:
I'm currently running tests with UDP broadcasts with a Linux box as the "server" using a Due/ethernet shield and RPi as the "clients". It is working fine. Unicast packets are not a problem. edit: I was using a Mega 2560, but it wouldn't keep up at 30 packets/second.

edit: I would change this as a test. Maybe your PC is not listening on this port.

//change this

Serial.println(Udp.beginPacket(Udp.remoteIP(), 10023));
// to this
    Serial.println(Udp.beginPacket(Udp.remoteIP(), Udp.remotePort()));



Some computer OS use connection tracking to allow inbound packets. Unfortunately, if you are sending to 192.168.0.255, the connection tracking will allow a response from that IP, but maybe not your ethernet shield IP. Just a thought.

Disabling the firewall is not a good idea. Get used to allowing the protocol/port through it.

I've already done the change you told, but nothing to do. I've done a test with the sketch ChatServer for TCP connection and work fine if i use a cross cable even if i cannot ping the Arduino. If I connect the pc and the arduino throught a switch it doesn't works.

I'm using a Netgear 10/100 switch and it does ok. I haven't tried it with a gigabit switch yet.

SurferTim:
I'm using a Netgear 10/100 switch and it does ok. I haven't tried it with a gigabit switch yet.

My switch is D-LINK DES-1005D. Googling around I've found that Ehetrnet shild could have problem with this kind of switch. My ethernet card on PC is Realtek PCIe GBE.

Some more information:
I've done some other test using a cheap 5 port switch (Atlantis) to connect the Arduino to pc and all works great with UDP broadcast both recive and transmit packets, so the problem is really the switch. But I've done another test: I've connected with a cross cable the pc with Arduino and it doesn't works, I've also try with a normal cable and it doesn't work (as it should be, at least one normal behavior).
Maybe I've to change some parameters to the Etherent pc board, some kind of autonegotiation or duplex or speed? Or something in the Arduino side?

It may be the Wiznet ethernet controllers have a problem negotiating with gigabit network devices.