I'm able to access my laptop web server using the Android emulator, I'm using 10.0.2.2:portno
works well.
But when I connect my real Android phone, the phone browser can't connect to the same web server on my laptop. The phone is connected to the laptop using a USB cable. If I run the adb devices command, I can see my phone.
What am I missing?
With the simple solution (just access
laptop_ip_addr:port
from mobile device, when mobile and laptop are on the same WiFi), I get a ERR_CONNECTION_REFUSED error. That is, my MacBook seems to refuse the connection attempt from my mobile.ADB Reverse Socket (Android only)
This solution works for me (tested with a MacBook):
adb reverse tcp:4000 tcp:4000
4000
http://localhost:4000/
, and it will actually connect to the laptop, not the mobile deviceSee instructions here.
The downside is that this works only with a single mobile device at a time. If you want access with another mobile device, you have to first disconnect the first one (disable USB Debugging), connect the new one (enable USB Debugging), and run
adb reverse tcp:4000 tcp:4000
again.ngrok (works with all devices)
Another solution that should always work is ngrok (as mentioned in other answers). It works over the Internet, and not the local network.
It's extremely easy to use:
This outputs, among some other information, a line like
Now, you can navigate to
http://4cc5ac02.ngrok.io
on any device that is connected to the Internet, and this URL redirects tolocalhost:4000
of your laptop.Note that as long as the ngrok command is running (until you hit Ctrl-C), your project is publicly served. Everybody who has the URL can see it.
There is however a far better solution. You can access your host machine with the IP address "10.0.2.2". This has been designed in this way by the Android team. So your webserver can perfectly run at localhost and from your Android app you can access it via "http://10.0.2.2:8080".
You may have your web server listening on your loopback interface and not on your network interface. Major signs of this are:
127.0.0.1
andlocalhost
(from localhost or Android emulator) work192.168.xxx.xxx
do not work, whether from localhost, LAN, or WANI talk more about diagnosing this and fixing this in an answer here.
finally done in Ubuntu , i am running nodejs server on localhost:8080
1) open terminal type ifconfig you will get ip something like this : inet addr:192.168.43.17
2) now simply put url address like this : "192.168.43.17:8080" (8080 port coming from localhost port number) ex : "192.168.43.17:8080/fetch"
Ngrok is the best solution. If you're developing PHP then I recommend installing Laravel Valet, It has MacOS and Linux versions, then you may use
valet share
command. If you're developing any frontend tech and need to share a port like 3000 then use ngrok directlyngrok http 3000
USB doesn't provide network to mobile device.
If both your desktop and phone are connected to the same WiFi (or any other local network), then use your desktop IP address assigned by the router (not
localhost
and not127.0.0.1
).To find out the IP address of your desktop:
ipconfig
(Windows) orifconfig
(Unix)ifconfig | grep "inet " | grep -v 127.0.0.1
will yield only the important stufflocalhost
and127.0.0.1
)If your phone is connected to the mobile network, then things are going to be harder.
Either go hardcore:
<your desktop IP>:<server port number>
Otherwise use something like xip.io or ngrok.