Clients are still being able to connect for a while after server is closed.
So, I have a reconnect function on my client, the moment client loses connection to the server, it attempts to connect again. However, I noticed that if I close the server using .close(), the client gets disconnected (therefore it attempts to connect again) and it can connect again, then it gets disconnected a second time and isn't able to connect again. My conclusion is that there is some time between the clients getting disconnected and the server getting closed. I would solve this by adding some kind of a check on a connection attempt, whether the server is closing.
Here is a log from the client with added commentary, to better understand what is going on:
[22:10:21] [18:24:38:464] [main] [Client/<init>:17] [INFO] Starting door sensor client on Windows 10
[22:10:21] [18:24:38:479] [main] [Client/<init>:19] [INFO] Initializing network system
[22:10:21] [18:24:38:485] [main] [ReconnectableClient/attemptConnect:72] [INFO] Connecting to 192.168.1.15
[22:10:21] [18:24:39:501] [main] [ReconnectableClient/beginConnect:56] [INFO] Successfully connected on 1 attempt //Client connects to the server
[22:10:21] [18:24:39:502] [main] [Client/<init>:23] [INFO] Network system initialized
[22:10:21] [18:24:39:503] [main] [Client/<init>:25] [INFO] Door sensor client started
[22:10:21] [18:25:00:678] [nioEventLoopGroup-2-1] [SessionListener/disconnected:27] [WARN] Got disconnected from the server, attempting reconnect; reason: Server closed //Server#close is called, the client gets disconnected
[22:10:21] [18:25:00:679] [nioEventLoopGroup-2-1] [ReconnectableClient/attemptConnect:72] [INFO] Connecting to 192.168.1.15 //Immediatelly attempts to reconnect
[22:10:21] [18:25:00:716] [nioEventLoopGroup-2-1] [ReconnectableClient/beginConnect:56] [INFO] Successfully connected on 1 attempt //Is able to connect again, even though the server is shutting down
[22:10:21] [18:25:00:769] [nioEventLoopGroup-3-1] [SessionListener/disconnected:27] [WARN] Got disconnected from the server, attempting reconnect; reason: Connection closed. //Gets disconnected the second time, unable to connect again
A code snippet would be welcome to debug/fix the issue.
Not sure whether a code snippet would be useful. I will try to explain it as best as I can.
The server is closed by calling Server#close. The moment SessionListener#disconnected event is fired on the client, the program immediately reconnects by closing the old Session and creating a new one. However, even though Server#close was called, it still accepts the new connection. Therefore it can connect again, but, after a few milliseconds it gets disconnected again and it isn't able to connect since then.
Tho if you want to check the code, you can find it here. Link to the repo
If you take a look at this method https://github.com/GeyserMC/PacketLib/blob/2a8d18a3f825ab4737ed442c57b30f7cbd473703/src/main/java/com/github/steveice10/packetlib/AbstractServer.java#L154 you can see, that the server disconnects all clients and after that, it closes the server. Therefore there is some space where clients can connect after Server#close was called.