Ethernet UDPSendReceiveString bugfix?

I have been playing around with the W5100 shield and UDP and stumbled across a bug in the UDPSendReceiveString example located:


Of you send a UDP packet of say XXXXXXXXXX then immediately afterwards send a packet of YYYYY the packetBuffer char array retains the last packet contents, only overwriting the first 5 elements. So printing out packetBuffer after the second receive yields YYYYYXXXXX.

To fix this you either need to print out packetBuffer to the length of packetSize or zero out the array before or after each use.

The easiest way is to add the line:
memset(packetBuffer, 0, sizeof packetBuffer);

memset(packetBuffer, 0, UDP_TX_PACKET_MAX_SIZE);

Loop listing:

void loop() {
  // if there's data available, read a packet
  int packetSize = Udp.parsePacket();
    Serial.print("Received packet of size ");
    Serial.print("From ");
    IPAddress remote = Udp.remoteIP();
    for (int i =0; i < 4; i++)
      Serial.print(remote[i], DEC);
      if (i < 3)
    Serial.print(", port ");

    // Zero out the packetBuffer
    memset(packetBuffer, 0, UDP_TX_PACKET_MAX_SIZE); // ********** Added ************ //
    // read the packet into packetBufffer, UDP_TX_PACKET_MAX_SIZE);

    // send a reply, to the IP address and port that sent us the packet we received
    Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());

It may help someone.


memset it is a EthernetUdp library command ?

memset it is a EthernetUdp library command ?

No. It's a core C function.

karma +1 :wink:

I hope it doesn’t seem like I’ve “High Jacked” this thread, but I can’t get the UDPSendReceiveString example to work. (or any UDP examples I have found for that matter). I don’t know if it is my Arduino IDE (1.0.5) sketch config, or if it is MY Processing (2.01) setup.

Adruino Sketch Setup:

I edit the IPAdress with a valid static IP from my router range ( and I leave the local port at 8888.
Send/compile to UNO. No errors other than the line “#include <EthernetUdp.h>” is all in BLACK. The other include library statements, SPI and Ethernet are in ORANGE. Does this mean anything. Do I need to include a path or add the library EthernetUdp?

I’ve never done anything with Processing so this is new territory for me:

I edit this line to include my UNO address. String ip = “”; // the remote IP address

It seems to run OK. Hitting the RUN command a little Grey Window pops up.

No amount of text entry into the Arduino Terminal echos back to the Processing app, and the “Hello World” doesn’t get sent to the UNO.

I can Ping my Uno at My lap top is on via DHCP and Wifi. I have turned off my routers Firewall.

Anything obviously wrong here? Any suggestions as to how I might determine which side isn’t working. Are there command line UDP test I can do to see if the UNO will accept a UDP receive?

After I posted, I played a bit more and I discovered my problem is with the Processing part of the example. I searched for a command line UDP send and like all good things in computing, Linux came through for me. I found a cool command line linux app called socat.

After doing a sudo get app for socat I did what this guy from the WEB suggested:

echo “HELLO” | socat – UDP-DATAGRAM:,broadcast

Checkout some examples:

From the linux command line it sent the message to my Arduino. Now to figure out what I am doing wrong in Processing :slight_smile:

thanks for this post !
I hit on this problem and the memset line solved it !
Thanks again :slight_smile: