如何从我的 Android 设备访问我的本地主机? [英] How can I access my localhost from my Android device?
问题描述
我可以使用 Android 模拟器访问我的笔记本电脑网络服务器,我使用的是 10.0.2.2:portno
效果很好.
I'm able to access my laptop web server using the Android emulator, I'm using 10.0.2.2:portno
works well.
但是当我连接真正的 Android 手机时,手机浏览器无法连接到笔记本电脑上的同一网络服务器.手机使用 USB 数据线连接到笔记本电脑.如果我运行adb devices 命令,我可以看到我的手机.
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.
我错过了什么?
推荐答案
USB 不为移动设备提供网络.
USB doesn't provide network to mobile device.
如果您的台式机和手机都连接到同一个 WiFi(或任何其他本地网络),则使用路由器分配的台式机 IP 地址(不是 localhost
而不是 127.0.0.1
).
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
).
要找出您桌面的 IP 地址:
To find out the IP address of your desktop:
- 在命令行中输入
ipconfig
(Windows) 或ifconfig
(Unix)- Linux 上的单行
ifconfig |grep "inet" |grep -v 127.0.0.1
将只产生重要的东西 - 关于如何在窗户
- type into the command line
ipconfig
(Windows) orifconfig
(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
如果您的手机已连接到移动网络,那么事情会变得更加困难.
If your phone is connected to the mobile network, then things are going to be harder.
要么去硬核:
- 首先找出您的路由器外部 IP 地址 (https://www.google.de/search?q=myip)
- 然后,在路由器上,将某个端口转发到
<您的桌面 IP>:<服务器端口号>
- 最终使用外部 IP 地址和转发端口
Otherwise use something like xip.io or ngrok.
注意:
ifconfig
命令已被弃用,因此默认情况下在 Debian Linux 上缺失,从 Debian stretch 开始.在 Debian Linux 上检查网络配置的新推荐替代方法是 ip 命令.例如要使用 ip 命令显示网络配置,请运行以下命令: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
上面的ip命令可以简写为:
The above ip command can be abbreviated to:
ip a
如果您仍然喜欢使用
ifconfig
作为日常系统管理程序的一部分,您可以轻松地将其安装为net-tools
包的一部分.If you still prefer to use
ifconfig
as part of your daily sys admin routine, you can easily install it as part of thenet-tools
package.apt-get install net-tools
参考是这里
这篇关于如何从我的 Android 设备访问我的本地主机?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
- on Linux the one-liner
- Linux 上的单行