Simple TCP gameplay

Hello guys,



Thanks for a great SDK! I am working on a voxel kind of project and my goal is to make it Multiplayer as that plays a big part in my game.



So I tried creating a server using SpiderMonkey, I guess, with the following code;



[java]Server myServer = Network.createServer(6143);

myServer.start();[/java]



But it would not compile, telling me about surrounding it with Try and Catch statements. So I did. Ended up with;



[java] try {

Server myServer = Network.createServer(6143);

myServer.start();

} catch (IOException ex) {

Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);

}

[/java]



This compiled, so I tried creating a client, it it was the same story. I had to surround with Try and Catch like this;



[java]try {

Client myClient = Network.connectToServer("90.129.144.158", 6143);

myClient.start();

} catch (IOException ex) {

Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);

}[/java]



After this I try running it and the console told me the connection was timed out. Is that because I am trying to join a TCP game where I am the host?

If so, will it work if someone else plays the game, or did I miss a thing or two?



The log from the console told me this;





INFO: Kernel started for connection:0.0.0.0/0.0.0.0:6143.

2011-okt-30 14:06:13 mygame.Main simpleInitApp

SERIOUS: null

java.net.ConnectException: Connection timed out: connect

at java.net.PlainSocketImpl.socketConnect(Native Method)

at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)

at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)

at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)

at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)

at java.net.Socket.connect(Socket.java:525)

at java.net.Socket.connect(Socket.java:475)

at java.net.Socket.(Socket.java:372)

at java.net.Socket.(Socket.java:215)

at com.jme3.network.kernel.tcp.SocketConnector.(SocketConnector.java:62)

at com.jme3.network.Network.connectToServer(Network.java:166)

at com.jme3.network.Network.connectToServer(Network.java:123)

at mygame.Main.simpleInitApp(Main.java:62)

at com.jme3.app.SimpleApplication.initialize(SimpleApplication.java:230)

at com.jme3.system.lwjgl.LwjglAbstractDisplay.initInThread(LwjglAbstractDisplay.java:129)

at com.jme3.system.lwjgl.LwjglAbstractDisplay.run(LwjglAbstractDisplay.java:205)

at java.lang.Thread.run(Thread.java:619)




What is up with the IP set to 0.0.0.0? Any help is appreciated.


  • Lars.

You’re connecting to your external IP address which you likely aren’t getting access to because the port is blocked by your router. Try first connecting to “127.0.0.1” (this is your computer)… Presuming that succeeds, you will want to look up the instructions on port forwarding for your particular router.

I am using mobile internet, the one with a USB. So what is the setup of the IP on a TCP game? I guess I cannot use 127.0.0.1 as it is my localhost.

My guess is that you aren’t going to be able to unblock ports using one of those mobile USB sticks. If you’re connecting a client to server on the same computer, 127.0.0.1 is fine… If you were behind a router then you’d have DHCP assigned addresses for each device but your possibilities are really limited when you’re connected directly to the ISP.

Think I got LAN working now, since it establishes a connection. But the other player in the LAN wont show up. Ideas?

Unless I’m missing something your test case only shows a single client connecting to your server…



From a second process (i.e: another java application), run another client:



[java]

public static void main(String[] args){

try {

Client myClient = Network.connectToServer(“TheIPAddressYouUsed”, 6143);

myClient.start();

} catch (IOException ex) {

Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);

}

}

[/java]

Yeah, I might try that. I added a custom cube to the camera making it follow with target on the cube. So you control the cube and if I run from a second process as you say, the cube will be visible? I set up an ad-hoc network on my computer and have the game running on both computers in the network. My guess is that it connected successfully but we couldn’t see eachothers cubes that you control.



Also, a bit off-topic. Can I log everything in the console to a text-file for debugging purposes? That case I can see that the connection was established between the computers.

synok said:
Yeah, I might try that. I added a custom cube to the camera making it follow with target on the cube. So you control the cube and if I run from a second process as you say, the cube will be visible? I set up an ad-hoc network on my computer and have the game running on both computers in the network. My guess is that it connected successfully but we couldn't see eachothers cubes that you control.


Have you followed the networking tutorial?

synok said:
Also, a bit off-topic. Can I log everything in the console to a text-file for debugging purposes? That case I can see that the connection was established between the computers.


You want FileHandler.

Used like:

[java]myLogger.addHandler(myFileHandler);[/java]

Yep, I followed the tutorial as far as setting up a server and client goes. I presumed that was all I needed at the moment.



Edit: Also regarding the different processes. When I make the server and client versions I comment away the functions to start the server and compile, which would give me the client. When I want to compile the server version, I comment away the functions to start a client.

You can also look at the test chat server and test chat client in the JME tests.



It’s a good idea to have separate paths through your code for client and server rather than recompiling to get to each one… that could be fraught with peril.

I believe I tried anything now to get the other player into my game. Also I would be really happy if anyone could provide an example on a simple setup for a game server and client.

synok said:
I believe I tried anything now to get the other player into my game. Also I would be really happy if anyone could provide an example on a simple setup for a game server and client.


There is TestChatServer and TestChatClient that supports any number of clients. Setting up a game server is no different... just the messages you send are different.

Or for a more involved example you can look at MonkeyZone... but the others mentioned above are already in the SDK/JMP.

I found another library called JGN. I guess I can try that.

:roll: Its not like JGN does anything else than the networking in jme3 does. You will always have to understand whats happening and write the code for your game.

1 Like

Yeah, and I’m trying to understand how what I provided didn’t answer the question.

I think I have multiplayer working now, I just need to make the players visible. Everything connects as it should, so is there some kind of code to make each player visible?

may be you can try this code of mine

http://hub.jmonkeyengine.org/groups/spidermonkey/forum/topic/some-questions-about-spider-monkey/



at least it worked in past

synok said:
I think I have multiplayer working now, I just need to make the players visible. Everything connects as it should, so is there some kind of code to make each player visible?


What "visible" is would sort of be up to your game. SpiderMonkey, like JGN, is just the networking layer and it's still up to you to send the right messages and do something with those messages. JME doesn't yet provide any out of the box object sync system at least partially because it's not clear how to make something general that is usually very game specific. In fact, some games won't even use a sync system, for example.

How to proceed may depend on what kind of game you are making.

xieu90, thanks for the piece of code you put up. It took me around 10 minutes to set the classes right, but when I was done I started the server on my computer and the client on my computer aswell. I used my IPv4 IP, just to press the limits, and it worked out. It worked out good.



I thank you again and everyone who responded. Another quicky for xieu90, is it OK for me to use your project as template? I will of course modify it for my specific game needs later.

OK

use it as you want