opened
korstiaanS
username_0
Hi,

Can somebody explain me why the following code

```
void sendCmdToDevice(int sd, char *ip, char *sendstring, int port) {
    uint8_t retry=0;
    int sent_data;
    struct sockaddr_in destaddr;
    char packetBuffer[MAX_BUFFER+1]; // buffer to hold incoming packet
    unsigned int cli_len;
    int recv_data;
    
    //Set destination IP
    destaddr.sin_family = AF_INET;
    destaddr.sin_addr.s_addr = inet_addr(ip);
    destaddr.sin_port = htons(port);
    cli_len = sizeof(destaddr);
    
    do {
        sent_data = sendto(sd, sendstring, strlen(sendstring), 0, (struct sockaddr*)&destaddr, sizeof(destaddr));
        ESP_LOGI(TAG, "Send UDP packet: %s to %s:%d", sendstring, ip, port);
        if (sent_data < 0) {
            ESP_LOGW(TAG, "UDP send failed return code: %d", sent_data);
            vTaskDelay(250 / portTICK_PERIOD_MS); // wait to process and receive the answer back
        }
        vTaskDelay(10 / portTICK_PERIOD_MS); // wait to process and receive the answer back
        recv_data = recvfrom(sd, packetBuffer, sizeof(packetBuffer), 0, (struct sockaddr*)&destaddr, &cli_len); // also wait on timeout!
        char *recip = inet_ntoa(destaddr.sin_addr);
        if ((strcmp(recip, ip) == 0) && (recv_data > 0)) {
            packetBuffer[recv_data] = '\0';
            ESP_LOGI(TAG, "Received from %s UDP packet: %s", recip, packetBuffer);
            if (strstr(packetBuffer, "OK") != NULL) {
                break;
            }
        }
        retry++;
    } while (retry < 100);
}

```
generates 1 or more `sendto` errors (-1) and we have to retry a lot ONLY the first time after a reboot of the ESP32?
All the following times I execute this I don't get any errors anymore and the responses also come quick. (almost no retries anymore)

