I am trying to connect from an Android client (from my phone) to a server (my PC). Both the server and the client are using KryoNet. The connection is succesful when I try to connect from a client which is on the same PC as the server. The connection is also succesful when i try to connect from my Android phone connected to wireless (to the same router as the PC which runs the server). The connection fails when i try to connect from the phone while using mobile data (it gives a timeout error). I did port forwarding and opened the port from Windows firewall setting. Any idea what's wrong?
Client code:
final Client client = new Client();
client.getKryo().register(StringRequest.class);
final StringRequest request = new StringRequest();
new Thread(client).start();
System.out.println("Client started.");
try {
client.connect(5000, ipAddress, 54555);
client.addListener(new Listener() {
public void received(Connection connection, Object object) {
if (object instanceof StringRequest) {
StringRequest response = (StringRequest) object;
Gdx.app.log("Client",response.data);
}
}
});
} catch (IOException e) {
Gdx.app.log("Exception", e.getMessage());
e.printStackTrace();
}
request.data = "Hello";
client.sendTCP(request);
Server code:
Server server = new Server();
Kryo kryo = server.getKryo();
kryo.register(StringRequest.class);
server.start();
try {
server.bind(54555);
} catch (IOException e) {
e.printStackTrace();
}
server.addListener(new Listener() {
public void received (Connection connection, Object object) {
if (object instanceof StringRequest) {
StringRequest request = (StringRequest)object;
System.out.println("Server"+request.data);
StringRequest response = new StringRequest();
response.data = "response";
connection.sendTCP(response);
}
}
});
Does your Android application have the Internet permission?
<uses-permission android:name="android.permission.INTERNET" />
It is needed to communicate over the network. Also, make sure you are trying to connect with your PC's external IP, not local IP (192.168.x.xxx
). You can get your external IP here.