Chat freely about anything...

User avatar
By PatrykW
#78421 Hello everyone.
I'm having a weird issue with UDP performance.
What I'm doing is broadcasting an UDP packet (roughly 50 bytes) from ESP8266, then the receiving computer sends back an acknowledgment back (which is exact same packet plus one extra byte) and the entire operation takes whooping 100+ milliseconds. I find it really weird, because by main loop on ESP side is under 1ms, the server application sends ACK back in less than half ms, and the average ping from server machine to the ESP is 2ms.
Anyone knows what can cause behavior like that?

sending like that:
Code: Select allvoid sendUDP( IPAddress ip, int port, char msg[]){
  WiFiUDP UdpSend;
  UdpSend.beginPacket( ip, port );
  UdpSend.printf("%s", "+=+=");
  UdpSend.printf("%s", msg);
  UdpSend.printf("%s", "*=*=");
  UdpSend.endPacket();
}


and receiving like that (in the loop, of course):
Code: Select all  int packetSize = UdpIn.parsePacket();
  if (packetSize) {
    int len = UdpIn.read( incomingPacket, 255 );
    if ( len > 0 ){
      incomingPacket[len] = 0;
    }
    packetPayload = String(incomingPacket);
  }

everything using standard WiFiUdp.h so nothing fancy

----edit----
I just tried sending the packet to a specific machine instead of using multicast address and it seems to be more than 10 times faster this way. Why would multicast be so much slower?.....
User avatar
By rudy
#78427
PatrykW wrote:I just tried sending the packet to a specific machine instead of using multicast address and it seems to be more than 10 times faster this way. Why would multicast be so much slower?.....

Multicast has no target address to send to. So there is no confirmation of reception possible. With WiFi, even if you are sending UDP, there is a mechanism to ensure reliable transfers. So sending a Unicast message will have retries at the WiFi level.

What I have noticed is that multicast is unreliable. No delays if it gets there. So either get there quick, or not get there at all. What you described (slow transfers) I have not seen.
User avatar
By rudy
#78428
rudy wrote:
PatrykW wrote:I just tried sending the packet to a specific machine instead of using multicast address and it seems to be more than 10 times faster this way. Why would multicast be so much slower?.....

Multicast has no target address to send to. So there is no confirmation of reception possible. With WiFi, even if you are sending UDP, there is a mechanism to ensure reliable transfers. So sending a Unicast message will have retries at the WiFi level.

Due to this, and if I need to use Multicast, I will for the discovery, and after that switch to unicast for the remaining exchanges.

What I have noticed is that multicast is unreliable. No delays if it gets there. So either get there quick, or not get there at all. What you described (slow transfers) I have not seen.