this UDPchat program is running on a Mega with a ethernet shield V1
it communicates with a java program running on a WIN10 PC
// UDCP chat program using Arduino Ethernet Shield
#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
#define UDP_TX_PACKET_MAX_SIZE 100 //increase UDP size
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED}; // MAC address update as required
IPAddress localIP(192, 168, 1, 176); // local static localIP address
IPAddress remoteIP(192, 168, 1, 65); // local static localIP address
unsigned int port = 10000; // port to receive/transmit
char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet
EthernetUDP Udp; // An EthernetUDP instance to let us send and receive packets over UDP
void setup() {
Serial.begin(115200);
Serial.println("\n\nUDP chat program");
// select static or dynamic IP initialization
//Ethernet.begin(mac, localIP); // static IP initialize Ethernet shield
if(Ethernet.begin(mac)) // dyname IP initialize Ethernet shield using DHCP
Serial.println("Configured Ethernet using DHCP OK");
else Serial.println("Failed to configure Ethernet using DHCP");
Serial.print("My localIP address: "); // print local localIP address:
Serial.println(Ethernet.localIP());
// Initializes the ethernet UDP library and network settings.
if (Udp.begin(port)) Serial.println("UDP begin() OK");
else Serial.println("UDP begin() failed!");
Serial.print("UDP_TX_PACKET_MAX_SIZE ");
Serial.println(UDP_TX_PACKET_MAX_SIZE);
}
void loop() {
// if datagram received read it
int packetSize = Udp.parsePacket();
if (packetSize) {
Serial.print("Received packet of size ");
Serial.print(packetSize); // datagram packet size
Serial.print(" From ");
remoteIP = Udp.remoteIP(); // from localIP
Serial.print(remoteIP);
Serial.print(", port "); // from port
Serial.println(Udp.remotePort());
memset(packetBuffer, 0, UDP_TX_PACKET_MAX_SIZE);
// read the packet into packetBufffer and print contents
Udp.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE);
Serial.print("Contents: ");
Serial.println(packetBuffer);
}
if (Serial.available()) { // if text entered read it and send it
char text[100] = { 0 };
Serial.readBytesUntil('\n', text, 100);
Serial.print("Transmitting datagram to ");
Serial.print(remoteIP);
Serial.print(" port "); // from port
Serial.print(port);
Serial.print(" data ");
Serial.println(text);
Udp.beginPacket(remoteIP, port); // send to remote localIP and port 999
Udp.write(text);
Udp.endPacket();
}
delay(10);
}
java program
// UDPchat.java - simple peer-to-peer chat program using UDP
// - given remote IP address can send strings using UDP datagrams
// - will also wait for datagrams and display contents
// remote IP and port are specified via command line - default IP is 127.0.0.1 (i.e. localhost) and port is 1000
//
// e.g. to send datagrams to IP 146.227.59.130 port 1006
// java chat 146.227.59.130 1006
import java.io.*;
import java.util.*;
import java.net.*;
public class UDPchat extends Thread
{
private final static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
int port=999;//10001;// 10000; // port to send/receive datagrams on
String remoteIPaddress= "192.168.1.177";//"169.254.144.20";//("192.168.1.8");//127.0.0.1"); // IP to send datagrams
// constructor, parameter is command line parameters
public UDPchat(String args[]) throws Exception
{
// get remote IP address and port from command line parameters
if (args.length > 0) remoteIPaddress = (args[0]); // get IPaddress
if (args.length > 1) port = Integer.parseInt(args[1]); // get port number
System.out.println("chat program: IP address " + InetAddress.getLocalHost().toString() + " port " + port );
start(); // start thread to receive and display datagrams
// loop waiting for keyboard input, send datagram to remote IP
while(true)
try
{
String s = in.readLine(); // read a String
System.out.println("Sending to " + remoteIPaddress + " socket " + port + " data: " + s);
byte[] data = s.getBytes(); // convert to byte array
DatagramSocket theSocket = new DatagramSocket(); // create datagram socket and the datagram
DatagramPacket theOutput = new DatagramPacket(data, s.length(), InetAddress.getByName(remoteIPaddress), port);
theSocket.send(theOutput); // and send the datagram
}
catch (Exception e) {System.out.println("Eroor sending datagram " + e);}
}
// thread run method, receives datagram and display contents as a string
public void run()
{
try
{
// open DatagramSocket to receive
DatagramSocket ds = new DatagramSocket(port);
// loop forever reading datagrams from the DatagramSocket
while (true)
{
byte[] buffer = new byte[65507]; // array to put datagrams in
DatagramPacket dp = new DatagramPacket(buffer, buffer.length); // DatagramPacket to hold the datagram
ds.receive(dp); // wait for next datagram
String s = new String(dp.getData(),0,dp.getLength()); // get contenets as a String
System.out.println("UDP datagram length " + s.length()+ " from IP " + dp.getAddress() + " received: " + s );
s = "Acknowledge\n";
// byte[] data = s.getBytes(); // convert to byte array
// DatagramSocket theSocket = new DatagramSocket(); // create datagram socket and the datagram
// DatagramPacket theOutput = new DatagramPacket(data, data.length, InetAddress.getByName(remoteIPaddress), port);
// theSocket.send(theOutput); // and send the datagram
}
}
catch (SocketException se) {System.err.println("chat error " + se); }
catch (IOException se) {System.err.println("chat error " + se);}
System.exit(1); // exit on error
}
public static void main(String args[]) throws Exception
{
UDPchat c=new UDPchat(args);
}
}
mega serial output
UDP chat program
Configured Ethernet using DHCP OK
My localIP address: 192.168.1.177
UDP begin() OK
UDP_TX_PACKET_MAX_SIZE 100
Received packet of size 5 From 192.168.1.65, port 49777
Contents: hello
Transmitting datagram to 192.168.1.65 port 10000 data ok thank you
Transmitting datagram to 192.168.1.65 port 10000 data hello from Mega
Transmitting datagram to 192.168.1.65 port 10000 data test2 from mega
Received packet of size 13 From 192.168.1.65, port 51015
Contents: hello from pc
Received packet of size 23 From 192.168.1.65, port 52938
Contents: test from pc 1234567890
java program console output
F:\Ardunio\Networking\Ethernet\EthernetSheildV1\UDP\ChatProgram>java UDPchat 192.168.1.177 10000
chat program: IP address BB-DELL2/192.168.1.65 port 10000
hello
Sending to 192.168.1.177 socket 10000 data: hello
UDP datagram length 13 from IP /192.168.1.177 received: ok thank you
UDP datagram length 16 from IP /192.168.1.177 received: hello from Mega
UDP datagram length 16 from IP /192.168.1.177 received: test2 from mega
hello from pc
Sending to 192.168.1.177 socket 10000 data: hello from pc
test from pc 1234567890
Sending to 192.168.1.177 socket 10000 data: test from pc 1234567890
what are you running on the laptop to receive the UDP datagrams?