javalibgdxkryonet

Separate main() methods to run DesktopLauncher.main() and Server using Libgdx and Kryonet


I want my GameServer to run separately from the game itself. So, players(clients) can join into one static GameServer and I can handle them together and see how many clients are connected, currently.

But the problem is, I can only run only one of these classes(GameServer.main() and DesktopLauncher.main()) at the same time. GameServer must be running always at the background if I'm not wrong, right ? Yet, I can't run the game itself without stopping the GameServer. (It stucks saying Executing task 'DesktopLauncher.main()'...) I have some pictures to realize what's going on and what project structure looks like :

Pic 1 , Pic 2 , Pic 3

Here is my project structure :

core -java --com.mygdx.game ---Multiplayer ----Packets -----GameClient.java -----GameServer.java -----GameClientListener.java -----GameServerListener.java ---screens ---utils ---Application.java

GameServer.class

package com.mygdx.game.Multiplayer;


imports..

public class GameServer {

public int TCP_PORT,UDP_PORT;
public Server server;
public GameServerListener listener;

public static int totalClients = 0;

public GameServer() {

    TCP_PORT = UDP_PORT = xxxx;
    server = new Server(TCP_PORT,UDP_PORT);
    listener = new GameServerListener(this);

    startServer();
}


public void startServer() {

    server.addListener(listener);

    try {
        server.bind(TCP_PORT,UDP_PORT);
        //server.bind(TCP_PORT);

    } catch (IOException e) {
        e.printStackTrace();
    }
    registerPackets();
    server.start();

}

private void registerPackets() {
    server.getKryo().register(LoginRequest.class);
    server.getKryo().register(LoginResponse.class);
    server.getKryo().register(ChoiceRequest.class);
    server.getKryo().register(ChoiceRespond.class);

}

public static void main(String[] args) {
    new GameServer();
}

}

Thanks for any help.


Solution

  • Had the same issue. Downgrading to Android Studio 3.4 worked for me.

    Check: https://www.reddit.com/r/libgdx/comments/fxrlsm/unable_to_run_two_or_more_application_in_parallel/