read the UDP datagram into a char array and parse it as post # 2
e.g. this reads a UDP datagram and parses it
#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, 1, 177);
unsigned int localPort = 999;//8888; // local port to listen on
EthernetUDP Udp;
void setup() {
Serial.begin(115200);
Serial.println("UDP program");
// start the Ethernet and UDP:
if (Ethernet.begin(mac))Serial.println("Configured Ethernet using DHCP OK");
else Serial.println("Failed to configure Ethernet using DHCP");
// print your local IP address:
Serial.print("My IP address: ");
for (byte thisByte = 0; thisByte < 4; thisByte++) {
// print the value of each byte of the IP address:
Serial.print(Ethernet.localIP()[thisByte], DEC);
Serial.print(".");
}
Serial.println();
if (Udp.begin(localPort))Serial.println("UDP begin() OK");
else Serial.println("UDP begin() failed!");
}
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
char data[50] = {0};
int i = 0;
Serial.print("data = ");
for (i = 0; i < packetSize; i++) // process UDP PACKET FOR DATA
Serial.print(data[i] = (char)Udp.read()); // read the NEXT byte and check it
int k, j;
// find last space in string convert two integers
if (sscanf(strrchr(data, ' '), "%d,%d", &k, &j) == 2) {
Serial.print("\nk= ");
Serial.println(k);
Serial.print("j= ");
Serial.println(j);
}
}
}
I run a Java program to send a datagram
F:\temp>java UDPchat
chat program: IP address DELL2/192.168.1.68 port 999
Remote IP: 192.168.111.168 50155 15000,8882
Sending to 192.168.1.177 socket 999 data: Remote IP: 192.168.111.168 50155 15000,8882
on a UNO with Ethernet Shield V2 the Serial Monitor displays
07:47:14.369 -> UDP program
07:47:26.476 -> Configured Ethernet using DHCP OK
07:47:26.511 -> My IP address: 192.168.1.177.
07:47:26.511 -> UDP begin() OK
07:47:40.575 -> Received packet of size 43
07:47:40.575 -> From 192.168.1.68, port 59021
07:47:40.575 -> data = Remote IP: 192.168.111.168 50155 15000,8882
07:47:40.575 -> k= 15000
07:47:40.575 -> j= 8882
running a VB.NET program to send a datagram console displays
VB.NET program: transmit UDP datagram
enter text to transmit ?
Remote IP: 192.168.111.168 50155 15000,8882
Arduino serial monitor displays
08:20:42.419 -> From 192.168.1.68, port 53555
08:20:42.419 -> data = Remote IP: 192.168.111.168 50155 15000,8882
08:20:42.419 -> k= 15000
08:20:42.419 -> j= 8882