Connecting Real device to Android emulator - android-emulator

I am developing a network app for Android and I'm still stuck on connecting my real Android device with an device-emulator running on my desktop computer.
I've created private network with a router, so the only ones connected to the network are my pc and my mobile phone, in order to avoid firewall/closed ports problems.
My PC ip is 192.168.1.100 and I'm trying to ping each other so I can sea reachability of each network node. Ping works fine from my PC (not the emulator console) to the phone.
The problem is that I want to ping the PC-emulator from my mobile phone, not the PC itself... For that, I use the emulator console... Should I use my computer IP or should I use another one? I've seen some ip's like "10.0.x.x" here http://developer.android.com/guide/developing/tools/emulator.html
But I guess those are for connecting two EMULATORS, right?
Besides, I've tried to connect them by socket, creating a redirection for the port via Emulator console, but still can't connect them.
Any clues?
Thanks!!

I tried the early solution I gave you and it didn't work. As you said maybe the reason is the redir command of the emulator console only redirects packets comming from the localhost.
So I searched for a simple proxy server and used it in the same machine to test it out.
http://www.java2s.com/Code/Java/Network-Protocol/Asimpleproxyserver.htm
With this I used the following configuration:
on the proxy:
String host = "localhost";
int remoteport = 3000;
int localport = 4000;
Then run the emulator instance:
Server socket listening on port 2000.
Open telnet instance and issue "redir add tcp:3000:2000"
And finally on the real device open a socket to the machine address on port 4000.
So the network map looks like:
Device <-> machine:4000 Proxy machine:3000 <-> :3000 Emulator :2000 -> Application
This worked for me using the same application on the device and emulator.

I've reached the conclusion that emulator can only receives packets coming from the loopback (127.0.0.1), since when you issue "redir add tcp:port:newPort, it only redirects the first port (associated to the loopback) to the second port (associated to the "emulator virtual ip").
I've tried to create a bridge, which redirects all the packets coming to my pc to the IP 127.0.0.1, but still not works. Thus, I think the emulator has been developed only to communicate with other emulators...
I hope anyone that comes here contradicts me.

You may be able to connect a real device with an emulator instance.
Did you tried setting a redirection on the emulated device and then connect the real device trough a java socket?
For example:
On the emulated device open a server socket listening on port 2000, then open a telnet connection and issue the command:
redir add tcp:4000:2000
Finally, open a socket on the real device to your machine address (192.168.1.100) on port 4000.

Related

How to connect a small web server program to a web browser which is running in another device in the internet?

I had made a small web server program that can handle HTTP requests, it worked fine on loopback ip address. I Had connected the host computer to a wifi using a router, i can access that program using another device connected to the same router using a web browser. but the problem is when i tried to open the open the command from the device connected to another router/internet, it didnt connect.
I had used the address like "10.0.0.4:8080" to connect with the device on the same router. later i tried with the public IP address to connect to my web server program but it didnt work!
what am i missing in giving the correct ip address and port number such that it could be connected with the devices that arent connected with my router.
You have to add port forwarding on the router from outside to the specific IP of the device. Then use public IP from outside and public or private from LAN. Probably also set it to static internal IP from device or router.
DMZ to the device is also an option but is overkill.
That way lot of bots will scan your server so carefull with the security and what you expose.

How to connect two memu emulators for socket connection in android?

I am new in android and trying to test my android app for socket connection between two emulators, one as server and second as client, as they can send and receive data. I created two projects for server socket and client socket with same package name in android studio 2.3.3 and want to run every project on separate emulator. I know to run this scenario I need to do port forwarding via telnet commands and know how to do it for default android studio emulators, but I want to use it for memu emulators. I don’t know if memu supports telnet, the way to do port forwarding is exactly the same like android studio emulators and if not (memu doesn’t support telnet) is there solution for my goal and how to do it?
I set ADB host port for first emulator (server socket) to 5559 , static ip 10.0.3.14 and for second emulator (client socket) to 5563, ip 10.0.3.15. When I run “telnet localhost 5558” to connect (memu) emulator console in cmd, result is this message: “Connecting To localhost...Could not open connection to the host, on port 5558: Connect failed”. But if I run “telnet localhost 5559” telnetlocalhost window appears! I really surprised because when run adb devices command it shoe 5558 port number (console port), but telnet work for 5559 port number (adb port).
I see in memu manager window, network settings,advanced there is a port forwarding button, but I don’t think it is the same I want. Also read on memu blog we can setup LAN with multiple MEmu instances via bridge connection, but I read another place “Network bridging does not work when the underlying physical network device is a wireless device”. So I confused if bridge connection can help me while I’m using laptop that (android)memu emulators run on it.
I really appreciated everyone can help me to find how connect two memu emulators for socket connection?
P.S: Finally, I apologize if I wrote more than enough and sorry for my poor English.

