Windows 向物理设备和模拟器公开WSL中的本地api
我正在使用windows 10计算机上的react本机应用程序。我安装了WSL,在它上面设置了我的后端需求,我可以看到我的api在windows的本地主机上响应,这方面没有问题 现在我想从我的移动应用程序中调用它——既可以从使用android Studio的android模拟器中调用,也可以从通过wifi连接到同一本地网络的物理android设备(使用Expo)中调用 使用Ngrok可以将我的本地主机公开给公共url,但这不是我想要的。如果可能的话,我希望这一切都是本地的。我的电脑的本地ipv4是Windows 向物理设备和模拟器公开WSL中的本地api,windows,api,networking,local,windows-subsystem-for-linux,Windows,Api,Networking,Local,Windows Subsystem For Linux,我正在使用windows 10计算机上的react本机应用程序。我安装了WSL,在它上面设置了我的后端需求,我可以看到我的api在windows的本地主机上响应,这方面没有问题 现在我想从我的移动应用程序中调用它——既可以从使用android Studio的android模拟器中调用,也可以从通过wifi连接到同一本地网络的物理android设备(使用Expo)中调用 使用Ngrok可以将我的本地主机公开给公共url,但这不是我想要的。如果可能的话,我希望这一切都是本地的。我的电脑的本地ipv4
10.0.0.8
,但该地址上没有显示任何内容(既不是来自移动应用程序,也不是来自我的桌面浏览器,它在使用localhost
时工作)
我的Ubuntu内部WSL说:
~$ cat /etc/hosts
# This file was automatically generated by WSL. To stop automatic generation of this file, add the following entry to /etc/wsl.conf:
# [network]
# generateHosts = false
127.0.0.1 localhost
127.0.1.1 JB-PC.localdomain JB-PC
# The following lines are desirable for IPv6 capable hosts
::1 ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
我的ipconfig
:
Carte Ethernet Ethernet :
Suffixe DNS propre à la connexion. . . : Home
Adresse IPv6 de liaison locale. . . . .: fe80::f04e:ae2b:40b9:3d82%6
Adresse IPv4. . . . . . . . . . . . . .: 10.0.0.8
Masque de sous-réseau. . . . . . . . . : 255.255.255.0
Passerelle par défaut. . . . . . . . . : 10.0.0.138
Carte Ethernet vEthernet (WSL) :
Suffixe DNS propre à la connexion. . . :
Adresse IPv6 de liaison locale. . . . .: fe80::68c3:25c9:588a:a957%17
Adresse IPv4. . . . . . . . . . . . . .: 172.20.32.1
Masque de sous-réseau. . . . . . . . . : 255.255.240.0
Passerelle par défaut. . . . . . . . . :
localhost在::1上ping:
C:\WINDOWS\system32>ping localhost
Envoi d’une requête 'ping' sur JB-PC [::1] avec 32 octets de données :
Réponse de ::1 : temps<1ms
Réponse de ::1 : temps<1ms
Réponse de ::1 : temps<1ms
Réponse de ::1 : temps<1ms
Statistiques Ping pour ::1:
Paquets : envoyés = 4, reçus = 4, perdus = 0 (perte 0%),
Durée approximative des boucles en millisecondes :
Minimum = 0ms, Maximum = 0ms, Moyenne = 0ms
我应该怎么做才能使它工作?因此,我找到了一个解决方案,这要感谢我成功地使用了选项2和端口转发,并找到了脚本。请记住,您的PowerShell脚本必须以管理员身份运行。网络在WSL1和WSL2上的工作方式不同。如果使用WSL2,Linux将具有不同的IP地址。-您是否在WSL2上运行Linux?linux系统中的IP地址是什么(例如,通过运行
IP地址
)?您能从Windows ping到该IP地址吗?谢谢,找到了一个解决方案:)
C:\WINDOWS\system32>ping 127.0.0.1
Envoi d’une requête 'Ping' 127.0.0.1 avec 32 octets de données :
Réponse de 127.0.0.1 : octets=32 temps<1ms TTL=128
Réponse de 127.0.0.1 : octets=32 temps<1ms TTL=128
Réponse de 127.0.0.1 : octets=32 temps<1ms TTL=128
Réponse de 127.0.0.1 : octets=32 temps<1ms TTL=128
Statistiques Ping pour 127.0.0.1:
Paquets : envoyés = 4, reçus = 4, perdus = 0 (perte 0%),
Durée approximative des boucles en millisecondes :
Minimum = 0ms, Maximum = 0ms, Moyenne = 0ms