I want to limit the number of clients that can connect to the server to 4. My question is in SFML as I can tell to the server that if have 4 clients, do not accept more connection requests and stop listening new connections? I try with listener.close() but then does not receive data from the existing 4 players.Normally if you stop listening on a socket, it should do just that, stop listening. It shouldn't disconnect all active connections that were created through that listener as well. If this is the case, can you provide an example that shows this? Minimal, self-contained and compileable after copying and pasting the code would be nice. This could be a bug in SFML. Indeed, there recently were some bugs in SFML networking code that were fixed ::).
A selector doesn't store its own copies of the sockets (socket classes are not copyable anyway), it simply keeps a reference to the original sockets that you pass to the "add" function. Therefore, you can't use the selector as a socket container, you must store them oustide and make sure that they are alive as long as they are used in the selector.When you close any socket, not only listeners, you should also remove them from the selector as well, if possible before you close them. The documentation text might lead people to think that as long as their sf::Socket object still exists, selecting on them is permitted no matter what state they are in. This is technically true, however, the behaviour of selecting on a closed socket is not standardized and can lead to anything happening depending on the implementation of the underlying API. As such I recommend just not trying to select on any closed sockets at all.