我可以使用Android模拟器访问我的笔记本电脑web服务器,我使用10.0.2.2:portno 工作得很好。
但当我连接真正的Android手机时,手机浏览器无法连接到我笔记本电脑上的同一个web服务器。手机和笔记本电脑通过USB线连接。如果我运行 adb devices命令,我可以看到我的手机。
我错过了什么?
我可以使用Android模拟器访问我的笔记本电脑web服务器,我使用10.0.2.2:portno 工作得很好。
但当我连接真正的Android手机时,手机浏览器无法连接到我笔记本电脑上的同一个web服务器。手机和笔记本电脑通过USB线连接。如果我运行 adb devices命令,我可以看到我的手机。
我错过了什么?
当前回答
使用简单的解决方案(只需从移动设备访问laptop_ip_addr:port,当移动设备和笔记本电脑在同一个WiFi上时),我得到一个ERR_CONNECTION_REFUSED错误。也就是说,我的MacBook似乎拒绝了我手机的连接尝试。
ADB反向套接字(仅限Android)
这个解决方案适用于我(用MacBook进行测试):
用USB线连接Android移动设备到笔记本电脑 在移动设备上打开USB调试 在笔记本电脑上执行adb reverse tcp:4000 tcp:4000 使用您的自定义端口号,而不是4000 现在,在移动设备上,你可以导航到http://localhost:4000/,它实际上会连接到笔记本电脑,而不是移动设备
请看这里的说明。
缺点是这只能在一个移动设备上使用。如果你想用另一个移动设备访问,你必须先断开第一个设备(关闭USB调试),连接新的设备(打开USB调试),然后再次运行adb reverse tcp:4000 tcp:4000。
编辑:据报道此解决方案也适用于Windows。
Ngrok(适用于所有设备)
另一个应该总是有效的解决方案是ngrok(在其他答案中提到过)。它通过Internet工作,而不是本地网络。
它非常容易使用:
brew install ngrok
ngrok http 4000
在其他一些信息中,输出如下一行
Forwarding http://4cc5ac02.ngrok.io -> localhost:4000
现在,您可以在任何连接到Internet的设备上导航到http://4cc5ac02.ngrok.io,这个URL重定向到您笔记本电脑的localhost:4000。
请注意,只要ngrok命令正在运行(直到您按下Ctrl-C),您的项目就是公共服务的。所有拥有URL的人都可以看到它。
其他回答
在mac上,如果你的服务器是8080,在终端上运行这个命令
echo "
rdr pass inet proto tcp from any to any port 8080 -> 127.0.0.1 port 8080
" | sudo pfctl -ef -
以管理员身份运行CMD
在CMD屏幕上输入ipconfig,屏幕将显示文本
就像这张照片
您可以使用这个IP访问您的本地主机 您必须连接到与您的PC连接的相同网络
这些回答中没有一个提到从Windows防御防火墙打开Apache HTTP服务器。使用XAMPP v. 5.6.40,您可以尝试以下解决方案:
Go to Windows Defender Firewall, click on Inbound rules and look for Apache HTTP Server. There will be two instances of this rule. Double click on it and set the Action to Allow the connection. Go to the Advanced tab and tick Domain, Private, and Public options from the Profiles section. 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. Save your changes. Run cmd and type ipconfig. Take note of your IP Address. 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.
基于笔记本电脑的移动设备连接wamp服务器的解决方案:
首先,wifi不是路由器。因此,为了将我的移动设备连接到我笔记本电脑上基于localhost的wamp服务器,我需要一个路由器。 我下载并安装了一个免费的虚拟路由器:https://virtualrouter.codeplex.com/
配置它真的很简单:
右键单击系统托盘中的虚拟路由器图标 2 .单击“配置虚拟路由器” 填写密码 如果您的internet连接是以太网,请选择共享连接:以太网 然后在你的笔记本电脑和设备上打开wifi 在您的设备上连接到虚拟路由器网络名
然后你可以通过你的设备连接到你的笔记本电脑,启动浏览器并填写你的笔记本电脑的IPV4地址 (要在windows上找到它,键入CMD: ipconfig,并找到ipv4地址)
您应该看到wamp服务器主页。
在你的ubuntu/Macbook中使用这个来获取你系统的ip地址。你的手机和系统应该在同一个网络中
ip addr | grep inet这将给你一个看起来像192.168.168.46的ip地址。在你的智能手机上使用这个。
希望这能有所帮助。