Is This the Right Way To Do This?

What I want to do: I want to have a main class that creates a test server and then connects a test client to this server.



Main.java

[java]public class Main extends SimpleApplication {



public static void main(String[] args) {

Main app = new Main();

app.start();

}



@Override

public void simpleInitApp() {

Box b = new Box(Vector3f.ZERO, 1, 1, 1);

Geometry geom = new Geometry(“Box”, b);



Material mat = new Material(assetManager, “Common/MatDefs/Misc/Unshaded.j3md”);

mat.setColor(“Color”, ColorRGBA.Blue);

geom.setMaterial(mat);



rootNode.attachChild(geom);



//create test server

System.out.println(“Creating test server…”);

MyGameServer testServer = new MyGameServer();

testServer.simpleInitApp();

//create test client

System.out.println(“Creating test client…”);

MyGameClient testClient = new MyGameClient();

testClient.simpleInitApp();

}



@Override

public void simpleUpdate(float tpf) {

//TODO: add update code

}



@Override

public void simpleRender(RenderManager rm) {

//TODO: add render code

}

}[/java]



MyGameClient.java

[java]public class MyGameClient extends SimpleApplication {



public int hostPort = 6143;



public static void main (String[] args)

{

MyGameClient clientApp = new MyGameClient();

clientApp.start(JmeContext.Type.Display);



}



public void simpleInitApp()

{

try

{

Client thisClient = Network.connectToServer(“localhost”, hostPort);

thisClient.start();

}catch(IOException e)

{

System.out.println(“IOException e caught (in MyGameClient)!”);

}

}

}[/java]



MyGameServer.java

[java]public class MyGameServer extends SimpleApplication {

public static void main(String[] args)

{

MyGameServer serverApp = new MyGameServer();

serverApp.start(JmeContext.Type.Headless);

}



public void simpleInitApp()

{

//on initation create server

try{

Server gameServer = Network.createServer(6143);

gameServer.start();

}catch(IOException e){

System.out.println(“IOEXception e caught (in MyGameServer)!”);

}

}



}[/java]



Everything compiles and it ran fine, the client connected to the server. I was wondering how to “get the server going” and did I use ‘testServer.simpleInitApp()’ correctly or should I have done something different?



In the future I would like it so that a client is created upon the user running the main program (simpleInitApp), connects to the server, and that the server is already running (regardless of whether or not a user runs the main program). Any tips?



Sorry if I’m a bit of a noob to networking. Just trying to get something really basic running here.



Any help is greatly appreciated.

Is this just for testing? I’m trying to understand why you’d want your one application to run both the client and server otherwise.



I thought I’d understood it as just a test but then the “in the future” part confused me.

are you trying to make the users create their own server