My ESP32 requests configuration data from a server just after startup. If the later isn't ready to accept a socket connection, the ESP has to retry after a short delay until the connection is established and the data can be exchanged.
This is my first attempt: (doesn't work...)
Code: Select all
ESP_LOGI(tag, "Creating socket");
sockfd = lwip_socket(AF_INET, SOCK_STREAM, 0);
if(0 > sockfd)
{
ESP_LOGE(tag, "sockfd = %i", sockfd);
return;
}
do
{
ESP_LOGI(tag, "Connecting");
connStatus = lwip_connect(sockfd, (const struct sockaddr*)&serv_addr, sizeof(serv_addr));
if(0 > connStatus)
{
perror("connect : ");
ESP_LOGE(tag, "Connection failed, retry...");
lwip_close(sockfd);
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
}
while(0 > connStatus);
Code: Select all
I (5201) app_main: Creating socket
I (5206) app_main: Connecting
connect : : Connection reset by peer
E (5274) app_main: Connection failed, retry...
I (6275) app_main: Connecting
connect : : Socket is not connected
E (6275) app_main: Connection failed, retry...
I (7276) app_main: Connecting
connect : : Socket is not connected
E (7276) app_main: Connection failed, retry...
I (8277) app_main: Connecting
connect : : Socket is not connected
E (8277) app_main: Connection failed, retry...
I (9279) app_main: Connecting
connect : : Socket is not connected
E (9279) app_main: Connection failed, retry...
I (10280) app_main: Connecting
connect : : Socket is not connected
E (10280) app_main: Connection failed, retry...
Code: Select all
do
{
ESP_LOGI(tag, "Creating socket");
sockfd = lwip_socket(AF_INET, SOCK_STREAM, 0);
ESP_LOGI(tag, "sockfd = %i", sockfd);
if(0 > sockfd)
{
return;
}
int optVal = 1;
int result = lwip_setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &optVal, sizeof(optVal));
ESP_LOGI(tag, "setsockopt(SO_REUSEADDR) returned %i", result);
result = lwip_setsockopt(sockfd, SOL_SOCKET, SO_REUSEPORT, &optVal, sizeof(optVal));
ESP_LOGI(tag, "setsockopt(SO_REUSEPORT) returned %i", result);
ESP_LOGI(tag, "Connecting");
connStatus = lwip_connect(sockfd, (const struct sockaddr*)&serv_addr, sizeof(serv_addr));
if(0 > connStatus)
{
perror("connect : ");
ESP_LOGE(tag, "Connection failed, retry...");
lwip_close(sockfd);
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
}
while(0 > connStatus);
Code: Select all
I (5709) app_main: Creating socket
I (5714) app_main: sockfd = 4096
I (5717) app_main: setsockopt(SO_REUSEADDR) returned -1
I (5723) app_main: setsockopt(SO_REUSEPORT) returned -1
I (5729) app_main: Connecting
connect : : Connection reset by peer
E (5742) app_main: Connection failed, retry...
I (6742) app_main: Creating socket
I (6742) app_main: sockfd = 4097
I (6743) app_main: setsockopt(SO_REUSEADDR) returned -1
I (6745) app_main: setsockopt(SO_REUSEPORT) returned -1
I (6751) app_main: Connecting
connect : : Connection reset by peer
E (6763) app_main: Connection failed, retry...
I (7764) app_main: Creating socket
I (7764) app_main: sockfd = 4098
I (7765) app_main: setsockopt(SO_REUSEADDR) returned -1
I (7767) app_main: setsockopt(SO_REUSEPORT) returned -1
I (7773) app_main: Connecting
connect : : Connection reset by peer
E (7785) app_main: Connection failed, retry...
I (8786) app_main: Creating socket
I (8786) app_main: sockfd = 4099
I (8787) app_main: setsockopt(SO_REUSEADDR) returned -1
I (8789) app_main: setsockopt(SO_REUSEPORT) returned -1
I (8795) app_main: Connecting
connect : : Connection reset by peer
E (8809) app_main: Connection failed, retry...
I (9810) app_main: Creating socket
I (9811) app_main: sockfd = -1
So, my question is how I can try connecting to the server until he accepts the connection?
Thanks in advance,
Paul.