lwip_socket trying to connect to server

User avatar
PaulVdBergh
Posts: 58
Joined: Fri Feb 23, 2018 4:45 pm
Location: Brasschaat, Belgium

lwip_socket trying to connect to server

Postby PaulVdBergh » Sat May 12, 2018 10:35 am

Hi all,

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);
And the resulting monitor output:

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...
In my second attempt, I create a new socket each try :

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);
giving this result:

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
It seems like the ESP gets exhausted of socket handles...
So, my question is how I can try connecting to the server until he accepts the connection?

Thanks in advance,

Paul.

WiFive
Posts: 3529
Joined: Tue Dec 01, 2015 7:35 am

Re: lwip_socket trying to connect to server

Postby WiFive » Sat May 12, 2018 11:00 am


User avatar
fly135
Posts: 606
Joined: Wed Jan 03, 2018 8:33 pm
Location: Orlando, FL

Re: lwip_socket trying to connect to server

Postby fly135 » Sat May 12, 2018 2:19 pm

I use the Posix socket api and it works as expected.

John A

User avatar
PaulVdBergh
Posts: 58
Joined: Fri Feb 23, 2018 4:45 pm
Location: Brasschaat, Belgium

Re: lwip_socket trying to connect to server

Postby PaulVdBergh » Sat May 12, 2018 6:27 pm

Thanks for the suggestions.

Replacing

Code: Select all

lwip_close(sockfd)
with

Code: Select all

lwip_close_r(sockfd)
did the job.

Who is online

Users browsing this forum: No registered users and 71 guests