Kryonet - Local Connection works, External doesn't

Hey,

I recently started out with networking but I already ran into a brickwall, my knowledge of networking is very little so I hope the fix will be rather simple.

Basically, I’m using Kryonet and so far just used the sample code on the page, so Running a Server and Connecting a Client, altered it a little so I can increase a number on the server by pressing space on the client… which works BUT
only when I do it on the same computer with 127.0.0.1 OR I also tried it on two different laptops which are connected to the same Router and used the (I guess internal?) IP 10.0.0.1 … both worked

But when I tried to connect to the server with the IP I get from for example http://www.whatsmyip.org/ it doesn’t work, unable to connect.

Server
[spoiler]

public class KryoServer {

	public KryoServer() throws IOException{
		Server server = new Server();
		server.start();
		server.bind(54555, 54777);

		Kryo kryo = server.getKryo();
		kryo.register(SomeRequest.class);
		kryo.register(SomeResponse.class);
		
		server.addListener(new Listener() {
			   public void received (Connection connection, Object object) {
			      if (object instanceof SomeRequest) {
			    	  Main.test++;
			        /* SomeRequest request = (SomeRequest)object;
			         System.out.println(request.text);

			         SomeResponse response = new SomeResponse();
			         response.text = "Thanks!";
			         connection.sendTCP(response);	*/
			      }
			   }
			});
	}
}

[/spoiler]

Client
[spoiler]

public class KryoClient {
	
	Client client;
	String IP;
	
	public KryoClient(String IP) throws IOException{
		this.IP = IP;
		client = new Client();
		client.start();
		client.connect(5000, IP, 54555, 54777);

		Kryo kryo = client.getKryo();
		kryo.register(SomeRequest.class);
		kryo.register(SomeResponse.class);
		
		client.addListener(new Listener() {
			   public void received (Connection connection, Object object) {
			      if (object instanceof SomeResponse) {
			         SomeResponse response = (SomeResponse)object;
			         System.out.println(response.text);
			      }
			   }
			});
	}
	
	public void sendRequest(){
		SomeRequest request = new SomeRequest();
		client.sendTCP(request);
	}
}

[/spoiler]

Thanks

I think you just have to configure your router to forward the ports you’re using (54555, 54777) to the local IP on which your server is running ( 10.0.0.1 ) :wink:

Are you sure I need to forward my ports?

Prime example Minecraft, I click join, type in the server’s IP and bam I’m in. What advantage does Minecraft have? That they use a login-system and connect Clients and Servers via accounts? But why would that eliminate the need to forward the ports? I’m kind of confused… i dont think it’s the firewall, I disabled it on the laptop I launched the server ;D

Thanks, appreciate your advice

Port forwarding is necessary for people outside of your home’s network to access the server. Not the other way around.You don’t need to port forward your computer if you are connecting to someone else’s server.

Oh snap, I see. Will check tomorrow if that fixes the problem.

Thanks!

Look at Nat-punchtrough :wink:
It’s a way to make connection possible with server behind routers.
I’ve achieved that in C# but because sockets and IP protocol are the same in all languages I think you can do that in java.

I just discovered that my millions of collision-avoidance attempts never satisfyingly worked because I didn’t cast the integer values to float… I feel an inner, deep, deep, creeping rage but also relief that it seems to work now.

Not really ontopic but I just had to express my feelings in a textual way. Thank you.

I have another Kryonet question but I dont think it’s worth opening another thread.

I’m a little confused on how to design things.
Right now, if a client joins the server, instead of sending the client all the Players (the objects) I just send the players’ x and y coordinates and let the client create it’s own Player objects based on this information. Partially to send less data and partially because sending the whole Player object didn’t seem to work ;D

Is this a good way of doing it? Because now I added the ability that the Players can carry objects, if now a client joins the server it doesn’t get the information that one of the players is already carrying an object, since it’s building its own Players based on the x and y coordinations I send to it.
Now an obvious solution to that would be, that besides sending only the x and y coordinates I also send the information what object the player is carrying… but I can see this growing to more and more information and giving me a major headache

Feedback appreciated

I’m no expert in this area, but are you sending all of the X’s and Y’s separately? My thought would to make a “PlayerPacket” class that contains all of the data that you need to send and send that at once.

I send a float[][] array with all the x and y values, and the player’s ID. Now I also added a string for the type of object the player is carrying.

Now I only have one type of object the player could carry, but obviously that will change. So I will have to do quite a bunch of if-else checks to see what object the client has to create.
Oh well, this is pretty much my first game (that actually looks like a game) and I started with Multiplayer right away, I ecpect it to fail eventually :stuck_out_tongue:

So you need to open ports on the server app’s side.
What about android online gaming ? If I dont use Wifi its the ISP… will it just work ?

I would only send the positions out when they change (and the packet was sent successfully!). If the client is standing still, there’s no reason to constantly send the player coordinates.

You could try sending different types of packets with a small identifier at the start of the packet, so the server/clients know how to handle each identifier’s data while also cutting down on packet size.

PS, how are you liking kryonet?

I’ve given up on multiplayer for now, because I think I need to gather more experience till I can dive into that, given that it is a rather complex field.

But yes, Kryonet is awesome! I had no multiplayer experience whatsoever before I started using Kryonet, but got everything to work that I wanted! (at least locally it did)

I would recommend it anytime :point: