Evenilink
Evenilink

Reputation: 85

Can't connect Android to desktop server

I'm developing a game using libGdx and Kryonet and I'm currently working on the server <-> android connection.

First I run the server on a computer, and if I run two instances of the program on the same computer (so the IP will be the local IP: "127.0.0.1"), I don't get any problems, and the connection works fine.

But if instead of running the game on desktop, I run it on the android device, the connection doesn't happen. Both the devices are connected to the same Wi-fi network, so to discover the IP, what I do is type "ipconfig" on the console, and check the line that says: "IPv4 Adress" under "Wireles LAN adapter Wi-Fi" (I think it's this one).

I've added this 3 lines to the android manifest file (are they really needed?):

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

Here's the important error log:

enter image description here Do I need to do anything on the device some extra code for the android connection to establish? More permissions perhaps?

Here's the relevant code: MPServer:

public class MPServer {
Server server;
Array<PlayerInfo> playersInfo;
int numNewPlayers;

public MPServer() throws IOException {
    numNewPlayers = 0;
    playersInfo = new Array<PlayerInfo>();
    server = new Server();

    Network.registerPackets(server);
    addListeners();
    server.bind(Network.PORT);
    server.start();
}

private void addListeners() {
    server.addListener(new Listener() {
        @Override
        public void connected(Connection connection) {

        }

        @Override
        public void disconnected(Connection connection) {

        }

        @Override
        public void received(Connection c, Object object) {

        }
    });
}

public static void main(String[] args) {
    try {
        new MPServer();
        Log.set(Log.LEVEL_DEBUG);
    } catch (IOException e) {
        e.printStackTrace();
    }
}
}

MPClient:

public class MPClient {
static final int TIME_OUT = 5000;
Client client;
MultiPlayMatch match;


public MPClient(String name, int team, MultiPlayMatch match) {
    this.match = match;
    client = new Client();
    client.start();

    Network.registerPackets(client);
    addListeners();

    try {
        client.connect(15000, Network.WIFI_IP, Network.PORT);
    } catch (IOException e) {
        e.printStackTrace();
        client.stop();
    }

    while(true) {

    }

}

private void addListeners() {
    client.addListener(new Listener.ThreadedListener(new Listener() {
        @Override
        public void connected(Connection connection) {

        }

        @Override
        public void disconnected(Connection connection) {

        }

        @Override
        public void received(Connection connection, Object object) {

        }
    }));
}
}

Upvotes: 0

Views: 250

Answers (1)

nazmul
nazmul

Reputation: 435

If you are using Windows OS , the reason might be windows firewall, try disabling it if you want to connect your mobile to the local server.

Upvotes: 1

Related Questions