Send UDP packet using Arduino problem

Hello everyone ,

I have a problem in my UDP packet sender i will describe it. first this is the code

#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
   
// Set WiFi credentials
#define WIFI_SSID "DIGI_676df8"
#define WIFI_PASS "aa219892"
#define UDP_PORT 9180

 // UDP
WiFiUDP UDP;
char packet[255];
char reply[] = "Packet received!";
char packet_ret[36] = {01,0x24,00,0x1E,06,00,00,00,0x0B,00,00,00,0x10,00,00,00,0x15,00,00,00,0x1A,00,00,00,0x1F,00,00,00,0x24,00,00,00,0x29,00,00,00} ;

 
void setup() {

  Serial.begin(115200);
  Serial.println();
   

  WiFi.begin(WIFI_SSID , WIFI_PASS);
   

  Serial.print("Connecting to ");
  Serial.print(WIFI_SSID );
  Serial.print(WIFI_PASS);

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

  Serial.println();
  Serial.print("Connected! IP address: ");
  Serial.println(WiFi.localIP());


  UDP.begin(UDP_PORT);
  Serial.print("Listening on UDP port ");
  Serial.println(UDP_PORT);
   
}
   
void loop() {
 
int packetSize = UDP.parsePacket();
  if (packetSize) {
    Serial.print("Received packet! Size: ");
    Serial.println(packetSize); 
    int len = UDP.read(packet, 255);
   if (len > 0)
    {
      packet[len] = '\0';
    }

    UDP.beginPacket(UDP.remoteIP(), UDP.remotePort());
    UDP.write(reply);
    UDP.endPacket();

    UDP.beginPacket(UDP.remoteIP(), UDP.remotePort());
    UDP.write(packet_ret);
    UDP.endPacket();

  }
 
 
}

I'm using Wireshark to monitor the packets , for the 1st reply array it worked perfectly and i can see it in the program
280211 22240.862216 192.168.1.6 192.168.1.2 UDP 58 9180 → 9180 Len=16
the sent data : Packet received!
while for the 2nd reply packet_ret it should be a36 bytes message, but in wireshark i could see that the arduino only sent 2 bytes as following
280212 22240.862610 192.168.1.6 192.168.1.2 UDP 44 9180 → 9180 Len=2
the sent data: only 0124

both arrays type is char, so i couldn't really know why this happen.

Try explicitly stating the packet size.

    UDP.write(packet_ret, 36);

I suspect it is currently terminating when if finds 0x00.

@

This Topic has been locked as it is the identical or too similar your other topic.

Please do not duplicate your questions as doing so wastes the time and effort of the volunteers trying to help you as they are then answering the same thing in different places.

Please create one topic only for your question and choose the forum category carefully. If you have multiple questions about the same project then please ask your questions in the one topic as the answers to one question provide useful context for the others, and also you won’t have to keep explaining your project repeatedly.

Repeated duplicate posting could result in a temporary or permanent ban from the forum.

Could you take a few moments to Learn How To Use The Forum

It will help you get the best out of the forum in the future.

Thank you.