首页 > 解决方案 > 无法连接到 TCP 端口:尝试在 WSL2 中的 Android 模拟器上打开 Expo 应用程序时连接被拒绝

问题描述

我一直在关注本指南https://medium.com/@pellea/using-adb-with-wsl2-7aebbb070a47到目前为止,WSL2 可以通过 Android Studios 看到在 Windows 上运行的模拟器-> AVD 管理器。例如,在 Windows 和 WSL2 上,adb devices正确显示:

List of devices attached
emulator-5554   device

但是,当我转到我的 expo 应用程序并尝试启动 Android 模拟器时,我收到以下错误消息

Couldn't start project on Android: could not connect to TCP port 5554: Connection refused

这是在我尝试了以下操作之后:

  1. ADB_SERVER_SOCKET=tcp:<MY IP>:5037在我的 WSL2 配置文件中导出
  2. 通过(来自上面的链接)从我的 Windows 防火墙解除阻止 WSL2 vEthernet 连接
Set-NetFirewallProfile -DisabledInterfaceAliases "vEthernet (WSL)"
  1. 我已将 5554 从 Windows 移植到 WSL2,以及 5037(从 0.0.0.0 到 127.0.0.1):
$WSL_CLIENT = bash.exe -c "ip addr show eth0 | grep -oP '(?<=inet\s)\d+(\.\d+){3}'";
$WSL_CLIENT -match '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}';
$WSL_CLIENT = $matches[0];
iex "netsh interface portproxy add v4tov4 listenport=5554 listenaddress=127.0.0.1 connectport=5554 connectaddress=$WSL_CLIENT"

端口如下:

netsh interface portproxy show all

Listen on ipv4:             Connect to ipv4:

Address         Port        Address         Port
--------------- ----------  --------------- ----------
127.0.0.1       8081        172.25.38.171   8081
0.0.0.0         5037        127.0.0.1       5037
127.0.0.1       5554        172.29.149.0    5554

标签: expoadbwindows-subsystem-for-linux

解决方案


您是否尝试将端口 5554 映射到 Windows IP?(或本地主机)。Android 模拟器是在 Windows 上运行的,而不是 WSL2,所以端口代理应该是从 WSL2 到 Windows,而不是向后。

您是否已经以其他方式解决了它?


推荐阅读