سؤال

This seems to be a popular problem, but I'm still having trouble finding a solution even after spending a lot of time troubleshooting. I'm hoping there's an updated solution.

I'm setting up a simple Server and Client with the KryoNet Java networking library. My problem is that my client disconnects immediately after connecting to the server.

Here is my code:

Server

public class TheServer extends Listener {

    static Server server;
    static final int PORT = 8215;

    public static void main(String[] args) throws IOException {
        server = new Server();
        server.start();
        server.bind(PORT);
        server.addListener(new TheServer());
        System.out.println("server started on " + PORT);
    }

    public void connected(Connection c) {
        System.out.println("connected: " + c.getID());
    }

    public void disconnected(Connection c) {
        System.out.println("disconnected: " + c.getID());
    }

}

Client

public class TheClient extends Listener {

    static Client client;
    static final String IP = "localhost";
    static final int PORT = 8215;

    public static void main(String[] args) throws IOException {
        client = new Client();
        client.start();
        client.connect(5000, IP, PORT);
        client.addListener(new TheClient());
        //client.setKeepAliveTCP(2000);
    }

}

After running TheServer and then TheClient, my console prints:

server started on 8215
connected: 1
disconnected: 1

Note that the time between the connection and disconnection is almost immediate, certainly less than the connection timeout time I set. Also note that I commented out the setKeepAliveTCP() method because while I do not think it is necessary, I inserted it to see if it would work.

هل كانت مفيدة؟

المحلول

After some more digging around, I found that starting the client with:

new Thread(client).start()

instead of

client.start()

fixes the problem.

"Starting with r122, client update threads were made into daemon threads, causing the child processes to close as soon as they finish initializing."

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top