Client is not started

I follow all the codes of the NetworkTutorial on youtube
When I tried to compile it, it just return an error,saying that “client is not started”.
This is my code, can anyone help me?
[java]
package mygame;

import com.jme3.app.SimpleApplication;
import com.jme3.network.Network;
import com.jme3.network.Server;
import com.jme3.scene.Geometry;
import com.jme3.system.JmeContext;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import mygame.UtNetworking.NetworkMessage;

public class ServerMain extends SimpleApplication {

private Server server;

public static void main(String[] args) {
    UtNetworking.initializeSerializable();
    ServerMain app = new ServerMain();
    app.start(JmeContext.Type.Headless); // headless type for servers!
}

@Override
public void simpleInitApp() {
    try {
        server = Network.createServer(UtNetworking.port);
        server.start();
    } catch (IOException ex) {
        Logger.getLogger(ServerMain.class.getName()).log(Level.SEVERE, null, ex);
    }
    Geometry geom = new CreateGeoms(this).createBox();
    rootNode.attachChild(geom);
}

@Override
public void simpleUpdate(float tpf) {
    //TODO: add update code
    server.broadcast(new NetworkMessage("Hello World!!!"+tpf));
}

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

}
[/java]

[java]
/*

  • To change this template, choose Tools | Templates
  • and open the template in the editor.
    */
    package mygame;

import com.jme3.app.SimpleApplication;
import com.jme3.network.Client;
import com.jme3.network.Message;
import com.jme3.network.MessageListener;
import com.jme3.network.Network;
import com.jme3.scene.Geometry;
import java.io.IOException;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.logging.Level;
import java.util.logging.Logger;
import mygame.UtNetworking.NetworkMessage;

/**
*

  • @author Y
    */
    public class ClientMain extends SimpleApplication {

    private Client client;
    private ConcurrentLinkedQueue<String> messageQueue;

    public static void main(String[] args) {
    ClientMain app = new ClientMain();
    app.start(); // standard display type
    }

    @Override
    public void simpleInitApp() {
    try {
    client = Network.connectToServer(“127.0.0.1”,UtNetworking.port);
    client.start();
    } catch (IOException ex) {
    Logger.getLogger(ServerMain.class.getName()).log(Level.SEVERE, null, ex);
    }
    Geometry geom = new CreateGeoms(this).createBox();
    rootNode.attachChild(geom);
    messageQueue = new ConcurrentLinkedQueue<String>();
    client.addMessageListener(new NetworkMessageListener());
    }

    @Override
    public void simpleUpdate(float tpf) {
    //TODO: add update code
    String message = messageQueue.poll();
    if(message!=null){
    fpsText.setText(message);}
    else{
    fpsText.setText(“NO MESSAGE!!!”);
    }
    }
    private class NetworkMessageListener implements MessageListener<Client>{
    public void messageReceived(Client source, Message m){
    if(m instanceof NetworkMessage){
    NetworkMessage message = (NetworkMessage) m;
    messageQueue.add(message.getMessage());
    }
    }
    }

    @Override
    public void destroy() {
    client.close();
    super.destroy();
    }
    }
    [/java]
    [java]
    /*

  • To change this template, choose Tools | Templates

  • and open the template in the editor.
    */
    package mygame;

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

/**
*

  • @author Y
    */
    public class UtNetworking {
    public static final int port = 6000;
    public static void initializeSerializable(){
    Serializer.registerClass(NetworkMessage.class);
    }
    @Serializable
    public static class NetworkMessage extends AbstractMessage{
    private String message;
    public NetworkMessage(){

     }
     public NetworkMessage(String message){
         this.message = message;
     }
     public String getMessage(){
         return message;
     }
    

    }
    }
    [/java]
    [java]
    /*

  • To change this template, choose Tools | Templates

  • and open the template in the editor.
    */
    package mygame;

import com.jme3.app.SimpleApplication;
import com.jme3.material.Material;
import com.jme3.math.Vector3f;
import com.jme3.scene.Geometry;
import com.jme3.scene.shape.Box;

/**
*

  • @author Y
    */
    public class CreateGeoms {
    private Material m;
    private Box b;
    public CreateGeoms(SimpleApplication simpleApplication){
    m = new Material(simpleApplication.getAssetManager(), “Common/MatDefs/Misc/Unshaded.j3md”);
    b = new Box(Vector3f.ZERO, new Vector3f(1,1,1));
    }
    public Geometry createBox(){
    Geometry box = new Geometry(“box”,b);
    box.setMaterial(m);
    return box;

    }

}
[/java]

There would have been some more information in the error… perhaps a stack trace?

As an aside, your simpleInitApp logic is kind of off. You try to connect to the server but then if an exception is thrown you just log it and continue. Perhaps there is an earlier error in the log about not being able to connect or something. It would be better to just rethrow the exception (wrapped in a RuntimeException or something) or at least bail out in some way.

The other thing is that you register your listener after starting. In theory, you might receive and drop a bunch of messages between the time you start() and the time the listener is added. It’s better to add the listener before starting.

The test project has a TestChatClient and a TestChatServer which is a fully working client and server for sending TCP and UDP chat messages. It’s probably the best example of the basics.

1 Like
<cite>@pspeed said:</cite> There would have been some more information in the error... perhaps a stack trace?

As an aside, your simpleInitApp logic is kind of off. You try to connect to the server but then if an exception is thrown you just log it and continue. Perhaps there is an earlier error in the log about not being able to connect or something. It would be better to just rethrow the exception (wrapped in a RuntimeException or something) or at least bail out in some way.

The other thing is that you register your listener after starting. In theory, you might receive and drop a bunch of messages between the time you start() and the time the listener is added. It’s better to add the listener before starting.

The test project has a TestChatClient and a TestChatServer which is a fully working client and server for sending TCP and UDP chat messages. It’s probably the best example of the basics.

Thank you very much. I found TestChatClient and TestChatServer are really helpful