Android Kryonet client wont connect to my PC server?

1.7k views Asked by At

I made a basic server in eclipse:

Server server;
int tcp = 8885, udp = 8885;

public ServerMain() {
    server = new Server();
    server.addListener(new Listener() {
        public void connected(Connection c) {
            System.out.println("Connection made");
        }

        public void received(Connection c, Object p) {

        }

        public void disconnected(Connection c) {
            System.out.println("Disconnection made");
        }
    });
    server.start();
    try {
        server.bind(tcp, udp);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

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

Then I created my client in android studio. I installed the library correctly. (According to google) I put the Kryonet jar into the libs folder and then rebuilt the project. I could then use Kryonet classes. Here is my code for my client:

Client client;
String ip = "MY.IP.ADDRESS";
int tcp = 8885, udp = 8885;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Log.i(TAG, "Start");

    client = new Client();
    client.addListener(new Listener() {
        public void received(Connection c, Object p) {

        }
    });
    client.start();
    try {
        client.connect(5000, ip, tcp, udp);
        Log.i(TAG, "conected");
    } catch(IOException e) {

        Log.i(TAG, "Error: " + e.getMessage());

    }
}

It's the exact same code I would use in a normal Java client except now it's running directly from my android device. I get this error:

Error: Unable to connect to: /MY.IP.ADDRESS:8885

It cannot connect to my IP even though I port forwarded it and all. Why won't it connect? I have read online to add in this to my manifest file:

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

So, I did:

<?xml version="1.0" encoding="utf-8"?>

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

Still to absolutely no avail. What am I doing wrong? (P.S. I have tried with both my network IP, and my public IP. I have no idea what I'm doing wrong, everything works fine with a normal Java program, but with an android app it bugs out.)

1

There are 1 answers

0
Osama Najjar On BEST ANSWER

Try this code, it's working:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Log.i(TAG, "Start");

    client = new Client();
    client.addListener(new Listener() {
            public void received(Connection c, Object p) {

            }
    });
    client.start();
    new ConnectToServer.execute();


    }
public class ConnectToServer extends AsyncTask<String, int[], String>
{
    @Override
    protected String doInBackground(String... arg0) {
        // TODO Auto-generated method stub
        try {
             client.connect(5000, ip, tcp, udp);
             Log.i(TAG, "conected");
        } catch(IOException e) {

        Log.i(TAG, "Error: " + e.getMessage());
        return null;
    }
}

}