I have server and client code that works perfectly as a Java application. The server is run on my local machine and listens on port 4444. Any Java application can connect easily, but when I move my code over to Android, I keep getting an IOException no matter what I try. I read that you have to change "localhost" to "10.0.2.2", but I'm still getting an IOException. I tried my IP address from whatsmyip.org but it still gives me an IOException. Here's my android code...
package test.myPackage;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class TestProjectActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView tv = new TextView(this);
tv.setText("Hello, Android");
setContentView(tv);
Socket s = null;
try {
s = new Socket("10.0.2.2", 4444);
tv.setText("socket: CONNECTED!");
} catch (UnknownHostException e) {
tv.setText("socket: unknown host");
} catch (IOException e) {
tv.setText("socket: IO Exception");
}
}
}
ipconfigin it. Just put yourIPV4in your code. On linux the open a terminal and executeifconfig. Obviously your ISP has blocked the access to your machine from outside. Your public IP is not what you want.