tcp_server example not able to reconnect the server after disconnect

dhavalpatel0114
Posts: 2
Joined: Wed Oct 10, 2018 8:32 pm

Re: tcp_server example not able to reconnect the server after disconnect

Postby dhavalpatel0114 » Tue Nov 06, 2018 7:29 pm

attached full code for TCP SERVER + SOFTAP + STA with bugfix of connection problems after disconnect and connect again.

+ fix with able to resume socket even after disconnecting and connecting back to the station or softap.

you can switch between station and softap by changing rf_mode value.

rf_mode = 0 for softap
rf_mode = 1 for station
Attachments
tcp_server.c
(9.15 KiB) Downloaded 130 times

dhavalpatel0114
Posts: 2
Joined: Wed Oct 10, 2018 8:32 pm

Re: tcp_server example not able to reconnect the server after disconnect

Postby dhavalpatel0114 » Fri Nov 16, 2018 7:28 pm

Another easy solution for this problem is to close server_socket(listen_sock) when we are closing the client socket(sock).
Add the highlighted line into tcp_server_task() function.
  1.  
  2.  if (sock != -1) {
  3.             ESP_LOGE(TAG, "Shutting down socket and restarting...");
  4.             shutdown(sock, 0);
  5.             close(sock);
  6.             close(listen_sock);
  7.         }
  8.     }
  9.     vTaskDelete(NULL);
  10. }

adam_esp
Posts: 4
Joined: Mon Apr 29, 2019 12:09 pm

Re: tcp_server example not able to reconnect the server after disconnect

Postby adam_esp » Tue Apr 30, 2019 8:00 am

But after a few closures and openings, error 112 returns!
Socket unable to bind: errno 112
Did anyone solve the problem permanently??

tangqingcai
Posts: 11
Joined: Thu Mar 23, 2017 3:05 am

Re: tcp_server example not able to reconnect the server after disconnect

Postby tangqingcai » Mon May 13, 2019 8:08 am

hi,mikemoy and axellin ;
About your answer,you just move while(1) to listen above;
This modify do not close listen_socket and do not need to recreate new listen_socket, do not need to re-bind ,so you could work well in this case.
But if you need to change your PORT to other, this modify is not helpful ;
because you should work as this workflow, create server_socket,bind,accept,R/W client_socket, close server_socket;
This is my opinion;

tangqingcai
Posts: 11
Joined: Thu Mar 23, 2017 3:05 am

Re: tcp_server example not able to reconnect the server after disconnect

Postby tangqingcai » Mon May 13, 2019 8:35 am

Update my test;
Added code close(listen_sock);after close(sock); its work well too;
but i have a question,i print out listen_sock ,it will be increased in every cycle;
Is it correctly?
Because i have been closed it in the end of cycle;I think the listen_sock will be released.

I (3114) example: Connected to AP
I (3114) example: Socket created id:54
I (3124) example: Socket binded
I (3124) example: Socket listening
I (9024) example: Socket accepted
I (11684) example: Received 4 bytes from 192.168.199.229:
I (11684) example: abcd
I (12294) example: Received 4 bytes from 192.168.199.229:
I (12294) example: abcd
I (12914) example: Received 4 bytes from 192.168.199.229:
I (12914) example: abcd
I (16594) example: Connection closed
E (16594) example: Shutting down socket and restarting...
I (16604) example: Socket created id:56
I (16604) example: Socket binded
I (16604) example: Socket listening
I (27964) example: Socket accepted
I (32774) example: Received 4 bytes from 192.168.199.229:
I (32774) example: abcd
I (33394) example: Received 4 bytes from 192.168.199.229:
I (33394) example: abcd
I (34014) example: Received 4 bytes from 192.168.199.229:
I (34014) example: abcd
I (45374) example: Connection closed
E (45374) example: Shutting down socket and restarting...
I (45374) example: Socket created id:58
I (45374) example: Socket binded
I (45384) example: Socket listening

Who is online

Users browsing this forum: jotman01, pegasus.automa and 15 guests