-1

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.)

Jacob Pickens
  • 471
  • 1
  • 7
  • 15
  • firewall running on your pc? If you run a java application on your pc connecting to the same pc it wouldnt traverse traverse the firewall. What about a java app running on a separate computer connecting to you on the network. – Burrito Jun 18 '15 at 04:10
  • The client on the computer connects just fine, however it's when I try to connect on android that I experience the problem. No I am not aware of a firewall on my computer either. (I also run a Mac, by default the firewall is off and I doubt I'd ever turn it on.) – Jacob Pickens Jun 18 '15 at 04:15
  • if the server is running on pc1 do you have a pc2 to try and connect with using the application you say works fine when connecting from pc1 to pc1? Whats the full stack trace from the exception? – Burrito Jun 18 '15 at 04:21
  • It's not a stack trace, it's more of just Kryonet's way of showing you can't connect. I do have a pc2, and I have used it before to do stuff just like this. I know for a fact it works on PCs, however this is my first time using it on Android. Kryonet does boast that it works on both computers and Android however, so this must be an Android problem rather than anything else. Permissions perhaps? – Jacob Pickens Jun 18 '15 at 04:23

1 Answers1

2

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;
    }
}

}

Yurets
  • 3,999
  • 17
  • 54
  • 74