Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Server Socket

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.

like image 281
theAnonymous Avatar asked Jun 19 '15 08:06

theAnonymous


People also ask

What are Android sockets?

A socket is an endpoint for communication between two machines. The actual work of the socket is performed by an instance of the SocketImpl class. An application, by changing the socket factory that creates the socket implementation, can configure itself to create sockets appropriate to the local firewall.

What is a server socket?

Sockets are commonly used for client and server interaction. Typical system configuration places the server on one machine, with the clients on other machines. The clients connect to the server, exchange information, and then disconnect. A socket has a typical flow of events.

What is the purpose of the Bluetooth server socket?

The interface for Bluetooth Sockets is similar to that of TCP sockets: Socket and ServerSocket . On the server side, use a BluetoothServerSocket to create a listening server socket. When a connection is accepted by the BluetoothServerSocket , it will return a new BluetoothSocket to manage the connection.

What is the difference between server socket and client socket?

A server (program) runs on a specific computer and has a socket that is bound to a specific port. The server waits and listens to the socket for a client to make a connection request. If everything goes well, the server accepts the connection. Upon acceptance, the server gets a new socket bound to a different port.


2 Answers

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)

like image 57
Emil Avatar answered Oct 20 '22 14:10

Emil


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
like image 1
user2641570 Avatar answered Oct 20 '22 13:10

user2641570