How to correctly stop a TCP server from listening?

User avatar
kolban
Posts: 131
Joined: Tue Jun 16, 2015 1:09 pm
Location: Fort Worth, Texas, USA

How to correctly stop a TCP server from listening?

Postby kolban » Tue Oct 27, 2015 12:05 pm

I am using the SDK to create a socket server within my ESP8266. To accomplish this, I use the SDK API called espconn_accept(). This works great. However, in my application I also want to stop the server from listening ... and this is where I am confused.

What API should I use to stop the socket from listening any further?

I thought maybe that it might have been espconn_delete(), however when I call that, I get a return code of -5 (ESPCONN_INPROGRESS) and the socket is still listening for new connections.

ESP_Faye
Posts: 1646
Joined: Mon Oct 27, 2014 11:08 am

Re: How to correctly stop a TCP server from listening?

Postby ESP_Faye » Fri Oct 30, 2015 12:12 pm

Hi,

Returning code of -5 (ESPCONN_INPROGRESS) means that there are still TCP clients connected to TCP server.

You could call espconn_disconnect to disconnect the clients first, then call espconn_delete to delete the server.

Thanks for your interest in ESP8266 !

User avatar
kolban
Posts: 131
Joined: Tue Jun 16, 2015 1:09 pm
Location: Fort Worth, Texas, USA

Re: How to correctly stop a TCP server from listening?

Postby kolban » Fri Oct 30, 2015 12:35 pm

Thank you for the response. This opens up a whole new set of considerations. I think you are telling me that if I configure an ESP8266 to listen on a particular port for client connections then once client connections start to arrive, I can't simply "close" the server listener without first closing all the client connections. This is an important semantic that I don't think I ever saw documented or discussed.

It now begs some new questions:

1) For a given socket listener, is there a way to determine which client connections are "associated" with that listener? It seems in order to close the socket listener, I need to close clients that were created when they connected to that server port ... am I supposed to track those myself?

2) What if I wish to suspend the arrival of new server connection requests while I process the clients I already have? I had assumed that I could "stop" a TCP server for listening when I had accumulated my limit of client connections. Since I don't seem to be able to do this without first closing the previous client connections that I am not yet finished with, I appear to be at an impass.

ESP_Faye
Posts: 1646
Joined: Mon Oct 27, 2014 11:08 am

Re: How to correctly stop a TCP server from listening?

Postby ESP_Faye » Mon Nov 02, 2015 11:16 am

Hi,

Thanks for your valuable advice ! We will add it in the documentation.

1. espconn_get_connection_info can get the information of connections, here is an example in tcp_server_mutli_sent http://bbs.espressif.com/viewtopic.php?f=31&t=763.

2. espconn_tcp_set_max_con_allow can set the maximum number of TCP clients that allowed to connect to the server.

Thanks for your interest in ESP8266 !

Who is online

Users browsing this forum: No registered users and 27 guests