SpiderMonkey connection suddenly closes

I am working on a multiplayer game using SpiderMonkey and for some reason shortly after the first message is sent, the connection closes.

One message is sent soon after the connection is initialized (in simpleInitApp()), however once simpleUpdate() starts sending messages, the connections is closed.

Here is the primary class that handles network connections. Both of it’s println() statements get printed.

package src.john01dav.spleef.client.networking;

import com.jme3.network.Client;
import com.jme3.network.Network;

import src.john01dav.spleef.client.SpleefClient;
import src.john01dav.spleef.common.networking.MessageClientInformation;
import src.john01dav.spleef.common.networking.MessageRegistrar;

import javax.swing.*;
import java.io.IOException;

public class SpleefClientNetwork{
    private SpleefClient spleefClient;
    private Client networkClient;

    public SpleefClientNetwork(SpleefClient spleefClient){
        this.spleefClient = spleefClient;
    }

    public void start(){
        try{
            MessageRegistrar.registerMessages();

            networkClient = Network.connectToServer("127.0.0.1", 16384);
            networkClient.addMessageListener(new ClientMessageListener(spleefClient));
            networkClient.start();

            if(!networkClient.isConnected()){
                System.out.println("Client not started");
            }
        }catch(IOException e){
            e.printStackTrace();
            JOptionPane.showMessageDialog(null, "Failed to connect to server. See stdout for details.");
            spleefClient.stop();
        }

        sendClientInformation();
    }

    public void stop(){
        networkClient.close();
    }

    private void sendClientInformation(){
        MessageClientInformation message = new MessageClientInformation(spleefClient.getName());
        networkClient.send(message);
        System.out.println("Client information sent");
    }

    public Client getNetworkClient(){
        return networkClient;
    }

}

Additionally, according to the server the client connects then immediately disconnects.

Here is the client log:

Aug 06, 2015 5:47:34 PM com.jme3.system.JmeDesktopSystem initialize
INFO: Running on jMonkeyEngine 3.0.10
Aug 06, 2015 5:47:34 PM com.jme3.system.Natives extractNativeLibs
INFO: Extraction Directory: /home/david/IdeaProjects/Spleef
Aug 06, 2015 5:47:35 PM com.jme3.system.lwjgl.LwjglContext printContextInitInfo
INFO: Lwjgl 2.9.0 context running on thread LWJGL Renderer Thread
Aug 06, 2015 5:47:35 PM com.jme3.system.lwjgl.LwjglContext printContextInitInfo
INFO: Adapter: null
Aug 06, 2015 5:47:35 PM com.jme3.system.lwjgl.LwjglContext printContextInitInfo
INFO: Driver Version: null
Aug 06, 2    indent preformatted text by 4 spaces015 5:47:35 PM com.jme3.system.lwjgl.LwjglContext printContextInitInfo
INFO: Vendor: NVIDIA Corporation
Aug 06, 2015 5:47:35 PM com.jme3.system.lwjgl.LwjglContext printContextInitInfo
INFO: OpenGL Version: 4.5.0 NVIDIA 346.82
Aug 06, 2015 5:47:35 PM com.jme3.system.lwjgl.LwjglContext printContextInitInfo
INFO: Renderer: GeForce GTX 770/PCIe/SSE2
Aug 06, 2015 5:47:35 PM com.jme3.system.lwjgl.LwjglContext printContextInitInfo
INFO: GLSL Ver: 4.50 NVIDIA
Aug 06, 2015 5:47:35 PM com.jme3.audio.lwjgl.LwjglAudioRenderer initInThread
INFO: Audio Device: OpenAL Soft
Aug 06, 2015 5:47:35 PM com.jme3.audio.lwjgl.LwjglAudioRenderer initInThread
INFO: Audio Vendor: OpenAL Community
Aug 06, 2015 5:47:35 PM com.jme3.audio.lwjgl.LwjglAudioRenderer initInThread
INFO: Audio Renderer: OpenAL Soft
Aug 06, 2015 5:47:35 PM com.jme3.audio.lwjgl.LwjglAudioRenderer initInThread
INFO: Audio Version: 1.1 ALSOFT 1.15.1
Aug 06, 2015 5:47:35 PM com.jme3.audio.lwjgl.LwjglAudioRenderer initInThread
INFO: AudioRenderer supports 64 channels
Aug 06, 2015 5:47:35 PM com.jme3.audio.lwjgl.LwjglAudioRenderer initInThread
INFO: Audio effect extension version: 1.0
Aug 06, 2015 5:47:35 PM com.jme3.audio.lwjgl.LwjglAudioRenderer initInThread
INFO: Audio max auxilary sends: 4
Client not started
Client information sent
Aug 06, 2015 5:47:37 PM com.jme3.network.base.DefaultClient handleError
SEVERE: Termining connection due to unhandled error
com.jme3.network.kernel.ConnectorException: Connector closed.
    at com.jme3.network.base.ConnectorAdapter.run(ConnectorAdapter.java:161)

