Here is the Arduino code. I have it slowed down to one packet per second so you can see it work. You can speed it up by changing the msPerSecond value.
#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,2,2);
IPAddress gateway(192,168,2,1);
IPAddress subnet(255,255,255,0);
unsigned int UDPport = 5005; // local port to listen for UDP packets
// Change to your PC IP
IPAddress UDPServer(1,2,3,4); //Your UDP server
const int UDP_PACKET_SIZE= 48; // NTP time stamp is in the first 48 bytes of the message
byte packetBuffer[ UDP_PACKET_SIZE]; //buffer to hold incoming and outgoing packets
unsigned int noChange = 0;
// A UDP instance to let us send and receive packets over UDP
EthernetUDP Udp;
unsigned long currentTime;
unsigned long secondTime;
unsigned long msPerSecond = 1000UL;
int UDPCount = 0;
void setup()
{
Serial.begin(115200);
pinMode(4,OUTPUT);
digitalWrite(4,HIGH);
// start Ethernet and UDP
Serial.print(F("Starting w5100..."));
// Ethernet.begin(mac,ip,gateway,gateway,subnet);
while(!Ethernet.begin(mac)) {
Serial.println(F("failed. Retrying in 5 seconds."));
delay(5000);
Serial.print(F("Starting w5100..."));
}
Serial.println(Ethernet.localIP());
Udp.begin(UDPport);
delay(2000);
currentTime=millis();
secondTime = currentTime;
Serial.println("Ready");
}
void loop()
{
currentTime = millis();
getUDPpacket();
if(currentTime - secondTime > msPerSecond) {
byte rtnVal = Ethernet.maintain();
switch(rtnVal) {
case 1: Serial.println(F("\r\nDHCP renew fail"));
break;
case 2: Serial.println(F("\r\nDHCP renew ok"));
break;
case 3: Serial.println(F("\r\nDHCP rebind fail"));
break;
case 4: Serial.println(F("\r\nDHCP rebind ok"));
break;
}
Serial.println(F("\r\nUDP send"));
sendUDPpacket(UDPServer); // send an NTP packet to a time server
secondTime += msPerSecond;
}
}
unsigned int udpCount = 0;
// send an UDP request to the UDP server at the given address
unsigned long sendUDPpacket(IPAddress& address)
{
udpCount++;
// set all bytes in the buffer to 0
memset(packetBuffer, 0, UDP_PACKET_SIZE);
sprintf((char*)packetBuffer,"Arduino count %u",udpCount);
Udp.beginPacket(address, UDPport); // Requests are to port 5005
Udp.write(packetBuffer,UDP_PACKET_SIZE);
Udp.endPacket();
}
void getUDPpacket() {
if ( Udp.parsePacket() ) {
// We've received a packet, read the data from it
if(Udp.remoteIP() == UDPServer) {
Serial.print(F("UDP IP OK "));
}
else {
Serial.println(F("UDP IP Bad"));
return;
}
if(Udp.remotePort() == UDPport) {
Serial.println(F("Port OK"));
}
else {
Serial.println(F("Port Bad"));
return;
}
Udp.read(packetBuffer,UDP_PACKET_SIZE); // read the packet into the buffer
Serial.print(F("Received: "));
Serial.println((char*)packetBuffer);
}
}
edit: Here is the PC linux C code. I had to get into the "server" to get this code. Remember to open port 5005/udp on the PC "server" firewall or it will fail.
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
int sock, n, nr;
socklen_t fromlen;
struct sockaddr_in server;
struct sockaddr_in from;
sock = socket(AF_INET, SOCK_DGRAM, 0);
if (sock < 0)
printf("Can not create socket in server\n");
memset(&server, 0, sizeof(struct sockaddr_in));
server.sin_family = AF_INET;
server.sin_port = htons(5005);
server.sin_addr.s_addr = INADDR_ANY;
if(bind(sock, (struct sockaddr *)&server, sizeof(server)) < 0)
printf("Can not bind in server!\n");
memset(&from, 0, sizeof(struct sockaddr_in));
fromlen = sizeof(struct sockaddr_in);
while(1) {
int n, l1;
unsigned char tBuf[48];
unsigned char outBuf[48];
fflush(stdout);
n = recvfrom(sock, tBuf, sizeof(tBuf), 0, (struct sockaddr*) &from, &fromlen);
if (n < 0) {
printf("Can not receive in server!\n");
}
printf("%s from IP:%s, Port:%hu\r\n",&tBuf[0],inet_ntoa(from.sin_addr), ntohs(from.sin_port));
sprintf(outBuf,"Server: %s\0",tBuf);
socklen_t length = sizeof(struct sockaddr_in);
n = sendto(sock, outBuf, 48, 0, (const struct sockaddr *)&from, fromlen);
if(n < 0) {
printf("Can not send from client");
}
}
}
I saved this code as udpserver.c in my C directory.
cd C
cc udpserver.c -o udpserver.exe
./udpserver.exe