First time after boot (execute method for 7 ip's in a loop) gives result:

```
I (12284) livingHub - myUdp.c: Send UDP packet: A1=L to 192.168.56.5:9750
I (12364) livingHub - myUdp.c: Send UDP packet: A1=L to 192.168.56.5:9750
I (12444) livingHub - myUdp.c: Send UDP packet: A1=L to 192.168.56.5:9750
I (12524) livingHub - myUdp.c: Send UDP packet: A1=L to 192.168.56.5:9750
W (12524) livingHub - myUdp.c: UDP send failed return code: -1
I (12524) livingHub - myUdp.c: Send UDP packet: A1=L to 192.168.56.5:9750
W (12524) livingHub - myUdp.c: UDP send failed return code: -1
I (12524) livingHub - myUdp.c: Send UDP packet: A1=L to 192.168.56.5:9750
W (12524) livingHub - myUdp.c: UDP send failed return code: -1
I (12854) livingHub - myUdp.c: Send UDP packet: A1=L to 192.168.56.5:9750
I (12864) livingHub - myUdp.c: Received from 192.168.56.5 UDP packet: 61FCA1=LOK
I (12864) livingHub - myUdp.c: Send UDP packet: A1=L to 192.168.56.4:9750
I (12904) livingHub - myUdp.c: Send UDP packet: A1=L to 192.168.56.4:9750
I (12944) livingHub - myUdp.c: Send UDP packet: A1=L to 192.168.56.4:9750
I (12994) livingHub - myUdp.c: Send UDP packet: A1=L to 192.168.56.4:9750
I (13064) livingHub - myUdp.c: Send UDP packet: A1=L to 192.168.56.4:9750
I (13104) livingHub - myUdp.c: Send UDP packet: A1=L to 192.168.56.4:9750
I (13184) livingHub - myUdp.c: Send UDP packet: A1=L to 192.168.56.4:9750
I (13214) livingHub - myUdp.c: Received from 192.168.56.4 UDP packet: 617DA1=LOK
I (13214) livingHub - myUdp.c: Send UDP packet: A1=L to 192.168.56.2:9750
I (13234) livingHub - myUdp.c: Send UDP packet: A1=L to 192.168.56.2:9750
I (13244) livingHub - myUdp.c: Send UDP packet: A1=L to 192.168.56.2:9750
I (13314) livingHub - myUdp.c: Send UDP packet: A1=L to 192.168.56.2:9750
I (13374) livingHub - myUdp.c: Send UDP packet: A1=L to 192.168.56.2:9750
I (13404) livingHub - myUdp.c: Send UDP packet: A1=L to 192.168.56.2:9750
I (13454) livingHub - myUdp.c: Send UDP packet: A1=L to 192.168.56.2:9750
I (13474) livingHub - myUdp.c: Received from 192.168.56.2 UDP packet: 5BD5A1=LOK
I (13484) livingHub - myUdp.c: Send UDP packet: A1=L to 192.168.56.8:9750
I (13504) livingHub - myUdp.c: Send UDP packet: A1=L to 192.168.56.8:9750
I (13574) livingHub - myUdp.c: Send UDP packet: A1=L to 192.168.56.8:9750
I (13624) livingHub - myUdp.c: Send UDP packet: A1=L to 192.168.56.8:9750
I (13674) livingHub - myUdp.c: Send UDP packet: A1=L to 192.168.56.8:9750
I (13704) livingHub - myUdp.c: Send UDP packet: A1=L to 192.168.56.8:9750
I (13774) livingHub - myUdp.c: Send UDP packet: A1=L to 192.168.56.8:9750
I (13794) livingHub - myUdp.c: Received from 192.168.56.8 UDP packet: 6190A1=LOK
I (13804) livingHub - myUdp.c: Send UDP packet: A1=L to 192.168.56.11:9750
I (13814) livingHub - myUdp.c: Send UDP packet: A1=L to 192.168.56.11:9750
I (13844) livingHub - myUdp.c: Send UDP packet: A1=L to 192.168.56.11:9750
I (13894) livingHub - myUdp.c: Send UDP packet: A1=L to 192.168.56.11:9750
I (13944) livingHub - myUdp.c: Send UDP packet: A1=L to 192.168.56.11:9750
I (14004) livingHub - myUdp.c: Send UDP packet: A1=L to 192.168.56.11:9750
I (14054) livingHub - myUdp.c: Send UDP packet: A1=L to 192.168.56.11:9750
I (14104) livingHub - myUdp.c: Received from 192.168.56.11 UDP packet: 6163A1=LOK
I (14104) livingHub - myUdp.c: Send UDP packet: A1=L to 192.168.56.3:9750
I (14114) livingHub - myUdp.c: Send UDP packet: A1=L to 192.168.56.3:9750
I (14144) livingHub - myUdp.c: Send UDP packet: A1=L to 192.168.56.3:9750
I (14204) livingHub - myUdp.c: Send UDP packet: A1=L to 192.168.56.3:9750
I (14244) livingHub - myUdp.c: Send UDP packet: A1=L to 192.168.56.3:9750
I (14314) livingHub - myUdp.c: Send UDP packet: A1=L to 192.168.56.3:9750
I (14354) livingHub - myUdp.c: Send UDP packet: A1=L to 192.168.56.3:9750
I (14394) livingHub - myUdp.c: Received from 192.168.56.3 UDP packet: E14EA1=LOK
I (14394) livingHub - myUdp.c: Send UDP packet: D1=L to 192.168.56.11:9750
I (14414) livingHub - myUdp.c: Send UDP packet: D1=L to 192.168.56.11:9750
I (14424) livingHub - myUdp.c: Received from 192.168.56.11 UDP packet: 6163D1=LOK
```

BUT after this first time all the consecutives call to all 7 always gives:

```
I (150774) livingHub - myUdp.c: Send UDP packet: A1=L to 192.168.56.5:9750
I (150784) livingHub - myUdp.c: Received from 192.168.56.5 UDP packet: 61FCA1=LOK
I (150784) livingHub - myUdp.c: Send UDP packet: A1=L to 192.168.56.4:9750
I (150794) livingHub - myUdp.c: Received from 192.168.56.4 UDP packet: 617DA1=LOK
I (150794) livingHub - myUdp.c: Send UDP packet: A1=L to 192.168.56.2:9750
I (150814) livingHub - myUdp.c: Received from 192.168.56.2 UDP packet: 5BD5A1=LOK
I (150814) livingHub - myUdp.c: Send UDP packet: A1=L to 192.168.56.8:9750
I (150824) livingHub - myUdp.c: Received from 192.168.56.8 UDP packet: 6190A1=LOK
I (150834) livingHub - myUdp.c: Send UDP packet: A1=L to 192.168.56.11:9750
I (150844) livingHub - myUdp.c: Received from 192.168.56.11 UDP packet: 6163A1=LOK
I (150844) livingHub - myUdp.c: Send UDP packet: A1=L to 192.168.56.3:9750
I (150874) livingHub - myUdp.c: Received from 192.168.56.3 UDP packet: E14EA1=LOK
I (150874) livingHub - myUdp.c: Send UDP packet: D1=L to 192.168.56.11:9750
I (150924) livingHub - myUdp.c: Received from 192.168.56.11 UDP packet: 6163D1=LOK
```

At the end it works fine but still these two questions:

1. Why, the first time I always get 1 or more sendto -1 errors?
2. Why do I have to retry so much only the first time? (caching somewhere in the ESP?)
why errors and a lot of retries for UDP sendto only the first time after reboot
issue
created
TimXia
username_1
@username_0 I guess it is caused by ARP. The buffer for UDP is limited before ARP request is replied. When the buffers are full, it will return -1 until ARP request succeeds.
comment
closed
Alvin1Zhang
username_2
issue