Aug 06, 2015 5:47:37 PM com.jme3.app.Application handleError
SEVERE: Uncaught exception thrown in Thread[LWJGL Renderer Thread,5,main]
java.lang.IllegalStateException: Client is not started.
    at com.jme3.network.base.DefaultClient.checkRunning(DefaultClient.java:118)
    at com.jme3.network.base.DefaultClient.send(DefaultClient.java:222)
    at com.jme3.network.base.DefaultClient.send(DefaultClient.java:209)
    at src.john01dav.spleef.client.PlayerState.sendUpdate(PlayerState.java:42)
    at src.john01dav.spleef.client.PlayerState.update(PlayerState.java:28)
    at com.jme3.app.state.AppStateManager.update(AppStateManager.java:287)
    at com.jme3.app.SimpleApplication.update(SimpleApplication.java:239)
    at com.jme3.system.lwjgl.LwjglAbstractDisplay.runLoop(LwjglAbstractDisplay.java:151)
    at com.jme3.system.lwjgl.LwjglDisplay.runLoop(LwjglDisplay.java:185)
    at com.jme3.system.lwjgl.LwjglAbstractDisplay.run(LwjglAbstractDisplay.java:228)
    at java.lang.Thread.run(Thread.java:745)


Exception: java.lang.IllegalStateException thrown from the UncaughtExceptionHandler in thread "LWJGL Renderer Thread"
AL lib: (EE) alc_cleanup: 1 device not closed

Seems like you never register a ClientStateListener so it would be impossible to tell why/when the client closes.

…could be an error on the server, could be an error on the client. Hard to say.

…could be that you try to send stuff before the client is started.

ClientStateListener is your friend because it will tell you when you are actually connected for real and can actually send stuff.

I have added a ClientStateListener.

Network Class:

package src.john01dav.spleef.client.networking;

import com.jme3.network.Client;
import com.jme3.network.ClientStateListener;
import com.jme3.network.Network;

import src.john01dav.spleef.client.SpleefClient;
import src.john01dav.spleef.common.networking.MessageClientInformation;
import src.john01dav.spleef.common.networking.MessageRegistrar;

import javax.swing.*;
import java.io.IOException;

public class SpleefClientNetwork{
    private SpleefClient spleefClient;
    private Client networkClient;

    public SpleefClientNetwork(SpleefClient spleefClient){
        this.spleefClient = spleefClient;
    }

    public void start(){
        try{
            MessageRegistrar.registerMessages();

            networkClient = Network.connectToServer("127.0.0.1", 58468);
            networkClient.addMessageListener(new ClientMessageListener(spleefClient));
            networkClient.addClientStateListener(new ClientStateListener() {
                @Override
                public void clientConnected(Client client){
                    System.out.println("Client Connected");
                }

                @Override
                public void clientDisconnected(Client client, DisconnectInfo disconnectInfo) {
                    disconnectInfo.error.printStackTrace();
                    System.out.println(disconnectInfo.reason);
                }
            });
            networkClient.start();

            if(!networkClient.isConnected()){
                System.out.println("Client not started");
            }
        }catch(IOException e){
            e.printStackTrace();
            JOptionPane.showMessageDialog(null, "Failed to connect to server. See stdout for details.");
            spleefClient.stop();
        }

        sendClientInformation();
    }

    public void stop(){
        networkClient.close();
    }

    private void sendClientInformation(){
        MessageClientInformation message = new MessageClientInformation(spleefClient.getName());
        networkClient.send(message);
        System.out.println("Client information sent");
    }

    public Client getNetworkClient(){
        return networkClient;
    }

}

MessageServerInformation (not sure if this is relevant):

package src.john01dav.spleef.common.networking;

import com.jme3.network.AbstractMessage;
import com.jme3.network.serializing.Serializable;

@Serializable
public class MessageServerInformation extends AbstractMessage{
    private long seed;

    @SuppressWarnings("unused") //for serialization
    public MessageServerInformation(){}

    public MessageServerInformation(long seed){
        this.seed = seed;
        setReliable(true);
    }

    public long getSeed(){
        return seed;
    }

}

Logs:

