UDP communication

Hi everyone,
I am working on using UDPSendReceiveString example to receive message from the UDPClient and send reply. I am using Ethernet shield to provide UDPServer and wifi2866-12E working as UDPClient. The UDPclient send Hello message to the server but it does not receive any response! the code for UDPClient is :
/*

  • 31 mar 2015
  • This sketch sends UDP packets to an UDP server.
  • On a Mac the NC command can be used to receive the messages. ( sudo nc -lu 888 ).
  • Configuration : Enter the ssid and password of your Wifi AP. Enter the IP number and Udp number of your receiving UDP server.

*/

#include <ESP8266WiFi.h>
#include <WiFiUDP.h>

const char* ssid = "";
const char
password = "
***";
char packetBuffer[20];
// A UDP instance to let us send and receive packets over UDP
WiFiUDP Udp;

void setup() {
Serial.begin(115200);
delay(10);

// We start by connecting to a WiFi network

Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);

WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}

Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());

Udp.beginPacket("192.168.1.59", 8888);
Udp.write("hello");
Udp.endPacket();
}

void loop() {

int packetSize = Udp.parsePacket();
if (packetSize) {
int len = Udp.read(packetBuffer, 20);
if (len > 0) packetBuffer[len] = 0;
Serial.println(packetBuffer);

}

}

Is the ethernet shield receiving the packet?

edit: beginPacket returns a value. 1 = success, 0 = fail.

  if(Udp.beginPacket("192.168.1.59", 8888) == 0)
  {
      Serial.println("Begin packet failed");
  }

The main reason for a fail is the inability to resolve the domain name. Some DNS servers will fail if a localnet private IP is used as the domain name.

yes, it does receive the packet

And the ethernet shield is sending a return packet? Is this an Arduino WiFi shield? I had some problems with the WiFi shield sending UDP packets. Mine used a different port to send a packet that it listens on. It caused me some grief.

edit: I just checked again, and it does send on a different port. My test was using port 5005 in Udp.begin(), but when I send a packet, it uses port 4097 to send the packet. My "server" responds to the packet using port 4097 as the destination, but the WiFi shield is listening on port 5005, not 4097.

So, what do I suppose to change?

I really don't know what to tell you. I had other problems with the WiFi shield, especially the server firmware. I don't use it now.

The problem could also be the connection tracking function of your router if the packets go through it. My router holds the connection from the WiFi shield's port 4097 to the server's port 5005, but when the server responds with a destination of port 4097 5005, the router has trouble with the routing if the server is on the public interface and you try using port 5005.

If you are using the esp8266, as one of the include files indicates, are you certain the WiFiUDP.h is the correct file? I don't know if it has the same problems as the WiFi shield.

Hi,
I have tried these two codes:

UDPClient using ESP2866-12E:

#include <ESP8266WiFi.h>
#include <WiFiUDP.h>

const char* ssid     = "m1";
const char* password = "*******";
char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet,
WiFiUDP Udp;

void setup() {
  Serial.begin(115200);
  delay(10);

  // We start by connecting to a WiFi network

  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  
  WiFi.begin(ssid, password);
  
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected");  
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
  
  Udp.beginPacket("192.168.1.59", 1234);
  Udp.write("hello");
    Udp.endPacket();
}

void loop() {

    int packetSize = Udp.parsePacket();
  if (packetSize)
  {
    Serial.print("Received packet of size ");
    Serial.println(packetSize);
    Serial.print("From ");
    

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

  }
      
}

and UDPServer using Ethernet Shield:

#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, 59);

unsigned int localPort = 1234;
unsigned int remotPort = 5005;

// 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;

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*, 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(), remotPort);*
  • Udp.write(ReplyBuffer);*
  • Udp.endPacket();*
  • }*
  • delay(10);*
    }
    but still the server receive from the client but the client does not receive from the server, please help!!

Are both on the same router? Both private 192.168.1.x IPs? Have you tried using Wireshark to check the packets?

yes, they are on the same router and I have not used wireshark