Problems with smoothly closing the server

Hi guys,
this may be a stupid question but I don’t know how to solve this problem (yes, I read the networking tutorial):

When I close my server

server.close();
this.addToConsole("Server closed. You may close this window now.");

I get the following exception at the client side:

Termining connection due to unhandled error
com.jme3.network.kernel.ConnectorException: Error reading from connection to:localhost/127.0.0.1:6600
	at com.jme3.network.kernel.tcp.SocketConnector.read(SocketConnector.java:134)
	at com.jme3.network.base.ConnectorAdapter.run(ConnectorAdapter.java:158)
Caused by: java.net.SocketException: Connection reset
	at java.net.SocketInputStream.read(SocketInputStream.java:196)
	at java.net.SocketInputStream.read(SocketInputStream.java:122)
	at java.net.SocketInputStream.read(SocketInputStream.java:108)
	at com.jme3.network.kernel.tcp.SocketConnector.read(SocketConnector.java:120)
	... 1 more

I understand that the client throws an exception, I also tried working with ClientStateListener but I don’t really know how to use it or catch the exception otherwise…

I just want the client to go back into the main menu (or something like this) when the server closes instead of throwing this exception.
I would really much appreciate a little tip how to do this :wink:

Connection reset exception happens when the remote socket closed the connection. This is a natural behavior and will happen on client or on server.

Most game servers, when receive the server closing action, sends a close message to all clients. This avoid this kind of error and data loss, because clients will have time to close themselves and go to main menu before server close connection. Maybe you can do the same.

You should disconnect clients first and then close the server.

I do now disconnect all clients first like this:

 for(HostedConnection conn : gameAppState.getServer().getConnections()){
        conn.close("Server closing.");
    }

Now I only get the following message in the sdk:

Jul 12, 2015 2:31:33 PM com.jme3.network.base.DefaultClient dispatch
Schwerwiegend: Connection terminated, reason:Server closing..

I guess I have to live with that :stuck_out_tongue:

But this isn’t a error. It warns you connection was terminated and why…

Yep, I see that…

Thank you all :slight_smile:

Well, I can’t comment on why your client state listener didn’t work because I can’t see it… but to avoid this issue you need to register a client state listener that detects that the client connection has closed and then somehow let the rest of the application know.

The idea with sending a close message is good in general, but you have to catch such exceptions anyway

Imagine a Server Crash, it could raise this or a comparable exception, so you should also handle that.

Exactly, there are a variety of reasons that the connection will go down other than the server desiring it… so one must handle the client state changes anyway.