Conexión de Android socket a Java ServerSocket

I am having difficulty connecting an emulated Android Activity to a regular Java program running on my computer. Here are some of the precautions I took that should have fixed them...

  • I try to connect to 10.0.2.2, the ipaddress of the localhost that the emulator is on
  • I spawn a new thread separate from the UI thread to make the connection. (I didn't do a fancy worker thread, I simply created a new thread and said .start())

So basically thy system is composed of a normal Java program that declares a ServerSocket and then waits at the .accept() method. I kick that program off and hopefully it sits there and waits. I then switch over to my Activity class which has an onClick method of a button. That method invokes the .start() method of my new thread. Inside the .run method of my activity I declare a new Socket and try to connect to the local host.

Do you know of anything else I should be aware of? The primary problem is that nothing happens. I put a break point right after the .accept method call in the local host java program and I never reach it.

preguntado el 28 de agosto de 12 a las 14:08

This isn't really a code issue, but more of the way the emulator uses the computers network. The emulator is on an internal fake network that can't reach your computers actual one. You won't have this issue with an actual hardware device. -

Thanks Nick, that appears to be the problem. I thought I read somewhere you could communicate with the local host but I guess not. This will make debugging slightly more difficult... -

1 Respuestas

Please see this thread to connect the emulator network to the computer's network. Hope this helps

Cómo conectar el emulador de Android a Internet

The Top answer from the post is

Creo que algunas de las respuestas pueden haber abordado esto, aunque de manera indirecta, pero esto es lo que funcionó para mí.

Suponiendo que su problema ocurre cuando está en una red inalámbrica y tiene una tarjeta LAN instalada, el problema es que el emulador intenta obtener su configuración de DNS de esa tarjeta LAN. No es un problema cuando está conectado a través de esa LAN, pero es completamente inútil si está en una conexión inalámbrica. Me di cuenta de esto cuando estaba en mi computadora portátil.

So, how to fix? Simple: Disable your LAN card. Really. Just go to your Network connections, find your LAN card, right click it and choose disable. Now try your emulator. If you're like me, it suddenly ... works! - artifex

I found this comment on the answer useful.

Actually, the emulator takes the adapter with the highest priority, so switching the priority of your adapters (wireless as highest) should work too and it won't require you to disable the LAN-adapter completely. Thanks for your answer though, saved me a headache – Anzeo Apr 6 '11 at 14:47

In your case, the Java program is on the same machine. So I thing, the Java program Socket might be binding to the IP of the one of the network interfaces and the Android is trying to connect to the other. To be sure, just disable all network interfaces except one and run your program. Post that you can adjust the priority of the network interfaces.

contestado el 23 de mayo de 17 a las 13:05

Please add more details to your answer in case the link you provided ever changes. In that case, your answer will not be helpful. - Dckuehn

Thanks for the suggestion @Dckuehm, I edited the answer. - Pramod CS

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.