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, th
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.
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.
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:
brew cask install ngrok
ngrok http 4000
This outputs, among some other information, a line like
Forwarding http://4cc5ac02.ngrok.io -> localhost:4000
Now, you can navigate to http://4cc5ac02.ngrok.io
on any device that is connected to the Internet, and this URL redirects to localhost: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.
It is actually quite simple.
localhost
(I am using WAMP server for Windows)ipconfig
Once you've done that, you will see something like the following:
Wireless LAN adapter Wireless Network Connection: Connection-specific DNS Suffix . : Link-local IPv6 Address . . . . . : fe80::80bc:e378:19ab:e448%11 IPv4 Address. . . . . . . . . . . : 192.168.43.76 Subnet Mask . . . . . . . . . . . : 255.255.255.0 Default Gateway . . . . . . . . . : 192.168.43.1
192.168.43.76
)Note: Please set your network as "Home Network". Setting the network as Home Network means that you are allowing your PC to share stuff with other devices on the same network.
If you are using Windows 10, this can be done with the following:
If you are having an issue, it is most likely to do with Windows Firewall.
Run CMD as administrator
and on CMD screen type ipconfig and the screen will appear with text
as this photo
and you can access your localhost using this ip you have to be connected to same network as your pc connected to
Solution for Windows:
You are not able to view your website, mainly because your firewall ( default OS firewall or Antivirus firewall ) is preventing incoming connections.
On Windows Firewall:
To allow incoming connections on specific ports, go to:
Control Panel > Windows Defender Firewall > Advanced Settings > Right Click Inbound Rules > Add New Rule.
Then :
- Select "Port" radio button.
- Enter the specific port / range you on which you want to enable incoming connections ( the port on which your server is running )
- Select "Allow the connection" radio button.
- Click Next ( unless you want to change the defaults )
- Give your rule a name and click Finish.
Note:
If you are using an Antivirus having a firewall, then the above steps will not work since the Antivirus would have disabled Windows firewall and instead runs its own firewall.
In that case, depending on the Antivirus program, go to your Antivirus's Firewall settings and open the port.
As this is an old question, there is a new way to do this that is really really simple. Download the ADB Chrome Extension and then follow these instructions:
https://developers.google.com/chrome-developer-tools/docs/remote-debugging
Use this in your ubuntu/Macbook to get the ip address of your system. Your mobile and system should be in the same network
ip addr | grep inet
This will give you an ip address which looks like 192.168.168.46
. Use this in your smartphone.
Hope this helps.