I built a 2 schetches that basically listens to udp port 450 and echo's the message received.
The first using EthernetUDP
The second using WiFiUdp
EthernetUDP receives messages on port 450 and sends them back from port 450 to the PC port that initiated the message exchange. (= behaviour OK)
WifiUDP receives messages on port 450 and sends them back from port 4097 to the PC port that initiated the message exchange . (= behaviour NOT OK)
I saw this behavior using Wireshark, so I am 100% sure messages are sent and received. The target port of the reply message is OK, the source port is not.
This way of operating is not good because my PC client, who initiates the messages, expects the reply to originate from port 450. So my application works on EthernetUDP but not on WiFiUDP.
I looked at the source code and there seems no possibility to specify the source port from Arduino side using WifiUDP or from EthernetUDP either.
Any idea how to make WiFiUDP reply from the same port used to receive messages?
PS: There is a similar thread also adressing this problem, i am relaunching the issue as it was not solved on the other thread. The other forum member was unable to provide proof via Wireshark. I did. So to me this seems a possible firmware issue (Wifi Shield Modeld Wifi R3 genuine Arduino.)
Below: Wifi & Ethernet UDP sample code & attached Wireshark printscreen image with message trace.
Best regards,
Johi.
#include <SPI.h>
#include <WiFi.h>
#include <WiFiUDP.h>
#include <Wawi.h>
char ssid[] = "linksys"; // your network SSID (name)
char key[] = "uerv1z36"; // your network key
int status = WL_IDLE_STATUS; // the Wifi radio's status
// module adress:
byte mac[] = { 0x00, 0xAA, 0xBB, 0xCC, 0xDE, 55 };
byte ip[] = { 192, 168, 1, 55 };
// the router's gateway address:
byte gateway[] = { 192, 168, 1, 1 };
// the subnet:
byte subnet[] = { 255, 255, 225, 0 };
// TCP port
unsigned int port = 450;
// dns
byte dns[]={ 195,130,130,5 }; // your dns (not essential)
WiFiUDP Udp;
void setup()
{
// configure serial for diagnostics:
Serial.begin(115200);
Serial.println(F("EchUdpWifi starting."));
if (WiFi.status() == WL_NO_SHIELD)
{
Serial.println(F("WiFi shield not detected."));
while (true);
}
else
Serial.println("WiFi shield detected.");
Serial.println(F("Set static IP: "));
WiFi.config(ip,dns,gateway,subnet);
Serial.println("Network settings: ");
Serial.print("IP: ");
Serial.println(WiFi.localIP());
Serial.print("Port: ");
Serial.println(port);
Serial.print(F("Connect to Wifi Network ["));
Serial.print(ssid);
Serial.print(F("] ... "));
int status = WiFi.begin(ssid,key);
while ( status != WL_CONNECTED)
{
status = WiFi.begin(ssid,key);
delay(1000);
}
Serial.println(F("OK."));
// start TCP/IP Udp
Udp.begin(450);
}
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 remoteIp = Udp.remoteIP();
Serial.print(remoteIp);
Serial.print(", port ");
Serial.println(Udp.remotePort());
Serial.print("remoteIP=");
Serial.print(Udp.remoteIP());
Serial.print(" remotePort=");
Serial.print(Udp.remotePort());
Serial.print("\n");
Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
Udp.write(Udp.read());
Udp.endPacket();
}
}
#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
// module adress:
byte mac[] = { 0x00, 0xAA, 0xBB, 0xCC, 0xDE, 55 };
byte ip[] = { 192, 168, 1, 55 };
// the router's gateway address:
byte gateway[] = { 192, 168, 1, 1 };
// the subnet:
byte subnet[] = { 255, 255, 225, 0 };
// UDP port
unsigned int port = 450;
// dns
byte dns[]={ 195,130,130,5 };
EthernetUDP Udp;
void setup()
{
// configure serial for diagnostics:
Serial.begin(115200);
Ethernet.begin(mac,ip,gateway,subnet);
Udp.begin(port);
}
void loop()
{
int packetSize = Udp.parsePacket();
if (packetSize)
{
Serial.print("Received packet of size ");
Serial.println(packetSize);
Serial.print("From ");
IPAddress remoteIp = Udp.remoteIP();
Serial.print(remoteIp);
Serial.print(", port ");
Serial.println(Udp.remotePort());
Serial.print("remoteIP=");
Serial.print(Udp.remoteIP());
Serial.print(" remotePort=");
Serial.print(Udp.remotePort());
Serial.print("\n");
Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
Udp.write(Udp.read());
Udp.endPacket();
}
}