ESP32 WiFiUDP source port

pasikav
Posts: 2
Joined: Tue Oct 10, 2023 12:28 pm

ESP32 WiFiUDP source port

Postby pasikav » Tue Oct 10, 2023 12:47 pm

Hello,

I have a project that sends UDP packet on chosen port and on the same port other devices answers with feedback. I am using AsyncUDP to check if there is any packet coming to my port and WiFiUDP to send packets. Problem is while listening, esp s3 is not able to bind socket to the same port, therefore source number for it is random (as always over 50000) and those other devices answer to the same port that the request came. On devices that are using standard esp32 i don't have problem with that (in that case esp is listening and sending from the same port).

This is my code for sending packet.

Code: Select all

 WiFiUDP scanUdp;

  scanUdp.begin(Network.localIP(), SCAN_PORT);
 
  byte header_buffer[SEARCH_REQUEST_HEADER_SIZE];
  memcpy(header_buffer, SEARCH_REQUEST_HEADER, SEARCH_REQUEST_HEADER_SIZE);
  
  if(!scanUdp.beginPacket(IPADDR_BROADCAST, SCAN_PORT)){
    DEBUG_PRINTLN(F("Scan request scanUDP.beginPacket returned an error"));
  }

  scanUdp.write(header_buffer, SEARCH_REQUEST_HEADER_SIZE);

  if(!scanUdp.endPacket()){
    DEBUG_PRINTLN(F("Scan request scanUDP.endPacket returned error"));
  } else {
    isScanRequestSend = true;
    scanningTime = millis();
  }
 
While on esp32 i found line scanUdp.begin(IPAddress, Port) useful for settings source port to my preference, this does not work on esp32 s3 and the bind for socket works only when i stop listening to the port.

So my question is: Is it possible to set source port for sending udp packet while listening to this port at the same time or is it possible to somehow get this random source port that esp is setting while unable to bind my port to socket, so I can look for answers on it too?

Who is online

Users browsing this forum: No registered users and 65 guests