Handle network errors

Hi! I am learning jme network and need help with handle error situations.
I have AppState to encapsulate my project basic server.


@Override
    public void initialize(AppStateManager stateManager, Application application) {
        super.initialize(stateManager, application);
        this.app = (Thrust) application;

        try {
            server = Network.createServer(port);
            Serializer.registerClasses(RequestMessage.class, SyncSpatialsMessage.class, SyncSpatialsMessage.SyncData.class,
                    CommandMessage.class, ChatMessage.class, PlayerAuthMessage.class, PlayerEnterMessage.class,
                    PlayerExitMessage.class, ExplodeMessage.class, SyncSpaceshipsMessage.class, SyncSpaceshipsMessage.Data.class,
                    AuthResponce.class);
            logger.info("Serializer ready");
            server.start();
            logger.info("Server started");
        } catch (Exception e) {
            logger.log(Level.SEVERE, "", e);
            app.stop();
        }
    }

    @Override
    public void cleanup() {
        for (HostedConnection connection : server.getConnections())
            connection.close("Server is down");
        if (server.isRunning()) {
            server.close();
            logger.info("Server stopped");
        }
        Serializer.setReadOnly(false);
        super.cleanup();
    }

So, basically i have many listeners except ErrorListener to send some data, for example i sending spatials coordinates from server to client pretty often. All going ok except trouble situation. For example i emulate server crashing by just closing server. Clean code execting ok. Error on client occured
[SEVERE] 2017.01.27 16:27:16: Termining connection due to unhandled error
com.jme3.network.kernel.ConnectorException: Error reading from connection to:/127.0.0.1:9000
and it is expected too on client. I can show message about it and other stuff. But from now i can not start server anymore! When i detach server state and attach new i have this

[SEVERE] 2017.01.27 16:29:02: 
com.jme3.network.kernel.KernelException: Error hosting:0.0.0.0/0.0.0.0:9000

and my netstat shows me that Server is not really down, it continues to listen port! How is this possible when i did server.close()?

After fully restart server application(not just ServerAppState) everything starts going ok. Can someone experienced help me to understand what i am doing wrong while handling server? Is it normal situation when server fails to reconnect after network error and being closed&

I guess, I once had the same issue. When I remember correctly you should not call that what I have quoted above in cleanup, but rather:

@Override
public void cleanup() {
   server.close();
   super.cleanup();
}

Replace this cleanup method with yours and have a look if it does work.

1 Like

Do you actually see that log message?

You can look at the sim-eth examples. While they do much more than you want that also let you stop hosting… then you can check to see if the ports are still open or not then look at the code I used to stop the server. There was a fix I needed to start hosting again because of Serailizer issues and I’m sure I’ve done that in sim-eth-es because I just tested it. I don’t remember if I did it in sim-eth-basic yet or not. (Actually sim-eth-basic seems broken for me right now.)

Here’s the working one:

1 Like

Thank u for fast reply! Looks like situation going better then before, i mean less network errors occurs, but not enought to say all is ok. Anyway this simple solution helped!

1 Like

Cool project, many things to learn, thx for link. To be honest basic network code looks like mine. When i read it sometimes i start to forgot it is not mine :), so acquainted code style u using. Except one huge difference, u r using network service much. I am not cause of i don’t know how. No documents about it i found.
If i will not solve remaining network problems i will return to this topic and hope on u guys :slight_smile:

The service stuff is just a way of plugging code in the server that will follow the life cycle of the connection. Like, it won’t be started until the connection is up. It will be stopped when the connection goes down, etc…

It’s to help keep code modular.