[Answered] Maximum number of open sockets supported

User avatar
kolban
Posts: 1683
Joined: Mon Nov 16, 2015 4:43 pm
Location: Texas, USA

[Answered] Maximum number of open sockets supported

Postby kolban » Sun Oct 09, 2016 3:14 am

Looking through the "menuconfig" settings we find a property within the "lwIP" area called "Max number of open sockets" which has a default of 4 and according to its help information, a maximum of 16. I am guessing that this is a constraint on the number of concurrently open sockets. I find myself surprised by a default of 4 and a maximum of 16. That "feels" low to me. That said, I have no evidence or experience that it would be a problem. A couple of questions though:
  • Is this truly the maximum we will have in an ESP32 or is this just an "as of now" story?
  • Does a "server socket" count against this number? For example, if I create a listening socket, does that reduce my number
    of concurrently connected clients by 1?
  • What happens to a client connection if we are running a listener on ESP32 and the maximum is reached? Is it rejected or held pending an accept for a future connection?
Last edited by kolban on Sun Oct 09, 2016 5:28 am, edited 1 time in total.
Free book on ESP32 available here: https://leanpub.com/kolban-ESP32

ESP_Sprite
Posts: 9759
Joined: Thu Nov 26, 2015 4:08 am

Re: Maximum number of open sockets supported

Postby ESP_Sprite » Sun Oct 09, 2016 4:54 am

I can't comment on what exactly LWIP (which is the TCP/IP stack we currently use) does when it hits the socket limit (except for returning -1 on a socket() call). I do know that for the ESP8266, 4 also was the minimum, and I've not heard many complaints about that. I'm not sure why 16 is chosen as the maximum number, but given that every potential open socket uses a bit of memory and LWIP is not designed for many simultaneous connections, I think that a maximum of 16 is reasonable.

ESP_igrr
Posts: 2072
Joined: Tue Dec 01, 2015 8:37 am

Re: [Answered] Maximum number of open sockets supported

Postby ESP_igrr » Sun Oct 09, 2016 3:39 pm

I don't think menuconfig option for the number of open sockets is used anywhere. It was added as an example of potential options which can be configured using menuconfig. But it isn't used in lwiptopts header file. Setting the number of sockets by itself is not the whole story, one also needs to set the number of TCP PCBs and some other parameters.

SpenZerX
Posts: 16
Joined: Sun Dec 13, 2015 9:23 am

Re: [Answered] Maximum number of open sockets supported

Postby SpenZerX » Thu Nov 10, 2016 8:19 pm

Hi,

is the maximum number of open sockets equal to the size of the bitfield in struct fd_set ? Can i expect that select function on FD will work on current idf?

And what is the answer to the question? Can i tweak the number of open sockets by a define ?
Creator of Smart Connected Devices - for EcSUHA.de Project

phatpaul
Posts: 110
Joined: Fri Aug 24, 2018 1:14 pm

Re: [Answered] Maximum number of open sockets supported

Postby phatpaul » Fri Mar 08, 2019 4:03 pm

I think the previous answer that "a maximum of 16 is reasonable" is outdated now.

I'm using ESP-WROVER with 4M of RAM. That should be plenty ram to support > 16 connections.

How can/should I increase LWIP_MAX_SOCKETS? Just hack the generated sdkconfig file?

User avatar
mbratch
Posts: 303
Joined: Fri Jun 11, 2021 1:51 pm

Re: [Answered] Maximum number of open sockets supported

Postby mbratch » Fri Aug 05, 2022 11:58 pm

Just opening a web page on the server on ESP32 with one browser opens 4 sockets. So 16 sockets max doesn't seem like such a large number really. Additionally, when I close the browser window, it only closes one of the sockets, leaving 3 open. So with just a couple of closings and re-openings, the maximum is gobbled up and I start getting errors on the server that there aren't enough sockets available.

kevinevans
Posts: 7
Joined: Thu Aug 25, 2022 10:34 pm

Re: [Answered] Maximum number of open sockets supported

Postby kevinevans » Tue Jul 25, 2023 5:26 pm

Has there been any updates to this? In the HTTP web server, the maximum of 13 open sockets isn't enough when you're potentially dealing with both websockets and regular HTTP requests.

phatpaul
Posts: 110
Joined: Fri Aug 24, 2018 1:14 pm

Re: [Answered] Maximum number of open sockets supported

Postby phatpaul » Thu Jul 27, 2023 2:33 pm

The way I've dealt with this limitation is to set the HTTP header "Connection: close\r\n". That tells the client to close every connection as soon as it is finished being used.
This has allowed me to serve static content and some APIs to several browsers and keep several websockets alive at once. I've seen no noticeable performance impact to closing unused connections (not sure if this would be true with SSL/HTTPS?).

On libesphttpd you can enable that feature by setting this in menuconfig: ESPHTTPD_SINGLE_REQUEST

Who is online

Users browsing this forum: alifepretend and 143 guests