14

I am unable to reach the Android Server on the emulator from a program on my desktop, how do I solve it?

Some code (from How to find LAN ip address of android device?):

public static ArrayList<String> getSelfIP(){
 try {
 ArrayList<String> ipList = new ArrayList<>();
 for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
 NetworkInterface intf = en.nextElement();
 for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
 InetAddress inetAddress = enumIpAddr.nextElement();
 if (!inetAddress.isLoopbackAddress()) {
 ipList.add(inetAddress.getHostAddress().toString());
 }
 }
 }
 return ipList;
 } catch (SocketException ex) {}
 return null;
}

The result is [fe80::5054:ff:fe12:3456%eth0, 10.0.2.15]

What do I have to configure or do to make the emulator reachable by my desktop programs?

I have done the following:

> adb forward tcp:50000 tcp:50000

However, I am unable to access the server through localhost:50000.

asked Jun 19, 2015 at 8:42
5
  • Why so difficult? See here: developer.android.com/tools/devices/emulator.html But much easier is to use a real device. Commented Jun 19, 2015 at 8:47
  • I have updated the question with the forwarding ADB command, which does not seem to work. Commented Jun 19, 2015 at 9:08
  • Which ip and port do you use to connect to the server on the emulator? On which port your server is listening? Commented Jun 19, 2015 at 9:09
  • Emulator Server is listening at tcp port 50000. I have tried both 10.0.2.1 and 10.0.2.15 to no avail. Commented Jun 19, 2015 at 9:20
  • Are you sure the server is listening on port 50000? Try this command: adb devices -l to see the port number of the emulator you are using. Commented Jun 27, 2015 at 19:11

3 Answers 3

4
+25

Try using the ip address 10.0.2.2.

It is the Special alias to your host loopback interface (i.e., 127.0.0.1 on your development machine)

Ashkan Mobayen Khiabani
34.4k35 gold badges112 silver badges187 bronze badges
answered Jun 23, 2015 at 4:07
Sign up to request clarification or add additional context in comments.

Comments

1

Take a look at this Android documentation, Section "Using Network Redirection".

Setting up Redirection through the Emulator Console

Each emulator instance provides a control console the you can connect to, to issue commands that are specific to that instance. You can use the redir console command to set up redirection as needed for an emulator instance.

First, determine the console port number for the target emulator instance. For example, the console port number for the first emulator instance launched is 5554. Next, connect to the console of the target emulator instance, specifying its console port number, as follows:

telnet localhost 5554

Once connected, use the redir command to work with redirection. To add a redirection, use:

add <protocol>:<host-port>:<guest-port>

where <protocol> is either tcp or udp, and <host-port> and <guest-port> sets the mapping between your own machine and the emulated system, respectively.

For example, the following command sets up a redirection that handles all incoming TCP connections to your host (development) machine on 127.0.0.1:5000 and will pass them through to the emulated system's 10.0.2.15:6000:

redir add tcp:5000:6000

In your case the last command would be

redir add tcp:5000:5000
answered Jun 26, 2015 at 18:56

Comments

0

I had this issue once, use 10.0.2.2 IP adress it should solve your problem.

answered Jun 28, 2015 at 10:12

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.