Problem 1)server.available() works even without server.begin(). if available() doesn't find a listening socket, it calls begin to add it. This should not happen.
Problem 2) server's operator bool returns false if a client already connected to the listening socket. When a client connects, the socket state changes to established. The next call to available() adds a new listening socket, but until then if (server) is false so it is not possible to use op bool to detect if the server is started. It would be useful in a sketch and for Problem 1.
The documentation for operator bool says "It can also tell you when no more sockets are available to listen for more clients, because the maximum number have connected.". This is a bad attempt to make a special case of the Problem 2 a feature. Returning false from op bool if there is no more free position to add a new listening socket is not useful. available still works for connected clients and it will add a listening socket as soon as there is a free position.
One solution would be to add a field bool started, set it to true in begin and use it in op bool (and set it to false in end()PR).
Other solution would be in op bool to return true if (server_port[i] == _port) without checking the status. This will work because after begin there will always be at least one record with _port. Method available() removes disconnected clients, but always adds a new listening socket if there isn't one. This version of op bool then can be used at the beginning of available() to check if server was started with begin(). I prefer this version and I will try to a PR.
Problem 1)
server.available()
works even withoutserver.begin()
. ifavailable()
doesn't find a listening socket, it callsbegin
to add it. This should not happen.Problem 2) server's operator bool returns false if a client already connected to the listening socket. When a client connects, the socket state changes to established. The next call to
available()
adds a new listening socket, but until thenif (server)
is false so it is not possible to use op bool to detect if the server is started. It would be useful in a sketch and for Problem 1.The documentation for
operator bool
says "It can also tell you when no more sockets are available to listen for more clients, because the maximum number have connected.". This is a bad attempt to make a special case of the Problem 2 a feature. Returning false from op bool if there is no more free position to add a new listening socket is not useful.available
still works for connected clients and it will add a listening socket as soon as there is a free position.One solution would be to add a field
bool started
, set it to true in begin and use it in op bool (and set it to false inend()
PR).Other solution would be in op bool to return true if
(server_port[i] == _port)
without checking the status. This will work because afterbegin
there will always be at least one record with _port. Methodavailable()
removes disconnected clients, but always adds a new listening socket if there isn't one. This version of op bool then can be used at the beginning of available() to check if server was started withbegin()
. I prefer this version and I will try to a PR.