Why does an outward-facing “server” socket close?
It is bound to a port and should continue listening indefinitely.
What are the reasons for NSAPI_ERROR_NO_SOCKET?
Is there an underlying problem?
Should I just reconnect?
server_socket.open(iface);
server_socket.bind(port);
server_socket.listen(5);
server_ socket.set_blocking(true);
nsapi_error_t accept_res;
while (is_connected) {
accept_res = NSAPI_ERROR_BUSY; // Assume failure
TCPSocket *incoming_socket = server_socket.accept(&accept_res); // Block until incoming connection
switch(accept_res) {
case NSAPI_ERROR_NO_SOCKET:
// Why did my server_socket close ???
break;
case NSAPI_ERROR_OK:
// I was expecting to keep accepting connections
break;
}
}
How can I manage the sockets so that I never reach the max value defined in mbed_app.json ?
Theoretically a malicious user can keep opening connections until my server crashes.
Where can I find the counter in LWIP/Mbed that this parameter relates to ?
Do you know where I can find a good doc of all options available in mbed_app.json ?
Thanks !
OK I think I figured out a hacky way to make it work. You’ll have to be using MBed CLI or studio though (or mbed-cmake), not the online compiler.
First, go to mbed-os/features/lwipstack/LWIPStack.h and do a find and replace for “private:” with “public:”. Now that the data is accessible, we can do it this way:
#include <LWIPStack.h>
size_t getNumFreeSockets()
{
LWIP & netStack = reinterpret_cast<LWIP &>(OnboardNetworkStack::get_default_instance());
netStack.adaptation.lock();
size_t freeCount = 0;
for (int i = 0; i < MEMP_NUM_NETCONN; i++) {
if (!netStack.arena[i].in_use) {
++freeCount;
}
}
netStack.adaptation.unlock();
return freeCount;
}
These options are documented on this page and its subpages (though I agree these are difficult to find).