823

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?

Arghavan
  • 1,125
  • 1
  • 11
  • 17
gath
  • 24,504
  • 36
  • 94
  • 124

45 Answers45

631

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 not 127.0.0.1).

To find out the IP address of your desktop:

  • type into the command line ipconfig (Windows) or ifconfig (Unix)
    • on Linux the one-liner ifconfig | grep "inet " | grep -v 127.0.0.1 will yield only the important stuff
    • there's a bunch of suggestions on how to have a similar output on Windows
  • there's going to be a bunch of IP's
  • try all of them (except the forementioned localhost and 127.0.0.1)

If your phone is connected to the mobile network, then things are going to be harder.

Either go hardcore:

  • first find out your router external IP address (https://www.google.de/search?q=myip)
  • then, on the router, forward some port to <your desktop IP>:<server port number>
  • finally use the external IP address and forwarded port

Otherwise use something like xip.io or ngrok.

NOTE: The ifconfig command has been deprecated and thus missing by default on Debian Linux, starting from Debian stretch. The new and recommended alternative for examining a network configuration on Debian Linux is ip command. For example to use ip command to display a network configuration run the following:

ip address

The above ip command can be abbreviated to:

ip a

If you still prefer to use ifconfig as part of your daily sys admin routine, you can easily install it as part of the net-tools package.

apt-get install net-tools

Reference is here

geneowak
  • 978
  • 11
  • 21
Ilya Saunkin
  • 18,934
  • 9
  • 36
  • 50
  • Thanks, this was for great help for me. The issue for me was that I didn't know that the local apache server could be addressed via the ip 10.0.2.2. ... I tries lots of other ip-adresses, like the one show by ipconfig etc. But again, thanks alot! – Vidar Vestnes Sep 22 '11 at 10:25
  • 6
    I don't understand this problem solution. Can you help me about how my telephone can access my server? I hope you flesh out this solution to me. Thanks. – iremce Dec 16 '11 at 00:06
  • Thanks, it works but can't access virtual host on real device. My virtual host is raw.local.com – emeraldhieu May 16 '12 at 07:01
  • 1
    Read my comment below.. You must also have internet access permission. – Sanjay Kumar May 14 '13 at 07:09
  • 5
    @simo go to your router homepage, 10.0.0.1 for XFinity, usually 192.168.1.1 though and log in, look for Connected Devices or Local IPs , find your computer and bam. Mine was 10.0.0.#, then just add [10.0.0.#:(port)/Server/Servlet] or whatever – jp093121 Dec 01 '13 at 18:21
  • See documentation here http://developer.android.com/tools/devices/emulator.html#networkaddresses – Henry Aloni Aug 31 '14 at 12:20
  • if you use Grunt you have to allow access via IP, you must change in the 'Gruntfile.js' the value of 'hostname' from 'localhost' to '0.0.0.0'. – dang Jun 22 '16 at 08:02
  • @Juan the main point is the first sentence. Other than that, I kind of expected this type of comment from a developer who can't find the IP address of his machine on the LAN. – Ilya Saunkin May 08 '17 at 13:31
  • @ElijahSaounkine I can see you significantly refactored your answer, so you proved my point. – chesscov77 May 09 '17 at 16:11
  • @Juan your point is not substantial, therefore your point was not very clear: did you not understand what the previous format of answer said OR you weren't happy with the text of the answer. – Ilya Saunkin May 11 '17 at 08:39
  • @ElijahSaounkine In my opinion, any answer you provide here must be strictly practical, in order to arrive to a solution to the problem. If you just state a bunch of technical concepts without really explaining or showing steps to a solution, then its not a valid answer. Just my two cents. Your refactored answer, on the other hand, is way better, thus im removing the downvote! – chesscov77 May 11 '17 at 21:11
  • 4
    This should not be the accepted answer for this question. Port forwarding in Chrome devtools and adb reverse works perfectly fine with a USB cable. – Roger Madsen Nov 22 '17 at 13:24
  • you can use USB tether. – Banee Ishaque K Jan 30 '18 at 08:40
  • 2
    In ubuntu, you have to disable the firewall as well, type "sudo ufw disable" for it – FingerSmith Feb 08 '18 at 05:37
  • 1
    I got 403 Forbidden when trying to access the localhost. The solution to that is [here](https://stackoverflow.com/a/23385021/4284627). – Donald Duck Oct 21 '18 at 08:14
  • Also note that you might have to enable some settings on your server for this to work. Here's what I had to do for Jekyll: https://stackoverflow.com/a/16608698/873165 – Kevin Workman Aug 18 '19 at 21:53
  • you have forgotten to mention sudo ufw allow – webolizzer Sep 22 '19 at 21:08
  • "use your desktop IP address assigned by the router" - thanks, man. That now works for me! – Dmitry Lvov Mar 01 '20 at 11:17
  • I came upon https://github.com/azimjohn/jprq. An alternative to `ngrok` but open source and without any limitation. Also it will give you a constant domain instead of a dynamic one – Amir Heshmati Dec 12 '20 at 09:15
  • Thanx man! ifconfig | grep "inet " | grep -v 127.0.0.1 and inet showed me my local ip. In my case im accessing my phone by 192.168.0.104:8080. – Mihail Kolomiets Mar 07 '21 at 18:51
  • Doesn't work. ERR_CONNECTION_REFUSED – Patrick May 04 '22 at 18:07
  • Share internet in the pc and connect with your phone. In some internet connections, if pc and phone is in same wifi it works but in some internet not connect. In this case use this method. – Kanan Aug 09 '23 at 16:38
342

It is actually quite simple.

  • Turn on WiFi Hotspot of your Android phone/router and connect your Laptop to your phone
  • Start your server at localhost (I am using WAMP server for Windows)
  • Now open the command prompt and enter
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
  • Copy the IPv4 Address (in this case, it is 192.168.43.76)
  • In your mobile browser, simply paste the IPv4 Address along with the port like 192.168.43.76:3000

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:

  • Open Settings
  • Go to Network & Internet
  • Select WiFi in the left menu
  • Tap on the name of the connected WiFi
  • Set the Network Profile of the network to be Private

If you are having an issue, it is most likely to do with Windows Firewall.

  • Open Control Panel
  • Go to Windows Defender Firewall
  • Tap on Allow an app or feature through Windows Defender Firewall
  • Check whether the app is enabled for Private networks (there should be a tick)
  • If it is not enabled, tap Change settings and tick the checkbox under Private for the app
Gangula
  • 5,193
  • 4
  • 30
  • 59
Dhiral Pandya
  • 10,311
  • 4
  • 47
  • 47
  • 45
    hm did this but the browser seems to take forever to load then it gives me an oops? am I doing something wrong? And what do you mean by home network – Lion789 Jan 18 '14 at 10:55
  • 18
    Please remember to turn off the firewall in computer aswell. – Minh Nguyen Oct 08 '15 at 10:43
  • 4
    *Home Network it is (: Home network means your computer i allowed to share stuff with the devices of the same network. – Leap Hawk Dec 19 '16 at 12:57
  • What do you mean `Turn on Wifi Hotspot of your Android phone/router` ? Do i have to turn on the `Wifi` or `Wifi Hotspot` these both are different i guess – Moeez Feb 08 '17 at 10:13
  • 1
    not working... browser is just trying to load my ip address and fails – Wijay Sharma Feb 13 '18 at 18:18
  • @Lion789, I think he means HomeGroup – Jnr Feb 20 '18 at 10:20
  • Thank you for this answer, it works for me even with my firewall on. I'm trying to make this solution work now when my phone and computer are both connected to my private network, and the IP changes, but the solution doesn't work anymore - my phone says "x.x.x.x took too long to respond". Is my router preventing my phone from connecting to my computer now? – Seany242 Feb 24 '18 at 14:52
  • 1
    This works, but how can I connect to a virtual host set up on my localhost like this? – yaakov Sep 02 '18 at 15:46
  • 15
    "Tap on Allow an app or feature through Windows Defender Firewall Check whether the app is enabled for Private networks (there should be a tick)" What app do you mean? – Liz Lamperouge Jul 16 '19 at 15:07
  • @LizLamperouge In the case of the example, it would be Wamp server. Or if you say use liver-server through node.js/npm, then you would need to check that option in the firewall settings. It depends what you use as the server. Try disabling the firewall temporarily to see if this is the issue. If it is, you know it's a service/app that needs to be granted through windows defender. – friek108 Mar 27 '20 at 04:03
  • Also, check out this question - it may help https://stackoverflow.com/questions/43016713/wampserver-access-server-from-mobile-phone – friek108 Mar 27 '20 at 04:29
  • 6
    my problem was that my network wasn't private, thank you so much – Mahdieh Shavandi Apr 12 '20 at 13:22
  • If you change your network profile to private and it still doesn't work, turn off the server, disconnect from WiFi and then connect again. It worked for me without turning the firewall off. – sanitizedUser Jul 17 '20 at 02:22
  • Thank you! It works on windows. How to achieve the same goal on Mac? – Sovai Feb 12 '22 at 04:50
  • This didn't work for me with `firebase emulators:start` on Windows 10 and Android 11. Tried everything suggested above, but still no luck :( The only thing that worked was using ngrok as suggested below. – thdoan Sep 26 '22 at 08:47
  • Does not work for localhost for Angular/ node server. I have completed all the steps, but still it doesn't work – Franco Jun 06 '23 at 13:48
  • Wow, thank you. I just had to switch my network to "Private Network". Did not need to disable firewall. – Gangula Jun 17 '23 at 23:24
255

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):

  1. Connect Android mobile device with USB cable to laptop
  2. Enable USB Debugging on mobile device
  3. On laptop, run adb reverse tcp:4000 tcp:4000
    • Use your custom port number instead of 4000
  4. Now, on the mobile device, you can navigate to http://localhost:4000/, and it will actually connect to the laptop, not the mobile device

See 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.

EDIT: this solution has been reported to also work with Windows.


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:

brew 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.

weibeld
  • 13,643
  • 2
  • 36
  • 50
  • 3
    Thank you. You helped me a lot. I used adb reverse just replacing 4000 port to 3000 and it worked as i needed. – Marcus Crisostomo Nov 09 '19 at 15:19
  • ngrok was what worked for me in linux, super easy, but every time you it gives you a different url, which expires every 2 hours – StudioWorks Jan 14 '21 at 20:42
  • ngrok worked for me, but I was having trouble with the brew install, following the steps here once I registered for ngrok worked for me: https://dashboard.ngrok.com/get-started/setup – kashgo Feb 23 '21 at 21:38
  • I tried by reversing the port but it worked only once only and stoped working on the next launch so I used the ngrok to achieve this and believe me, it is awesome and working fine. – Shoaib Khan Mar 11 '21 at 06:06
  • I needed exactly a solution that allowed keeping 'localhost' as the url, as a test server I'm using (e.g. mozilla hubs) only allows requests from localhost. My normal ngrok method wasn't an option here. – Kyle Baker Mar 13 '21 at 02:35
  • The *adb solution* should be the accepted answer, since it keeps the ***localhost*** URL. – joakimriedel May 04 '21 at 14:42
  • Good stuff, it might not work with cask anymore: https://github.com/Homebrew/discussions/discussions/902#discussioncomment-398348 – George Adrian Echim Feb 09 '22 at 09:34
  • 1
    where to run this `adb reverse tcp:4000 tcp:4000` on cmd ? – Shoaib Kakal Aug 13 '22 at 09:38
  • Adb worked fine for me. Had to do some firewall trickery to open WSL ports but after that it worked. – foxesque Dec 23 '22 at 10:33
  • This doesn't work for who is running on a ssl port (443) – Iglesias Leonardo Jan 23 '23 at 15:52
  • ngrok worked for me on windows. Install with `choco install ngrok` and running `ngrok http [PORT] --host-header="localhost:[PORT]"` – Ethan Fischer Apr 06 '23 at 18:44
99

Mac OS X users

I achieved this by enabling remote management:

  • Ensure that your phone and laptop are connected to the same WiFi network
  • On Mac, go to System preferences/sharing
  • Enable remote management

You will see a message similar to this:

  • Other users can manage your computer using the address some.url.com

On your Android device, you should now be able to go to some.url.com, which delegates to localhost on your Mac. You can also use ifconfig to get the IP address of your Mac.


Portable solution with ngrok (any OS with Node.js)

If you don't mind exposing your project with a temporary domain you can use ngrok. Lets say I have an app that runs on localhost:9460 I can simply write

npm install ngrok -g

ngrok http 9460

This will give me:

Session Status                online
Update                        update available (version 2.2.8, Ctrl-U to update)
Version                       2.2.3
Region                        United States (us)
Web Interface                 http://127.0.0.1:4040
Forwarding                    http://f7c23d14.ngrok.io -> localhost:9460
Forwarding                    https://f7c23d14.ngrok.io -> localhost:9460

Connections                   ttl     opn     rt1     rt5     p50     p90
                              0       0       0.00    0.00    0.00    0.00

I can now reach https://f7c23d14.ngrok.io as a way to remotely view localhost. This is great to share design work or progress with clients.


Alternate solution with nginx proxy pass

If you are running something like this through nginx proxy_pass it will require a bit more tweaking - this is a hacky approach, but it works for me and I am open to suggestions on improving it:

  • Enable remote management (as mentioned above)
  • Temporarily set the server to listen on port 81 as opposed to 80
  • Type in the following command:
sudo nginx -s reload
  • Visit http://youripaddress:81
server {
  listen 80;
  listen 81;   # <-------- add this to expose the app on a unique port
  server_name  ~^(local|local\.m).example.com$;
  # ...
}

Reload and visit http://youripaddress:81

Ed The ''Pro''
  • 875
  • 10
  • 22
random-forest-cat
  • 33,652
  • 11
  • 120
  • 99
  • 2
    If you're using this 'remote management' setup another helpful thing to know is if you have a lot of aliased servers running in mamp, your mobile device (connected via usb) will only give you access to the first that is defined in httpd-vhosts.conf so just move your current project to the top of the list – DrewT Apr 14 '14 at 20:22
  • I enabled remote management on my mac as you said, and I was given another set of checkboxes for various ways that my computer could be remotely managed. I left them all unchecked except Observe, and it worked fine for me. My web server was running on my mac on localhost port 5555, and I saw "other users can manage your computer using the address 10.0.0.12," so in my phone's browser I typed 10.0.0.12:5555 and saw my web page. – Marcus Jan 07 '18 at 04:31
  • It worked when used the IP `192.168.0.101` but the hostname didn't work. – Krishnadas PC Jun 28 '18 at 06:38
  • 14
    On a Mac, just ALT+click Wi-Fi in your menu bar to get your IP address. – matharden Jul 03 '18 at 11:07
  • I disabled my firewall but still it's showing connection refused, any idea how to get it working? – Unnikrishnan Mar 19 '20 at 18:56
  • ngrok will give cors issue as it changes the url – Deen John Mar 26 '21 at 03:14
  • ngrok found very easy and helpful. But is it secure? – Rohit Parte Apr 27 '21 at 13:58
  • I have used ngrok solution, it works but not totally, I have xampp installed and I have made 100% complete working WordPress site, when I try to visit my site using ngrok, it just shows html content, no css or image is showing. Any idea? – jeet singh Apr 27 '21 at 15:29
  • Edit suggestion: For `ngrok` users using macOS's *hosts* file, with multiple different dev sites served depending on which hostname is passed in, you can use the *-host-header* rewrite option: `ngrok http -host-header=rewrite www.example-local-site.com:80`. – Fateh Khalsa Jan 05 '22 at 04:46
  • how would you solve this if we want to connect by a custom domain instead of IP? on my laptop nginx listens on the loopback ip, and my etc/hosts is configured to a domain name. so on browser mywebsite.com and api.mywebsite.com which all gets reverse proxied accordingly in nginx. connecting only via ip address on my phone doesnt allow this interaction. – cozycoder Sep 09 '22 at 21:49
  • This is a good advice, but now few things changed: after you go to sharing and enable remote managment (don't need to tick on anything there) you have local hostname at the bottom of the Sharing view (and you can edit the name) And if you are using safari be sure to start this with http e.g. "http://local-hostname:port" – Kaspazza May 09 '23 at 09:40
86

This solution is usable even when your mobile device and computer is connected through usb:

You need to use port forwarding in this case. In the Google chrome inspect window (chrome://inspect) You can see the connected devices. enter image description here

Click on the port fowading button, and set a random port say 3000 to the port which your laptop uses say 8080.

enter image description here

now use localhost:3000 from device to access the localhost:8080(or_whatever_ip:portno) in the laptop. You can check it in the mobile browser. Try localhost:3000 in the mobile browser. Make sure to check the "Enable port forwarding" checkbox in the "Port forwarding settings" window

Ajmal Ansari
  • 986
  • 7
  • 8
  • Nice!! I'd like also to add some pro tips on both ends (computer & android): 1.Disable the firewall. 2 Check if there isn't some anti-virus blocking traffic – testing_22 Jan 22 '22 at 23:57
  • If this solution is not working for someone, just try `http://localhost:8080`. Thanks – George G Jul 10 '22 at 02:52
  • Note to self: This method does not work with custom urls (TLDs). Only with localhost. – Dale Ryan Aug 16 '22 at 09:10
  • This is same as using the adb command `adb reverse tcp:3000 tcp:8080` – Ajmal Ansari Nov 09 '22 at 02:20
  • The official Google Documentation for this is here - https://developer.chrome.com/docs/devtools/remote-debugging/local-server/ – jake May 04 '23 at 15:25
42

I found a quick solution to this problem. Try this link. It should help you fix the problem.

I only changed one thing, where the tutorial states you change '127.0.0.1' to 'All', change it to the IP address your server is running on instead.

After that, you should be able to connect to your localhost.


Below is a (proofread) copy of the information from the linked page:

Step 1

Install the Wamp server (or any other you prefer).

This is one of the best servers I know to set up a local server. If you have installed Apache or any other server, ignore this step.

Download and install Wamp Server from here.

Step 2

Add a new rule for port 80 in Windows Firewall.

  1. Open the Control Panel and select Windows Firewall.

  2. Select Advanced settings from the left panel of the Windows Firewall setting page.

  3. Select Inbound Rules from the left panel, then select New Rule.

  4. Select Port and click Next.

  5. Select the “Specific local ports” radio button and enter 80 as the port value.

  6. Keep Allow the connection unchanged and move to the next step.

  7. Keep Profile options unchanged and click Next.

  8. Give some nice name to your new rule and click Finish.

This will enable port 80 access on local network IP.

Step 3

Edit the httpd.conf file of the Wamp server to fix the 403 error.

We need to edit this file. Otherwise, we will get the 403 forbidden error when we access the localhost through a local network IP.

  1. Click on the Wamp server tray icon.

  2. Open Apache server sub menu.

  3. Select httpd.conf.

  4. Find this section of configuration in the httpd.conf file:

    Directory “c:/wamp/www/”
    #
    # Possible values for the Options directive are “None”, “All”,
    # or any combination of:
    # Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
    #
    # Note that “MultiViews” must be named *explicitly* — “Options All”
    # doesn’t give it to you.
    #
    # The Options directive is both complicated and important. Please see
    # http://httpd.apache.org/docs/2.2/mod/core.html#options
    # for more information.
    #
    Options Indexes FollowSymLinks
    
    #
    # AllowOverride controls what directives may be placed in .htaccess files.
    # It can be “All”, “None”, or any combination of the keywords:
    # Options FileInfo AuthConfig Limit
    #
    AllowOverride all
    
    #
    # Controls who can get stuff from this server.
    #
    
    # onlineoffline tag – don’t remove
    Order Deny,Allow
    Deny from all
    Allow from 127.0.0.1
    

Find and replace ‘127.0.0.1’ with ‘All’, save the file, and restart your Wamp server.

Step 4

Find your local network IP.

  1. Open the command prompt.

  2. Type and enter the ipconfig command.

  3. In my case, my local area network address is 10.0.0.2.

This is the IP which you need to access your localhost on your Android phone over wifi. To test if it is working, type this IP address in your desktop browser where your localhost server is installed. The browser should display your localhost page successfully. This will assure that this local network IP is now successfully accessible on your Android phone.

I hope this tutorial will help you to access your localhost over wifi.

Pang
  • 9,564
  • 146
  • 81
  • 122
GertV
  • 831
  • 2
  • 10
  • 23
  • Thanks! this worked so easy. just one thing; I left the httpd.conf as `# onlineoffline tag - don't remove Order Allow,Deny Allow from all` – numediaweb Feb 13 '13 at 10:26
  • 2
    tutorial is windows based? wut? – abbood Dec 13 '13 at 14:03
  • 1
    link is not working anymore so the whole answer is useless now as the information has not been added to SO on how to do it. – rtack May 09 '16 at 09:06
  • For Wamp server v3, I was getting a forbidden error. Check out this answer http://stackoverflow.com/questions/23382627/wamp-cannot-access-on-local-network-403-forbidden on how to amend the vhosts info (don't edit the httpd.conf file as shown above) – friek108 Mar 14 '17 at 01:40
  • This is a really old version of Wamp it looks like, the config files are quite a bit more complicated now. Especially if you use the VHosts feature. I can't figure it out, too many layers of overrrides. – Jonathan Feb 11 '18 at 01:02
42

On Windows PC You may not need to do anything else than finding out your IPv4 Address using "ipconfig" command.

  • Step 1 : Connect your phone to PC using USB cable.
  • Step 2 : Use command 'ipconfig' to find out your IPv4 Address (External IP of the Router) e.g 192.168.1.5 in my case.
  • Step 3: Just access "192.168.1.5:8080" from your phone browser. It works !

Other Env Details : Windows 7, Google Nexus 4 (4.2.2), Tomcat Server, Grails Application.

You should also make sure that you have the permission to access internet in AndroidManifest file :

<uses-permission android:name="android.permission.INTERNET"/>
Sanjay Kumar
  • 1,474
  • 14
  • 22
22

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".

Akhil Jain
  • 13,872
  • 15
  • 57
  • 93
15

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

Jason
  • 51,583
  • 38
  • 133
  • 185
  • 3
    There is no need to download ADB any more it is deprecated, but go to https://developers.google.com/chrome-developer-tools/docs/remote-debugging and follow the instructions. Note: I had to forward port 8443 to localhost:8443 – Dimitris May 20 '14 at 07:23
  • does this only work for url calls from Chrome on the device or is it supposed to work from within my app too? – Orkun Jan 14 '15 at 14:06
  • 3
    This put me on the right track - an more recent answer about Chrome Dev Tools can be found here: [Android Chrome Reverse Port Forwarding](http://stackoverflow.com/a/19191943/383414) – Richard Le Mesurier Mar 25 '15 at 16:51
  • I got stuck because I didn't have google chrome installed on my device. Doh! – Chad Bingham Mar 12 '16 at 02:05
10

"Port forwarding on Chrome for Android makes it easy to test your development site on mobile. It works by creating a listening TCP port on your mobile device that maps to a particular TCP port on your development machine. Traffic between these ports travels through USB, so the connection doesn't depend on your network configuration."

More details here: https://developer.chrome.com/devtools/docs/remote-debugging#port-forwarding

Daniel
  • 984
  • 8
  • 14
9

Run CMD as administrator

and on CMD screen type ipconfig and the screen will appear with text

as this photo enter image description here

and you can access your localhost using this ip you have to be connected to same network as your pc connected to

Youssef Subehi
  • 2,790
  • 1
  • 18
  • 20
8

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 :

  1. Select "Port" radio button.
  2. Enter the specific port / range you on which you want to enable incoming connections ( the port on which your server is running )
  3. Select "Allow the connection" radio button.
  4. Click Next ( unless you want to change the defaults )
  5. 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.

KJ Sudarshan
  • 2,694
  • 1
  • 29
  • 22
7

If you are using a real device and still facing the problem, follow these steps as these steps help me to solve the issue:

  1. Make sure both the mobile device and the laptop are on the same WiFi network.
  2. verify your targeted URL has a port number, something like this:https://localhost:44301
  3. run this command on the Android Studio terminal, adb reverse tcp:44301 tcp:44301
  4. replace 44301 with your port number

Hopefully, you will be able to run without any issue.

Happy codding
6

Try going to this file: C:\wamp\bin\apache\Apache2.2.11\conf\httpd.conf

#   onlineoffline tag - don't remove
    Order Deny,Allow
    Allow from all // change it Deny
    Allow from 127.0.0.1

And change 10.0.2.2 to your IP address.

Peter
  • 142
  • 11
bangptit
  • 61
  • 1
  • 1
6

You may have your web server listening on your loopback interface and not on your network interface. Major signs of this are:

  • Hits on 127.0.0.1 and localhost (from localhost or Android emulator) work
  • Hits on 192.168.xxx.xxx do not work, whether from localhost, LAN, or WAN

I talk more about diagnosing this and fixing this in an answer here.

Community
  • 1
  • 1
einnocent
  • 3,567
  • 4
  • 32
  • 42
6

ngrok lets you put your localhost onto a temporary server and is very simple to set up. I've provided some steps here that can be found in the link:

  1. Download the ngrok zip from the link above
  2. Open the zip
  3. Run your server locally and take note of the port number
  4. In the terminal, go to the folder where ngrok lives and type ngrok http [port number]

You'll see a little dashboard in your terminal with an address pointing to your localhost. Point your app to that address and build to your device.

Maxwell
  • 6,532
  • 4
  • 37
  • 55
6

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"

iamkdblue
  • 3,448
  • 2
  • 25
  • 43
  • this only works for me when connected to same network. I need something that works without internet – Geek Guy Apr 24 '19 at 16:51
6

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.

Naveen T P
  • 6,955
  • 2
  • 22
  • 29
5

I used this process:

  • Install Fiddler on the PC
  • Set up PC and Android device following these excellent instructions
  • Simply go to the browser on the Android device and type in http://ipv4.fiddler to access the localhost

Note that this process requires you to change the WiFi settings on the Android device at the start and end of every session. I found this less painful then rooting my Android device.

user3078359
  • 627
  • 1
  • 6
  • 4
5

The easier way to default localhost is to use http://localhost:port. This works in a laptop and Android as well.

If it does not work, then in android set the default IP of your phone to 127.0.0.1:port :)

Open terminal and type :-

 hostname -i
 #127.0.0.1
 hostname -I
 #198.168.*.*
fmw42
  • 46,825
  • 10
  • 62
  • 80
Harry1992
  • 453
  • 1
  • 5
  • 12
3

This is what worked for me, I added another line after the 127.0.0.1 ip to specify the exact local network ip address (not the public ip address) of the device I wanted to use. In my case my Samsung Galaxy S3

As suggested by Bangptit edit the httpd.conf file (x being the version numbers): C:\wamp\bin\apache\Apache2.x.x\conf\httpd.conf

Search for the onlineoffline tag and add the ip of your phone (I found my phones ip address in my routers configuration pages):

onlineoffline tag - don't remove

 Order Deny,Allow
 Deny from all
 Allow from 127.0.0.1

my phones ip in the line below

 Allow from 192.168.1.65 
 Allow from ::1
 Allow from localhost

One could extend this to include an entire sub domain too for e.g. 192.168.1.0/24 etc

HostMyBus
  • 195
  • 2
  • 9
3

A solution to connect my mobile device to my wamp server based on my laptop:

First, wifi is not a router. So to connect my mobile device to my wamp server based on localhost on my laptop, I need a router. I have downloaded and installed a free virtual router: https://virtualrouter.codeplex.com/

Configuring it is really simple:

  1. right click on virtual router icon in System Tray
  2. click on Configure virtual router
  3. fill a password
  4. if your internet connection is in ethernet, choose Shared connection : Ethernet
  5. Then set wifi on on your laptop and device
  6. On your device connect to the virtual router network name

Then you can connect to your laptop via your device by launching a browser and fill the IPV4 address of your laptop (to find it on windows, type in cmd : ipconfig, and find ipv4 address)

You should see your wamp server home page.

Fred
  • 81
  • 1
  • 5
3

Was running into this problem using several different localhost servers. Finally got my app up and running on the phone in seconds just by using the Python simple server. It only takes a few seconds so is worth a try before getting into any more complicated solutions. First, make sure you have Python installed. cmd+r and type python for Windows or $ python --version in mac terminal.

Next:

cd <your project root>

$ python -m SimpleHTTPServer 8000

Then just find the address of your host machine on the network, I used System Preferences/Sharing on mac to find it. Tap that into your Android device and should load your index.html and you should be good.

If not then the problem is something else and you may want to look into some of the other suggested solutions. Good luck!

* EDIT *

Another quick solution to try if you're using Chrome is the Web Server for Chrome extension. I found it a quick and super easy way to get access to localhost on my phone. Just make sure to check Accessible to local network under the Options and it should work on your cell without any problem.

enter image description here

mikeym
  • 5,705
  • 8
  • 42
  • 62
3

First of all connect your phone and computer to common wifi.

Then, open command prompt using run as administrator

Give ipconfig command

Which shows wireless lan ip

Use ip:port of your server to access in phone

3

EASIEST way (this worked flawlessly for me) is to locally host your site at 0.0.0.0:<port_no> and to access it using mobile devices, use <local_ipv4_address>:<port_no>/<path> in browser.

  • To know your local ipv4 address, just type ipconfig in cmd
  • ANY device connected to the SAME network can access this url.
realpac
  • 537
  • 6
  • 13
3

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 directly ngrok http 3000


Xampp & ngrok

You can also use ngrok with Xampp in Windows. You can use following process. It's quite easy and simple.

  1. Sign up for a FREE account on ngrok
  2. Download the ngrok file from Download section and Open itenter image description here

Note the port your Apache server is running on. It's 80 in my case

enter image description here

  1. In ngrok command line, enter ngrok http YOUR_PORT and copy the Forwarding address enter image description here

Now you can use this Forwarding link on any device (i.e. your physical android devices) and it will be hosting your localhost server. For example,

Replace

http://localhost/my-files/

with

http://YOUR_FORWARDING_ADDRESS/my-files/
Shahzad Ansari
  • 317
  • 3
  • 10
Yamen Ashraf
  • 2,637
  • 2
  • 20
  • 26
2

First of all make your machine(where server is running)IP address static. Enter this static IP address in the android code. Next go to your wifi router's interface and check the left panel. You will see option such as port forwarding/forwarding. Click on it and forward port 80. Now edit your htttpd.conf file and edit it for

Allow from All

. Restart your server. Everything should work fine now.

Additionally you can reserve the IP address of your machine so that it is always allocated to you. In the left panel of your router's interface, find DHCP -> Address Reservation, click on it. Enter your machine's MAC address and the IP address you have entered in the code. Click on save. This will reserve the given IP address for your machine.

Abhishek Singh
  • 358
  • 2
  • 10
2

Adding a solution for future developers.

Copy address of your ip address. right click on your network -> network and sharing-> click on the connection you currently have-> details-> then the address beside ipv4 address is your ip address, note this down somewhere

Go to control panel -> system and security -> windows firewall -> advanced settings -> inbound rules -> new rules (follow the steps to add a port e.g 80, its really simple to follow)

put your ip address that you noted down on your phone browser and the port number you created the rule for beside it. e.g 192.168.0.2:80 and wala.

Possible solution if it doesn't connect. right click network->open network and sharing-> look under view your active connections, under the name of your connection at the type of connection and click on it if it is public, and make sure to change it to a home network.

Shuyaib Abdullah
  • 127
  • 1
  • 10
2

Although one answer has been accepted but this worked for me:

  1. Make sure both your computer and android device are on same network.
  2. get your computer's ip address. (in case of mac type ifconfig inside terminal and look for en0/en1 -> inet it should be something like 19*.16*.1.4.) (on windows, use `ipconfig)
  3. Open your mobile settings and go to network-> long press your wifi network-> modify network -> scroll down to advanced options -> chose proxy manual and in proxy hostname type your address i.e. 19*.16*.1.4 inside port enter your port. and save it.

  4. search for what is my ip in google chrome, and get your ip, lets say it is 1**.1**.15*.2**

  5. try accessing 1**.1**.15*.2**:port/ from your mobile device.

It is as simple as this.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
best wishes
  • 5,789
  • 1
  • 34
  • 59
2

Simple. First and foremost make your your android device and computer is connected on thee same network.e.g router Open command prompt by windows+R and search for cmd then open. On the command type ipconfig and get the ipv4 address.

NB: Firewall blocks access of your computer along network so you need to turn off firewall for the network if either public or private.

How to turn off firewall Open control panel > System and security > windows firewall > on the left pane select turn on and off windows firewall. > Then select turn off windows firewall(not recommended)

You are done

then open your mobile device and run your ip address 192.168.1.xxx


Daniel Nyamasyo
  • 2,152
  • 1
  • 24
  • 23
2

Building on Ilya Saunkin's answer, here's a command that should output your IP address formatted as a hyperlink - just substitute 3000 for whatever your port number is:

ifconfig | grep "inet " | grep -v 127.0.0.1 | sed 's/netmask.*//g' | sed 's/inet//g' | awk '{print "http://"$1":3000/"}'

Depending on where you're running the command, you should be able to (on a Mac) hold the command key and click/double-click to open the links.

Andy
  • 53
  • 1
  • 3
1

What worked for me ( target: debug an windows server app running on localhost:99999 from an client app running on android phone )

  1. phone and pc connected on the same network e.g. 192.168.1.xxx ( connected via wifi in my case)
  2. enable port 99999 in windows firewall
  3. run in cmd as administrator: netsh http add urlacl url=http://*:99999/ user=Everyone

then the service app url (my_dev_machine_ip:99999/path_to_service) was accessible from the phone.

Stefan Michev
  • 4,795
  • 3
  • 35
  • 30
1

Another thing to check is that some routers have issues bridging the requests when both 2.4G and 5G are enabled and the devices are on different frequencies. Trying disabling one of the frequencies so both devices are connected to the same interface.

markb
  • 79
  • 1
  • 7
1

None of these answers mentioned turning on the Apache HTTP Server from Windows Defender Firewall. Using XAMPP v. 5.6.40, you can try this solution:

  1. Go to Windows Defender Firewall, click on Inbound rules and look for Apache HTTP Server. There will be two instances of this rule.
  2. Double click on it and set the Action to Allow the connection.
  3. Go to the Advanced tab and tick Domain, Private, and Public options from the Profiles section.
  4. Go to the Protocols and Ports tab and configure your local ports. By default, XAMPP server runs on port 80. If you're using multiple ports for web development, simply place commas after each port.
  5. Save your changes. Run cmd and type ipconfig. Take note of your IP Address.
  6. Restart Apache from your XAMPP Control Panel. Try accessing your app via address:port/path, where address is your IP address, port is your port (80 in most cases), and path to your project in /xampp/htdocs.
Norseback
  • 193
  • 2
  • 4
  • 16
0

Using a USB cable:

(for example, if you use WAMP server):

1) Install your Android drivers on your PC and download portable Android Tethering Reverse Tool and connect your Android device through the Reverse Tool application.

2) Click on WAMP icon > Put Online (after restarting).

3) Open your IP in the Android browser (i.e. http://192.168.1.22 OR http://164.92.124.42 )

To find your local IP address, click Start>Run>cmd and type ipconfig and your IP address will show up in the output.

That's all. Now you can access (open) localhost from Android.

Peter
  • 142
  • 11
T.Todua
  • 53,146
  • 19
  • 236
  • 237
0

use connectify and xampp or equivalent, and type ip address on mobile URL bar to access

0

Personally I do not insist to use localhost when using device, there is no easy solution.

Just start the activity where you connect to localhost where you can use emulator. Whatever info you have to get from device can be generated with ease and can be sent to activity as parameter.

Ahmet Arslan
  • 5,380
  • 2
  • 33
  • 35
0

its very simple , - GOTO command line (Window + R [type cmd]) - type ipconfig , that will show the current IP address of your PC - write taht IP address on your Android Phone's browser with :80 e.g (http://192.168.x.x:80)

Done

localhost will appear on your Phone

Hamid
  • 1,493
  • 2
  • 18
  • 32
0

On linux use ip addr instead of ifconfig since ifconfig is deprecated for many years and not installed by default in recent distros

dwilda
  • 83
  • 2
  • 12
0

The above didn't work for me. This did for Mac:

In terminal:

ifconfig

Then take the number after inet and put it in your mobile browser:

enter image description here

Kyle Pennell
  • 5,747
  • 4
  • 52
  • 75
0

On mac run this command in the terminal if you have your server in 8080

echo "
rdr pass inet proto tcp from any to any port 8080 -> 127.0.0.1 port 8080
" | sudo pfctl -ef -
0

Having a netconfig xml and assign it in the manifest.xml is the best work around. This will bypass the androids default https only contraint.

Bineesh Kumar
  • 123
  • 10
0

while creating apps , debugging or rendering using mobile is helpful. The system localhost can be accessed using mobile with the IPv4 address with the port number. It should be noted, to achieve this, both the devices should be connected to the same wifi or network and the machines network should be private.

0

My solution is dnsmasq, what get zones from hosts file. Tutorial video:

https://www.youtube.com/watch?v=P2kiinwg00c

Configuration

#copy wifi id
ip link show

#create domain.conf
cd /etc/dnsmasq.d/
>example.com

#add to conf file
no-dhcp-interface=enP4p65s0
bogus-priv
domain=example.com
expand-hosts
local=/example.com/
domain-needed
no-resolv
no-poll
server=127.0.0.1
server=8.8.8.8
Vitalicus
  • 1,188
  • 13
  • 15
-1

If you're running on Linux this works for me

sudo iptables -F
id3vz
  • 460
  • 4
  • 15