How can I access my localhost from my Android device?

前端 未结 30 2260
面向向阳花
面向向阳花 2020-11-21 05:53

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

相关标签:
30条回答
  • 2020-11-21 06:44

    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.


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

    0 讨论(0)
  • 2020-11-21 06:45

    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

    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
    0 讨论(0)
  • 2020-11-21 06:45

    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

    0 讨论(0)
  • 2020-11-21 06:46

    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.

    0 讨论(0)
  • 2020-11-21 06:47

    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

    0 讨论(0)
  • 2020-11-21 06:47

    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.

    0 讨论(0)
提交回复
热议问题