0
\$\begingroup\$

I'm trying to implement Kryonet in my game and i'm facing some trouble. Using the source from the code i develop my server like that :

try {
        this.server = new Server();
        ServerManager.registerClasses(this.server);
        this.server.addListener(new Listener() {

            public void received(Connection connection, Object object) {
                if(object instanceof PositionMessage) {
                    System.out.println("[SERVER] Message from ID : " + connection.getID() + " - " + ((PositionMessage)object).message);
                }
            }

        });
    this.server.bind(port);
    this.server.start();
    System.out.println("Server started on port " + port);
    ServerManager.isLaunched = true;
} catch (IOException e) {
    e.printStackTrace();
    ServerManager.isLaunched = false;
}

And that's how my client is coded :

this.client = new Client();
ServerManager.registerClasses(this.client);
client.start();
client.addListener(new Listener() {

    public void connected (Connection connection) {
        System.out.println("Send");
        PositionMessage pm = new PositionMessage();
        pm.message = "Hi, i'm connected";
        client.sendTCP(pm);
    }

    public void received (Connection connection, Object object) {

    }
});
new Thread("Connect") {
    public void run () {
        try {
            System.out.println("Connecting on " + ipServer + " au port " + port);
            client.connect(5000, ipServer, port);
            System.out.println("I am connected");
            isConnected = true;
        } catch (IOException ex) {
            ex.printStackTrace();
            System.out.println("The server doesn't exist");
            isConnected = false;
        }
    }
}.start();

It works perfectly, the client can connect and the message is send and receive by the server. But with that code, the problem is that the server is only local and can't be access from the outside (or it is and i'm just bad at it...) so to bypass that i'm not using only a port to create the server, but a InetSocketAdress passing my ip adress. So i replaced the bind in the server code by this :

this.server.bind(socketAdresse, socketAdresse);

With socketAdresse =

ServerManager.socketAdresse = new InetSocketAddress(ipServer, port);

The server is created (at least it seems created, i can't find a way to find if the server is launched, but i have no exception catched. Now for example, if i create a server with my local ip (the same way i was doing without the InetSocketAddress) and the port 2302, the client never connect to the server and i have literally no message. It just tell me that on the client side that the connection was succesfull but the server never receive any information, + when i do a check on the client if it connected, it says "false".

So my problem is that when i don't use a InetSocketAdress, the client can connect fine on my local ip, but i want to be able to host on the web so i use an inetsocketadress but the problem is that a client can't connect to my server and it doesn't give me an error.

Thanks for the help !

\$\endgroup\$
1
\$\begingroup\$

I am not 100% sure that this will solve your problem but it seems that you forgot to register the PositionMessage class. Be sure to register it in the same order on the client and the server and also, do not register the server and client classes.

| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

You don't have to provide the host address in your server code on Kryonet. Your computer network has its own IP address which is used by Kryonet when you start the server.

So on client code, when you start the client you put in the IP address (127.0.0.1) in case where both client and server are on same network.

In the case when server and client are on different networks, you need to get the external IP of the computer(network) on which your game server is hosted. Lets say for example your server is hosted on computer(network) with IP 'xxx.x.x.x'. In your client code you will have client.connect(xxx.x.x.x, PORT_SERVER_LISTENING_ON).

All you have to do on server is server.bind(TIMEOUT, TCP_PORT, UDP_PORT_IF_YOU_USING).

Also if you want to host the server on your computer, you need to do port forwarding.

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.