How to connect a socket to Genymotion device that is hosting a server?

I am currently trying to make a socket connection from my client (iOS simulator) to my host (Genymotion simulator). I am having a hard time finding the correct IP address and configuring the right settings for VirtualBox to get this to work.
How can I find the proper IP address and port for iOS to connect to when I host the server on the Genymotion server?
By default Genymotion devices use a NAT connection to connect to internet. It means they are not visible from your local network. You can change this connection to a Bridged connection. This type of connection will let your local DHCP gives the Genymotion device’s IP, then you will be able to reach it from everywhere on your local network.
To fix this, open VirtualBox app, choose the device you want to setup, open the settings > Network tab > Adapter 2 > Attached to: "Bridged Adaptor” instead of “NAT".
Then you need to get the local IP of the device. You can get it by running this command:
adb shell "ifconfig | awk '/inet addr/{print substr(\$2,6)}' | awk 'NR==2'"
Then you can connect to your device very easily using this IP, from everywhere in your local network.
We are working on making this really easier in the future. I'll update the post when this will be released.

Connect an Android Device To a Web Service on Local Host

I implemented a web service for an Android application. The web service is running on my local host (192.168.1.2). Using the Android emulator I succeeded to connect to web service. The I tried to connect my Android device using debugging mode to web service but it didn't work. So my question is if it is possible to connect an Android device to this web service that is running on my local host (192.168.1.2) without using a real IP ?
It's much simpler way supported by google!
Connect your phone via usb to computer and enable usb debugging
On your computer open Chrome browser and type exactly this address: chrome://inspect/#devices
Now you can link your computer port to your device port by port forwarding button. On my computer I have service on address localhost:61437 and I just linked it to device's 8081 port. Remeber to check 'Enable port forwarding' checkbox
screen from service on my computer ( localhost:61437 )
screen from my mobile browser with the same service ( localhost:8081). And that's it. Also you use this service address in your application
Did you already solve your problem? I also got a problem like you. These are the steps that I already done:
unplug lan cable or turn off any other internet connection from your pc.
connect your android mobile to your pc using usb.
turn on usb tethering
back to your pc. check your ip. mine is 192.168.42.37
check your webservice app in your pc. let's say http://192.168.42.37/webserviceapp
back to your android mobile. try this url http://192.168.42.37/webserviceapp
Now you can access your webservice app in your pc from your mobile phone.
Well your localhost is 127.0.0.1 (or ::1) and your LAN IP is 192.168.1.2. Each pc/device that are connected under your LAN could reach your webservice on IP 192.168.1.2
Your Android device must be so connected under the same LAN maybe through Wifi connection so it will be able to talk with 192.168.1.2.
If you can't connect your Android device under the same LAN eg you have just a 3g connection you need to play with your router/firewall to redirect all incoming traffic (maybe just the http traffic) from your public ip to you private ip (192.168.1.2)
Hope this help
I'll throw in my process, since nothing on SO worked for me. Here are the steps I took to connect my physical android device to the web service running on my laptop (connected to the phone) on localhost:
Enable USB debugging on your Android device
Run your web service on your machine. My web service runs on localhost, port 3000 in development: http://localhost:3000/api/...
Run ifconfig (Unix), or ipconfig (Windows)
Find your machine's inet address on your LAN interface. Mine is 10.0.0.121 for interface wlan0. Externally, it is 68.43.XX.XXX, which is not the address that you want to use.
Use the LAN IP since you are connecting to your service on LAN, otherwise you might get an econnrefused (connection refused) error due to firewall rules
Build your http URL with that IP address, and the port that your web service is running on. For me, it's http://10.0.0.121:3000/api/...
When you launch your app, you should connections to your local web service in logs, Wireshark, etc, and you should see the desired activity/data in your Android application.
I had the same issues, researched a lot then found out that you have to explicitly make changes in your firewall settings. Your firewall is blocking your code to be accessed from external source. So, all you need to do is, go to firewall settings, add port 80 (in my case since, I am using Apache http Server) for inbound and outbound. Now, you can test it on your phone's browser http://192.16..**:80/
I've done that on a Mac using GasMask and Charles Proxy Server. Your phone and your computer have to be on the same network.
say the webservice url you want to access is at http://api.xyz.com, you first use GasMask to point that url to your localhost, then use Charles to set up a proxy server. Then you go to the settings on your phone, go into Wi-Fi, long-press the network you are connected to, choose Modify Network, and enter the proxy settings Charles gave you.
In my case, nothing of these solutions works because Windows firewall blocks it, but putting a rule on the firewall hasn't effect.
The problem in my case is that my laptop is connected with Wifi and Windows had the Wifi connection like a Public network. I must to change the network connection to Private network. http://www.comofuncionatodo.net/tecnologia/informatica/como-cambiar-de-red-publica-a-red-privada-en-windows-10/
I agree with the other answers as good approaches if you don't want to expose your DEV webservice on the internet. However, it's much easier if you do just expose the webservice. There's a number of free DNS services, but I've found no-ip to be the easiest to set up. I use it for exactly the purpose that you asked about; so I can test with my DEV webservice on a real device.
If you choose to go with no-ip (I have no affiliation with that company, it's just the one I've used and am familiar with), you can get a free publicly accessible URL like http://MyExampleWebServer.no-ip-org, and no-ip has a utility you can install so even if you're behind a dynamic IP, it will always keep the correct external IP associated with that URL. If you're working from your house, then you'd just need to make sure you port forward traffic from port 80 to your internal 192.x.x.x IP address (or whatever port you use; maybe 443 for ssl).
It's as easy as that, and now you can hit that webservice from any device that can access the internet.
I haven't worked with it, but I believe dyndns also offers a similar service.
This solution is for GAE development server in Eclipse
Step 1: Get the LAN IP
Goto your Windows Command Console (Press Win+R, then type "cmd"). In the console, enter "ipconfig". You will see a list of display. Under Wireless LAN adapter Wi-Fi, get the IPv4 Address. It will be something 192.168.x.x
LAN IP : 192.168.x.x
Step 2:
Go to Eclipse, Open the Configured server
Under Properties of GAE Development Server -> Local Interface address to bind to, enter the LAN IP address, and save.
Step 3:
Now you can access the GAE server by
http://192.168.x.x:8888/
8888 - Refers to the Port Number, as mentioned in the GAE development server
In order to access local web services using their own server hosts rather than IP addresses with ports, do these following steps:
Make sure your Android device and your local machine are on the same network.
Install SquidMan on your Mac, Linux, or any other Proxy Server.
Configure the proxy server's HTTPPort (ex. 5555) and clients (ex. 192.168.0.0/24) to your own network mask, and run the proxy server.
You are either using the web services in:
a. A web browser: Configure the proxy settings of your Android device from Modify WiFi networks.
b. Android application:
Set up the Proxy for your HTTP client. If you are using Volley, check this out: Volley Behind a Proxy server.
You can now connect to it by using whatever URL you are using on your host to connect to the web service (ex. http://my-local-machine.com)
Hint: If you got 4xx response codes, make sure your web service allows connections from other non-local-hosts.
If you are referring your localhost on your system from the Android emulator then you have to use
http://10.0.2.2:8080/
Because Android emulator runs inside a Virtual Machine(QEMU) therefore here 127.0.0.1 or localhost will be emulator's own loopback address.

Android Emulator Networking

How can I make Android Emulator and the host machine (Ubuntu 9.10) connected together to the same network?
For example
Android Emulator IP Address : 10.0.2.15
Ubuntu Host IP Address: 10.0.2.16
Thanks in advance
You have to use port forwarding using either 'adb forward' tool of Android or using 'redir add' from the emulator console
Android uses an internal router through which it accesses the host network connection. In that router, there is a firewall to avoid communication to the external world. If you need to enable communication between your emulator and the host, you need to use port forwarding option that is provided in Android emulator. Use telnet localhost port# in Android console. port# is usually appearing in the title bar of the emulator window. Numbers such as 5554, 5556, 5557 and so on.
Use add <protocol>:<host-port>:<guest-port> to define a port forwarding rule for your router. Protocol is udp/tcp and host port is the port of your computer and guest port is the port of your emulator. So, if you want to transfer all the http call to theh ost use add tcp:80:8080
More information is here