You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter应用无法连接WSL2中Rust(Tonic) gRPC服务器求助

Flutter应用无法连接WSL2中Rust(Tonic) gRPC服务器

在Windows系统的Android Studio中开发Flutter应用,后端为运行于WSL2的Rust(Tonic) gRPC服务器,但应用始终无法连接该服务器,报错信息如下:

connection refused (os error: connection refused, errno = 111), address = localhost, port = 49450

客户端代码:

// client.dart
final grpcChannel = ClientChannel(
  // '10.0.2.2',
  'localhost' // 填写我的IPv4也无效,
  port: 50051,
  options: const ChannelOptions(credentials: ChannelCredentials.insecure())
);

已尝试的解决方案:

  • 关闭Windows防火墙
  • 尝试使用ipconfig查到的所有IPv4地址
  • 在Android模拟器和真实安卓设备上分别测试
  • 使用ngrok,格式为grpc://ngrok_url

注意:使用localhost时,Postman和终端运行客户端代码均可正常连接服务器,但使用IP地址时均无法连接;仅Flutter应用存在连接问题。

终端测试代码示例:

Future<void> main() async {
  try {
    final stub = AuthClient(grpcChannel);
    RegisterRequest data = RegisterRequest();
    
    var resp = await stub.register(data);
    print("message received: ${resp.status}");

  } catch(e){
    print("error received: $e");
  }

}

内容的提问来源于stack exchange,提问作者Ajayi Olamide

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.25 13:02:42