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.
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)
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
I had this issue once, use 10.0.2.2 IP adress it should solve your problem.
Related
I'm trying to create a node js server for android phones. How can I create it at home in a local network without outer internet connections? I have wifi at home so my phone can connect to local network. I use official socket.io tutorial and I don't know what to write here (instead of http://chat.socket.io):
private Socket mSocket;
{
try {
mSocket = IO.socket("http://chat.socket.io");
} catch (URISyntaxException e) {}
}
open command prompt on your PC and hit ipconfig. note down your wireless IP address. Let's assume it to be 192.168.1.10. you can also set a static IP for your PC on your router. Next from your socket io code check what port number you are using. for example
http.listen(3000, function(){
console.log('listening on *:3000');
says that you are using port 3000.
Hence in your Android code you need to use http://192.168.1.10:3000/
I have a server program on my PC which is running and waiting for the Android client.
I set these in my Android code:
mHost = "127.168.1.1"; //or getLocalIpAddress()?
mPort = 5000;
mSocket = new Socket(mHost, mPort);
And have these permissions in the manifest:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
When I try to connect to my PC, I get this logcat output:
10-08 19:18:40.970: E/ex closeInputOutput(8735): java.lang.NullPointerException
10-08 19:18:40.970: E/doInBackground Exception(8735): java.net.ConnectException: failed to connect to /127.168.1.1 (port 5000): connect failed: ECONNREFUSED (Connection refused)
I have a router, how can I connect to my PC to 127.168.1.1:5000?
edit:
public static final String getLocalIpAddress() {
try {
for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
NetworkInterface intf = en.nextElement();
for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
InetAddress inetAddress = enumIpAddr.nextElement();
String ip4 = inetAddress.getHostAddress().toString();
if (!inetAddress.isLoopbackAddress() && InetAddressUtils.isIPv4Address(ip4)) {
Log.d(TAG, "getLocalIpAddress(): " + ip4);
return ip4;
}
}
}
}
catch (Exception e) {
Log.e(TAG, "ServerUtils: getLocalIpAddress(): " + e.getMessage());
}
return null;
}
this is my new Code, this return 192.168.1.104, wich is not working. with my ipconfig I get 192.168.1.102 (that is the correct.)
So in the last code what is wrong? How can I get the correct local IP?
127.168.1.1 is by default in the loopback range, so likely the connection attempt is not leaving your Android device. try to put your server on a 192.168.x.x ip
http://www.tcpipguide.com/free/t_IPReservedPrivateandLoopbackAddresses-3.htm
It may be different in your LAN, but usually the address 192.168.1.1 is the router address (not the host address).
Confirm you host address by running the command:
ipconfig
at command prompt. The IPv4 address will be the host address and the Gateway address the router address. Be sure to use the correct address on both PC and device.
Note: You need to post the relevant part of your code, both on PC and device, if you want more precise help.
--EDITED--
IP address is like a home address, each device has his own. So when you run ipconfig on the windows machine you get the windows machine ip address. When you call the getLocalIpAddress() on device you get the device IP address.
If you want to connect from the device to the PC, you need to:
Run on the PC a program that listens to the port 5000. If there is no program listening on this port, any attempt to connect to this port will be refused.
On device you run a program that connects to 192.168.1.102 (pc IP address) at port 5000
Well that is the problem of Firewall.... Go to your router's firewall, or System's Firewall... and add 5000 Port into exceptions list... inbound and outbound too.... And 2nd problem seems with your I.P address... 127 is not a Usable Class... It is Loop back testing (127.0.0.1 to 127.255.255.254 ...) Change your address from 127 to 192.168.0.1 or 1.1 ...
i'm trying to write my own android http server. It's quite OK but i have a problem with my AVD. I don't want to download my app to phone everytime I want to test changes. I would like to connect to my app via AVD.
To get the ip address i'm using this function:
private String getLocalIpAddress() {
try {
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()) { return inetAddress.getHostAddress().toString(); }
}
}
} catch (SocketException ex) {
Log.e("ServerActivity", ex.toString());
}
return null;
}
on my phone everything works good, but when i run my app on AVD it shows ip: 10.0.2.15
and i'm unable to connect to it.
Is there any way to connect to my app running on AVD ?
If it does matter my app uses port 8080.
Telnet into the device (assuming it is on port 5554):
telnet localhost 5554
At the Android console prompt use a redirect:
redir add tcp:8080:8080
Pointing your browser to 'http://127.0.0.1:8080/' should now send, and receive to the AVD.
Courtesy of:
http://www.rhill.co.uk/?p=35
While I don't know the answer to your problem directly, I do know that when connecting FROM an AVD TO your computer, you have to use 10.0.2.2 because your AVD is essentially behind another "router". It doesn't get a local lan IP from your router. See this question for more information. From this link, he quotes:
Each instance of the emulator runs behind a virtual router/firewall
service that isolates it from your development machine's network
interfaces and settings and from the internet. An emulated device can
not see your development machine or other emulator instances on the
network. Instead, it sees only that it is connected through Ethernet
to a router/firewall.
I'm trying to listen on a port using ServerSocket on an Android device. I want to be able to connect to this port over WiFi using a computer on the same network.
I get no exception when binding it to a port, however when I check netstat it says:
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 (null):4040 (null):* LISTEN
I've tried countless ways of binding it to localhost, 0.0.0.0, the WiFi LAN IP address of the device with SocketInetAddress and InetAddress.getByName. Nothing seems to work.
When I try to connect to the port from a computer in the same WiFi (I've tried both netcat and Java's Socket.connect()), all I can see in Wireshark is an ARP request:
Who has [phone's LAN address]? Tell [computer LAN address].
This request repeat itself until timed out.
I've tried the reverse way, by setting the ServerSocket on the computer and connecting to that port from the phone, that works very well.
My testing phone is an Samsung Spica i5700 with a custom ROM.
Any ideas?
Edit:
The code is simple as this:
ServerSocket server = new ServerSocket();
server.setReuseAddr(true);
server.setTimeout(0);
server.bind(new InetSocketAddress(4040));
Socket client = null;
while((client = server.accept()) == null);
// Connected
enter code here
enter code here
Instead of using server.bind, try initializing the server socket like this:
server = new ServerSocket(4040);
Also, server.accept() will actually block until a connection is made, so you don't need that while loop (see: http://download.oracle.com/javase/1.5.0/docs/api/java/net/ServerSocket.html#accept() )
I struggled with this too and was only able to connect to my Android server by using:
ServerSocket myServerSocket = new ServerSocket();
String hostname = getLocalIpAddress();
myServerSocket.bind(new InetSocketAddress(hostname, myPort));
Where hostname was the local IP, which I got using the getLocalIpAddress() function from this page:
https://github.com/Teaonly/android-eye/blob/master/src/teaonly/droideye/MainActivity.java
I was able to get this working by using
ServerSocket server = new ServerSocket( myTcpPort, 0, addr );
where addr = InetAddress of your phone. Otherwise, it only seems to bind to localhost (127.0.0.1). Also, I'm using port 8080.
How can I find the IP address of the an HTC incredible connected to Verizon's network?
My other question - do I have to make any changes to my App to run it on a cellular network? Till now the App was running on a Nexus One connected to a local Wi-Fi network.
Thanks.
Through mobile network, to check your IP run the command below:
ifconfig rmnet0
or application like 'Android Status' can be handy. Get it from the market.
To get IP address info look at this example. Might need a tweak for your CDMA.
You need not modify anything to get it working on Mobile n/w
You can use NetworkInterface.getNetworkInterfaces();, then addresses = intf.getInetAddresses(); to enumerate all the IP addresses on all the interfaces. You're looking for an address associated with a PDP.
That should work. I'm currently looking for a neater way.
To get the internal IP Address you can use the following snippet of code:
Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
while (networkInterfaces.hasMoreElements()) {
NetworkInterface networkInterface = networkInterfaces.nextElement();
Enumeration<InetAddress> inetAddresses = networkInterface.getInetAddresses();
while (inetAddresses.hasMoreElements()) {
InetAddress inetAddress = inetAddresses.nextElement();
byte[] address = inetAddress.getAddress();
}
}
Note: This will return the private network IP Address.
If you want the public IP Address you will need to use STUN (try jSTUN).
Hope that helps!