我可以使用Android模拟器访问我的笔记本电脑web服务器,我使用10.0.2.2:portno 工作得很好。
但当我连接真正的Android手机时,手机浏览器无法连接到我笔记本电脑上的同一个web服务器。手机和笔记本电脑通过USB线连接。如果我运行 adb devices命令,我可以看到我的手机。
我错过了什么?
我可以使用Android模拟器访问我的笔记本电脑web服务器,我使用10.0.2.2:portno 工作得很好。
但当我连接真正的Android手机时,手机浏览器无法连接到我笔记本电脑上的同一个web服务器。手机和笔记本电脑通过USB线连接。如果我运行 adb devices命令,我可以看到我的手机。
我错过了什么?
当前回答
在Ilya Saunkin回答的基础上,这里有一个命令,应该输出你的IP地址格式为超链接-只需用3000代替你的端口号:
ifconfig | grep "inet " | grep -v 127.0.0.1 | sed 's/netmask.*//g' | sed 's/inet//g' | awk '{print "http://"$1":3000/"}'
根据您运行命令的位置,您应该能够(在Mac上)按住命令键并单击/双击打开链接。
其他回答
基于笔记本电脑的移动设备连接wamp服务器的解决方案:
首先,wifi不是路由器。因此,为了将我的移动设备连接到我笔记本电脑上基于localhost的wamp服务器,我需要一个路由器。 我下载并安装了一个免费的虚拟路由器:https://virtualrouter.codeplex.com/
配置它真的很简单:
右键单击系统托盘中的虚拟路由器图标 2 .单击“配置虚拟路由器” 填写密码 如果您的internet连接是以太网,请选择共享连接:以太网 然后在你的笔记本电脑和设备上打开wifi 在您的设备上连接到虚拟路由器网络名
然后你可以通过你的设备连接到你的笔记本电脑,启动浏览器并填写你的笔记本电脑的IPV4地址 (要在windows上找到它,键入CMD: ipconfig,并找到ipv4地址)
您应该看到wamp服务器主页。
最简单的方法(这对我来说完美无缺)是在0.0.0.0:<port_no>本地托管您的站点,并使用移动设备访问它,在浏览器中使用<local_ipv4_address>:<port_no>/<path>。
要知道您的本地ipv4地址,只需在cmd中键入ipconfig 任何连接到同一网络的设备都可以访问这个url。
在mac上,如果你的服务器是8080,在终端上运行这个命令
echo "
rdr pass inet proto tcp from any to any port 8080 -> 127.0.0.1 port 8080
" | sudo pfctl -ef -
我找到了一个快速解决这个问题的方法。试试这个链接。它应该能帮你解决问题。
我只改变了一件事,在教程中,你把“127.0.0.1”改为“所有”,把它改为你的服务器正在运行的IP地址。
在此之后,您应该能够连接到本地主机。
以下是链接页面信息的(校对)副本:
步骤1
安装Wamp服务器(或其他您喜欢的服务器)。
这是我所知道的设置本地服务器最好的服务器之一。如果您已经安装了Apache或其他服务器,请忽略此步骤。
从这里下载并安装Wamp服务器。
步骤2
在Windows防火墙中为80端口添加一条新规则。
打开控制面板,选择“Windows防火墙”。 在“Windows防火墙”设置页面的左侧面板中选择“高级设置”。 从左侧面板中选择Inbound Rules,然后选择New Rule。 选择“Port”,单击“Next”。 选择“Specific local ports”单选按钮,输入80作为端口值。 保持保持连接不变,执行下一步。 保持Profile选项不变并单击Next。 给新规则起个好听的名字,然后单击Finish。
这将启用本地网络IP上的80端口访问。
步骤3
编辑Wamp服务器的httpd.conf文件以修复403错误。
我们需要编辑这个文件。否则,当我们通过本地网络IP访问本地主机时,将得到403禁止错误。
Click on the Wamp server tray icon. Open Apache server sub menu. Select httpd.conf. 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
找到并将' 127.0.0.1 '替换为' All ',保存文件,并重新启动Wamp服务器。
步骤4
找到您的本地网络IP。
打开命令提示符。 键入并输入ipconfig命令。 在我的例子中,我的局域网地址是10.0.0.2。
这是你需要通过wifi访问你的Android手机上的本地主机的IP。要测试它是否正常工作,请在安装本地主机服务器的桌面浏览器中键入此IP地址。浏览器应该成功地显示您的本地主机页面。这将确保这个本地网络IP现在可以在您的Android手机上成功访问。
我希望本教程将帮助您通过wifi访问您的本地主机。
这些回答中没有一个提到从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.