Sending data (ints) with UDPSendRecieveString

I am trying to send an integer value using the UDPSendRecieveString sketch. The example sketch works except the integer values are not displayed in the Processing debugger’s output; all the text makes it through but not the values. Can somebody make sense of what I am doing wrong?

#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[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
IPAddress ip(192, 168, 88, 252);

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;
int BoxCount = 11;
int MissedBoxes = 1;
int data[] = {BoxCount, MissedBoxes};

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

  Serial.begin(9600);
}

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

    // read the packet into packetBufffer
    Udp.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE);
    Serial.println("Contents:");
    Serial.println(packetBuffer);

    // send a reply, to the IP address and port that sent us the packet we received
    Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
    
    Udp.write("Reply: ");
    Udp.write(ReplyBuffer);
    Udp.write("data: ");
    Udp.write(11);
    Udp.write(",");
    Udp.write(data[1]);
    Udp.write("done");
    Udp.endPacket();
  }
  delay(10);
}


/*
  Processing sketch to run with this example
 =====================================================

 // Processing UDP example to send and receive string data from Arduino
 // press any key to send the "Hello Arduino" message


 import hypermedia.net.*;

 UDP udp;  // define the UDP object


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

 void draw()
 {
 }

 void keyPressed() {
 String ip       = "192.168.1.177";	// the remote IP address
 int port        = 8888;		// the destination port

 udp.send("Hello World", ip, port );   // the message to send

 }

 void receive( byte[] data ) { 			// <-- default handler
 //void receive( byte[] data, String ip, int port ) {	// <-- extended handler

 for(int i=0; i < data.length; i++)
 print(char(data[i]));
 println();
 }
 */

udp.PNG

Do you know what Udp.write() does? Do you know what data type it takes? You should, before you try to use it to send ints.

Why are you using an int to hold a value of 1, anyway?

Do you know what Udp.write() does?

Nope, not anymore than I can deduce by its name. It appears to send a string.

Do you know what data type it takes?

Nope,...a string? Please do enlighten me.

Why are you using an int to hold a value of 1?

The actual number could be much larger: 2,000-10,000+ . For testing purposes I wanted to assign the variable other than zero, although it makes little difference.

udp.write() so either a char or byte.

so either a char or byte.

Exactly. NOT an int. So, what do you suppose happens when you pass it an incorrect type? Which byte of the two-byte int is sent?

Try assigning a value larger than 255 to the int, and see if you get something other than 0 as the value.

So integers are not going to work. Do I break it up into two bytes and send it one at a time then reassemble it on the receiving end?

Why does this work:

Udp.write("Reply: 129347809");

but not this:

byte box = 1;
Udp.write(box);

So integers are not going to work. Do I break it up into two bytes and send it one at a time then reassemble it on the receiving end?

If you do that, you would be making ints work. It really isn't difficult, using highByte() and lowByte() on the sending end, and word() on the receiving end (or simple bit shifting and addition).

Or, convert the int to a string (itoa() and back to an int (atoi()).

Why does this work...but not this

You have not defined what "work" means, so no answer is possible. Both methods should send data from point A to point B. If that is not happening, you have one problem. If the data is being send, but not interpreted correctly, you have another problem.