UDP recieve buffer issue?

I’m just starting to try some UDP stuff and have the following running on an Arduino Ehthernet :-

/*
  UDPSendReceive.pde:
 This sketch receives UDP message strings, prints them to the serial port
 and sends an "acknowledge" string back to the sender
 
 A Processing sketch is included at the end of file that can be used to send
 and received messages for testing with a computer.
 
 created 21 Aug 2010
 by Michael Margolis
 
 This code is in the public domain.
 */


#include <SPI.h>         // needed for Arduino versions later than 0018
#include <Ethernet.h>
#include <EthernetUdp.h>         // UDP library from: bjoern@cs.stanford.edu 12/30/2008


// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {0x90, 0xA2, 0xDA, 0x00, 0x76, 0x81};
IPAddress ip(192, 168, 216, 42);

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

// buffers for receiving and sending data
char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet,
char  ReplyBuffer[] = "acknowledged";       // a string to send back

// An EthernetUDP instance to let us send and receive packets over UDP
EthernetUDP Udp;

void setup() {
  // start the Ethernet and UDP:
  Ethernet.begin(mac,ip);
  Udp.begin(localPort);

  Serial.begin(57600);
  Serial.print("Hello");
  Serial.print(ip);
}

void loop() {
  // if there's data available, read a packet
  int packetSize = Udp.parsePacket();
  if(packetSize)
  {
    IPAddress remote = Udp.remoteIP();
    Udp.read(packetBuffer,UDP_TX_PACKET_MAX_SIZE);
    Serial.println(packetBuffer);


  }

}

And to test it a processing shield :-

/**
 * (./) udp.pde - how to use UDP library as unicast connection
 * (cc) 2006, Cousot stephane for The Atelier Hypermedia
 * (->) http://hypermedia.loeil.org/processing/
 *
 * Create a communication between Processing<->Pure Data @ http://puredata.info/
 * This program also requires to run a small program on Pd to exchange data  
 * (hum!!! for a complete experimentation), you can find the related Pd patch
 * at http://hypermedia.loeil.org/processing/udp.pd
 * 
 * -- note that all Pd input/output messages are completed with the characters 
 * ";\n". Don't refer to this notation for a normal use. --
 */

// import UDP library
import hypermedia.net.*;


UDP udp;  // define the UDP object

/**
 * init
 */
void setup() {

  // create a new datagram connection on port 6000
  // and wait for incomming message
  udp = new UDP( this, 6000 );
  //udp.log( true ); 		// <-- printout the connection activity
  udp.listen( true );
}

//process events
void draw() {;}

/** 
 * on key pressed event:
 * send the current key value over the network
 */
void keyPressed() {
    
    //String message  = str( key );	// the message to send
    String ip       = "192.168.216.42";	// the remote IP address
    int port        = 8888;		// the destination port
    
    for (int i =0; i < 100000; i++)

    {
      String st = Integer.toString(i); 
      String message = "Hello "+st;
      udp.send(message,ip,port);
      //udp.send(st,ip,port);
      delay(40);
    }
    
    
    // formats the message for Pd
    //message = message+";\n";
    // send the message
    //udp.send( message, ip, port );
    
}

/**
 * To perform any action on datagram reception, you need to implement this 
 * handler in your code. This method will be automatically called by the UDP 
 * object each time he receive a nonnull message.
 * By default, this method have just one argument (the received message as 
 * byte[] array), but in addition, two arguments (representing in order the 
 * sender IP address and his port) can be set like below.
 */
// void receive( byte[] data ) { 			// <-- default handler
void receive( byte[] data, String ip, int port ) {	// <-- extended handler
  
  
  // get the "real" message =
  // forget the ";\n" at the end <-- !!! only for a communication with Pd !!!
  data = subset(data, 0, data.length);
  String message = new String( data );
  
  // print the result
  println( "receive: \""+message+"\" from "+ip+" on port "+port );
}

When first run I get the expected result of :-

Hello 0
Hello 1
Hello 2
Hello 3
Hello 4
Hello 5
Hello 6
Hello 7
Hello 8
Hello 9
Hello 10
Hello 11
Hello 12
Hello 13
.
.
.
Hello 99999

But if I run it again I get :-

Hello 09999
Hello 19999
Hello 29999
Hello 39999
Hello 49999
Hello 59999
Hello 69999
Hello 79999
Hello 89999
Hello 99999
Hello 10999
Hello 11999
Hello 12999
Hello 13999
Hello 14999
Hello 15999
Hello 16999

The only way to clear the extra characters is to reset the Arduino. I guess I need to be clearing/flushing something somewhere but what?

Martin.

You need to clear (set all positions to zero) packetBuffer after the Serial.println() call. The read does not terminate the character string with a zero, so all previously read characters will still show.

That's kind of what I expected but how do I actually do it? For serial you can use Serial.flush(); but there doesn't seem to be an equivalent for UDP?

Martin.

You are not flushing UDP. You are resetting memory. Something like this:

for(int i=0;i<UDP_TX_PACKET_MAX_SIZE;i++) packetBuffer[i] = 0;

Thank you SuferTim. Just the help I needed as well. Cheers