Problem sending messages from client to server and Vice versa

Hi all. I have been trying to send messages from server to client and vice-versa, but had no luck.I saw the tutorial at this link



https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:advanced:networking



but noticed that some of the codes are different. This is what I did:



Server Side

[java]

package Main;



import com.jme3.app.SimpleApplication;

import com.jme3.network.connection.Client;

import com.jme3.network.connection.Server;

import com.jme3.network.events.ConnectionListener;

import com.jme3.network.events.MessageListener;

import com.jme3.network.message.Message;

import com.jme3.network.serializing.Serializable;

import com.jme3.network.serializing.Serializer;

import com.jme3.system.JmeContext;







public class ServerMain2 extends SimpleApplication{



Server gameServer;



public static void main(String [] args){

ServerMain2 app = new ServerMain2();

app.start(JmeContext.Type.Headless);

}



@Override

public void simpleInitApp() {

try{

gameServer = new Server(5110, 5111);

gameServer.setLabel(“GAME SERVER”);

gameServer.start();

Serializer.registerClass(HelloMessage.class);

gameServer.addMessageListener(new ServerListener(), HelloMessage.class);

Message message = new HelloMessage(“Server Online!”);

gameServer.broadcast(message);



}catch(Exception e){

System.out.println(e);

}

}



@Override

public void simpleUpdate(float tpf){

//System.out.println(gameServer.getConnectors().toString());

}



@Serializable

public class HelloMessage extends Message {

private String hello; // message data

public HelloMessage() {} // empty constructor

public HelloMessage(String s) { hello = s; } // custom constructor



public String getSomething(){

return “got something”;

}

}



}

[/java]



Server Side Message Listener:



[java]

package Main;



import Main.ServerMain2.HelloMessage;

import com.jme3.network.connection.Client;

import com.jme3.network.connection.Server;

import com.jme3.network.events.MessageListener;

import com.jme3.network.message.Message;



public class ServerListener implements MessageListener {

public void messageReceived(Server source, Message message) {

if (message instanceof HelloMessage) {

// do something with the message

HelloMessage helloMessage = (HelloMessage) message;

System.out.println(“Server received '” +helloMessage.getSomething() +"’ from client #"+source.getServerID());

} // else…

}



public void messageReceived(Message message) {



}



public void messageSent(Message message) {



}



public void objectReceived(Object object) {



}



public void objectSent(Object object) {



}

}

[/java]





Client Side:

[java]

package Main;



import com.jme3.app.SimpleApplication;

import com.jme3.network.connection.Client;

import com.jme3.network.events.MessageListener;

import com.jme3.network.message.Message;

import com.jme3.network.serializing.Serializable;

import com.jme3.network.serializing.Serializer;

import com.jme3.system.JmeContext;





public class ClientMain2 extends SimpleApplication{



Client client;

public static void main(String [] args){

ClientMain2 app = new ClientMain2();

app.start(JmeContext.Type.Headless);

}



@Override

public void simpleInitApp() {

try{

client = new Client(“localhost”, 5110, 5111);

client.start();

Serializer.registerClass(HelloMessage.class);

client.addMessageListener(new ClientListener(), HelloMessage.class);

Message message = new HelloMessage(“The CLIENT IS HERE!!!”);

client.send(message);

}catch(Exception e){

System.out.println(e);

}

}



@Serializable

public class HelloMessage extends Message {

private String hello; // message data

public HelloMessage() {} // empty constructor

public HelloMessage(String s) { hello = s; } // custom constructor

public String getSomething(){

return hello;

}

}



}

[/java]



Client Side Message Listener:



[java]

package Main;



import Main.ClientMain2.HelloMessage;

import com.jme3.network.connection.Client;

import com.jme3.network.events.MessageListener;

import com.jme3.network.message.Message;



public class ClientListener implements MessageListener{

public void messageReceived(Client source, Message message) {

if (message instanceof HelloMessage) {

// do something with the message

HelloMessage helloMessage = (HelloMessage) message;

System.out.println(“Client #”+source.getClientID()+" received: ‘"+helloMessage.getSomething()+"’");

} // else…

}



public void messageReceived(Message message) {



}



public void messageSent(Message message) {



}



public void objectReceived(Object object) {



}



public void objectSent(Object object) {



}

}

[/java]

First and most importantly:

com.jme3.network.connection.Client



You are using a very old and very broken version of SpiderMonkey. What version of JME are you running?



I completely rewrote SpiderMonkey in April because the old version had systemic bugs that could never be fixed. The new version is a lot easier to use, anyway.



Now…



The TestChatClient and TestChatServer are good examples to see message sending and receiving in action.



I can tell just by looking at your code that you have some problems and should have been getting errors in the logs… though with old crufty SpiderMonkey maybe it wasn’t kicking out errors.



The first major problem is that you use non-static inner classes for your messages. This can’t work because there is no way to instantiate these without a reference to the outer class instance. For example, to create a new HelloMessage the code would need to create it as a sub-object of your server instance. This can’t work.



Make them static or make them separate classes.



Second, and this might work sometimes and fail randomly at other times… you should add all of your listeners, register your messages, etc. to your client and server instances BEFORE you start() them. As soon as you start() the server it can start receiving connections and might get some before you’ve registered your listeners or setup your serialization. Likewise, as soon as the client is start()ed it contacts the server and messages pass back and forth.

I’m using Version: jMonkeyPlatform Alpha-4

You should definitely upgrade to beta. Alpha 4 is more than 6 months old now… ancient in JME terms.

thanks will do that now

Once you have updated to beta you will want to also enable updates to the latest stable version. When JME went beta, there are now stable bug-fixes that go out once a week or so… safer than the old nightlies. And there was one timer-related bug fixed right after beta was released that you will probably want.



…unrelated to SpiderMonkey, though.

A million thanks pspeed! yes u were right. Now it works smoothly as expected. Now it can send mesages from client to server and the other way round. I will do all those updates now.



K Out!