Aug 06, 2015 10:40:55 PM com.jme3.system.JmeDesktopSystem initialize
INFO: Running on jMonkeyEngine 3.0.10
Aug 06, 2015 10:40:55 PM com.jme3.system.Natives extractNativeLibs
INFO: Extraction Directory: /home/david/IdeaProjects/Spleef
Aug 06, 2015 10:40:56 PM com.jme3.system.lwjgl.LwjglContext printContextInitInfo
INFO: Lwjgl 2.9.0 context running on thread LWJGL Renderer Thread
Aug 06, 2015 10:40:56 PM com.jme3.system.lwjgl.LwjglContext printContextInitInfo
INFO: Adapter: null
Aug 06, 2015 10:40:56 PM com.jme3.system.lwjgl.LwjglContext printContextInitInfo
INFO: Driver Version: null
Aug 06, 2015 10:40:56 PM com.jme3.system.lwjgl.LwjglContext printContextInitInfo
INFO: Vendor: NVIDIA Corporation
Aug 06, 2015 10:40:56 PM com.jme3.system.lwjgl.LwjglContext printContextInitInfo
INFO: OpenGL Version: 4.5.0 NVIDIA 346.82
Aug 06, 2015 10:40:56 PM com.jme3.system.lwjgl.LwjglContext printContextInitInfo
INFO: Renderer: GeForce GTX 770/PCIe/SSE2
Aug 06, 2015 10:40:56 PM com.jme3.system.lwjgl.LwjglContext printContextInitInfo
INFO: GLSL Ver: 4.50 NVIDIA
Aug 06, 2015 10:40:56 PM com.jme3.audio.lwjgl.LwjglAudioRenderer initInThread
INFO: Audio Device: OpenAL Soft
Aug 06, 2015 10:40:56 PM com.jme3.audio.lwjgl.LwjglAudioRenderer initInThread
INFO: Audio Vendor: OpenAL Community
Aug 06, 2015 10:40:56 PM com.jme3.audio.lwjgl.LwjglAudioRenderer initInThread
INFO: Audio Renderer: OpenAL Soft
Aug 06, 2015 10:40:56 PM com.jme3.audio.lwjgl.LwjglAudioRenderer initInThread
INFO: Audio Version: 1.1 ALSOFT 1.15.1
Aug 06, 2015 10:40:56 PM com.jme3.audio.lwjgl.LwjglAudioRenderer initInThread
INFO: AudioRenderer supports 64 channels
Aug 06, 2015 10:40:56 PM com.jme3.audio.lwjgl.LwjglAudioRenderer initInThread
INFO: Audio effect extension version: 1.0
Aug 06, 2015 10:40:56 PM com.jme3.audio.lwjgl.LwjglAudioRenderer initInThread
INFO: Audio max auxilary sends: 4
Client not started
Client Connected
Client information sent
Connection Error
Aug 06, 2015 10:40:59 PM com.jme3.network.base.DefaultClient handleError
SEVERE: Termining connection due to unhandled error
com.jme3.network.kernel.ConnectorException: Connector closed.
    at com.jme3.network.base.ConnectorAdapter.run(ConnectorAdapter.java:161)

com.jme3.network.kernel.ConnectorException: Connector closed.
    at com.jme3.network.base.ConnectorAdapter.run(ConnectorAdapter.java:161)
Aug 06, 2015 10:41:06 PM com.jme3.app.Application handleError
SEVERE: Uncaught exception thrown in Thread[LWJGL Renderer Thread,5,main]
java.lang.IllegalStateException: Client is not started.
    at com.jme3.network.base.DefaultClient.checkRunning(DefaultClient.java:118)
    at com.jme3.network.base.DefaultClient.close(DefaultClient.java:251)
    at src.john01dav.spleef.client.networking.SpleefClientNetwork.stop(SpleefClientNetwork.java:55)
    at src.john01dav.spleef.client.SpleefClient.stop(SpleefClient.java:69)
    at com.jme3.app.SimpleApplication$AppActionListener.onAction(SimpleApplication.java:88)
    at com.jme3.input.InputManager.invokeActions(InputManager.java:169)
    at com.jme3.input.InputManager.onKeyEventQueued(InputManager.java:455)
    at com.jme3.input.InputManager.processQueue(InputManager.java:831)
    at com.jme3.input.InputManager.update(InputManager.java:883)
    at com.jme3.app.Application.update(Application.java:604)
    at com.jme3.app.SimpleApplication.update(SimpleApplication.java:231)
    at com.jme3.system.lwjgl.LwjglAbstractDisplay.runLoop(LwjglAbstractDisplay.java:151)
    at com.jme3.system.lwjgl.LwjglDisplay.runLoop(LwjglDisplay.java:185)
    at com.jme3.system.lwjgl.LwjglAbstractDisplay.run(LwjglAbstractDisplay.java:228)
    at java.lang.Thread.run(Thread.java:745)


Exception: java.lang.IllegalStateException thrown from the UncaughtExceptionHandler in thread "LWJGL Renderer Thread"
AL lib: (EE) alc_cleanup: 1 device not closed

Your stack trace indicates that you are closing the connection based on some input from the user… and that the client isn’t really started when this happens.

This happens when the client closes when it crashes.

public void stop(){
    spleefClientNetwork.stop();
    super.stop();
}

Mmm… yes, that error is not the real error.

You need to register an error listener to see what the real error is. I forgot it’s a separate listener when I suggested that you register a client state listener (which you should also have).