0

I'm trying to get started with basic client server stuff. I have a Java server socket program running on one of my computers. My Android phone is running a simple application and is acting as the client. I want the phone to be able to connect to the Java program on my computer.

The client tries to connect to my computer's local IP address, but I can't get it to connect. I get a IOException. Here's the Android code I'm using:

        try {
            echoSocket = new Socket("192.168.0.19", 7077);
            System.out.println("length: "+ "socket created!");
        } catch (UnknownHostException e) {
            System.out.println("length: "+ "Don't know about host.");
        } catch (IOException e) {
            System.out.println("Couldn't get I/O for "
                    + "the connection.");
        }

If I run both the client and my server, on my computer and then try to connect to the computer's local IP (same as above), it works fine. But I want to connect from my phone.

Any ideas what I need to do?

EDIT: Here's the stack trace:

   07-21 15:37:34.223: W/System.err(3474): android.os.NetworkOnMainThreadException
07-21 15:37:34.223: W/System.err(3474):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
07-21 15:37:34.223: W/System.err(3474):     at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
07-21 15:37:34.223: W/System.err(3474):     at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
07-21 15:37:34.223: W/System.err(3474):     at libcore.io.IoBridge.connect(IoBridge.java:112)
07-21 15:37:34.223: W/System.err(3474):     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
07-21 15:37:34.223: W/System.err(3474):     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
07-21 15:37:34.223: W/System.err(3474):     at java.net.Socket.startupSocket(Socket.java:566)
07-21 15:37:34.223: W/System.err(3474):     at java.net.Socket.tryAllAddresses(Socket.java:127)
07-21 15:37:34.223: W/System.err(3474):     at java.net.Socket.<init>(Socket.java:177)
07-21 15:37:34.223: W/System.err(3474):     at java.net.Socket.<init>(Socket.java:149)
07-21 15:37:34.223: W/System.err(3474):     at com.nikhildev.cricketeye.MainActivity.startVideoCamera(MainActivity.java:45)
07-21 15:37:34.223: W/System.err(3474):     at java.lang.reflect.Method.invokeNative(Native Method)
07-21 15:37:34.223: W/System.err(3474):     at java.lang.reflect.Method.invoke(Method.java:511)
07-21 15:37:34.223: W/System.err(3474):     at android.view.View$1.onClick(View.java:3594)
07-21 15:37:34.223: W/System.err(3474):     at android.view.View.performClick(View.java:4204)
07-21 15:37:34.223: W/System.err(3474):     at android.view.View$PerformClick.run(View.java:17355)
07-21 15:37:34.223: W/System.err(3474):     at android.os.Handler.handleCallback(Handler.java:725)
07-21 15:37:34.223: W/System.err(3474):     at android.os.Handler.dispatchMessage(Handler.java:92)
07-21 15:37:34.223: W/System.err(3474):     at android.os.Looper.loop(Looper.java:137)
07-21 15:37:34.223: W/System.err(3474):     at android.app.ActivityThread.main(ActivityThread.java:5041)
07-21 15:37:34.233: W/System.err(3474):     at java.lang.reflect.Method.invokeNative(Native Method)
07-21 15:37:34.233: W/System.err(3474):     at java.lang.reflect.Method.invoke(Method.java:511)
07-21 15:37:34.233: W/System.err(3474):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-21 15:37:34.233: W/System.err(3474):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-21 15:37:34.233: W/System.err(3474):     at dalvik.system.NativeStart.main(Native Method)
Cricketer
  • 2,391
  • 4
  • 24
  • 29

1 Answers1

0

With Android and sockets you need to watch out for a few things

  1. Android needs to be the client, since its IP address is expected to change more frequently than that of the server
  2. The port number you use should be > 1024
  3. If on a emulator, you need to be aware that the emulator's IP is 10.something. Look up the Android documentation.
  4. Permissions in the manifest

Lastly, your issue is that you are running a network api in the main UI thread. You need to run the same in a Async task. Thats why you are getting the StrictMode exception thingy.

Edit :

As for the ping, check if you can ping to your network router. And if you can ping to the outside world then your issue is that your router is blocking the ping inside.

taxeeta
  • 1,188
  • 1
  • 12
  • 25