Flutter gRPC error - OS Error: Connection refused
Flutter gRPC error - OS Error: Connection refused
我正在使用 protobuf 和 gRPC 在 Flutter 应用程序和 python 服务器(Flutter 中的客户端和 python 中的服务器)之间交换信息。服务器 运行ning 在 0.0.0.0 上,客户端正在使用服务器计算机的 IP 地址。
import 'dart:async';
import 'User.pbgrpc.dart';
import 'User.pb.dart';
import 'package:grpc/grpc.dart';
Future<Null> main() async {
final channel = new ClientChannel('IP_ADDRESS',
port: 50051,
options: const ChannelOptions(
credentials: const ChannelCredentials.insecure()));
final stub = new StorageClient(channel);
Test input = new Test();
input.id = 1;
try {
var response = await stub.getPerson(input);
print('Greeter client received: ${response}');
} catch (e) {
print('Caught error: $e');
}
await channel.shutdown();
}
如果我 运行 这个客户端使用 dart client.dart
一切正常,我得到了预期的响应。但是,如果我将此方法嵌入到 flutter 应用程序中,例如:
@override
Widget build(BuildContext context) {
Future<Null> testRPC() async {
final channel = new ClientChannel('IP_ADDRESS',
port: 50051,
options: const ChannelOptions(
credentials: const ChannelCredentials.insecure()));
final stub = new StorageClient(channel);
Test input = new Test();
input.id = 1;
try {
var response = await stub.getPerson(input);
print('Greeter client received: ${response}');
} catch (e) {
print('Caught error: $e');
}
await channel.shutdown();
}
testRPC();
...etc
}
我得到:
I/flutter (18824): Caught error: gRPC Error (14, Error connecting: SocketException: OS Error: No route to host, errno = 111, address = localhost, port = 45638)
更新: 当我 运行 使用模拟器的应用程序时它正在工作。所以这个错误只有在使用真实设备时才会发生。
就我而言,这是防火墙问题。 运行 systemctl stop firewalld
在服务器上解决了。
如果你 运行 AVD(Client) 和后端在同一台电脑上,你必须设置 base URL 而不是 "localhost/127.0.0.1" 到“10.0.2.2”。
这是 Github 中的答案。
我遇到了同样的错误,Flutter 应用程序作为 grpc 客户端,C# 作为 grpc 服务器。问题出在服务器上,我使用“localhost”作为主机参数,更改为“0.0.0.0”,现在 运行 没问题。
我正在使用 protobuf 和 gRPC 在 Flutter 应用程序和 python 服务器(Flutter 中的客户端和 python 中的服务器)之间交换信息。服务器 运行ning 在 0.0.0.0 上,客户端正在使用服务器计算机的 IP 地址。
import 'dart:async';
import 'User.pbgrpc.dart';
import 'User.pb.dart';
import 'package:grpc/grpc.dart';
Future<Null> main() async {
final channel = new ClientChannel('IP_ADDRESS',
port: 50051,
options: const ChannelOptions(
credentials: const ChannelCredentials.insecure()));
final stub = new StorageClient(channel);
Test input = new Test();
input.id = 1;
try {
var response = await stub.getPerson(input);
print('Greeter client received: ${response}');
} catch (e) {
print('Caught error: $e');
}
await channel.shutdown();
}
如果我 运行 这个客户端使用 dart client.dart
一切正常,我得到了预期的响应。但是,如果我将此方法嵌入到 flutter 应用程序中,例如:
@override
Widget build(BuildContext context) {
Future<Null> testRPC() async {
final channel = new ClientChannel('IP_ADDRESS',
port: 50051,
options: const ChannelOptions(
credentials: const ChannelCredentials.insecure()));
final stub = new StorageClient(channel);
Test input = new Test();
input.id = 1;
try {
var response = await stub.getPerson(input);
print('Greeter client received: ${response}');
} catch (e) {
print('Caught error: $e');
}
await channel.shutdown();
}
testRPC();
...etc
}
我得到:
I/flutter (18824): Caught error: gRPC Error (14, Error connecting: SocketException: OS Error: No route to host, errno = 111, address = localhost, port = 45638)
更新: 当我 运行 使用模拟器的应用程序时它正在工作。所以这个错误只有在使用真实设备时才会发生。
就我而言,这是防火墙问题。 运行 systemctl stop firewalld
在服务器上解决了。
如果你 运行 AVD(Client) 和后端在同一台电脑上,你必须设置 base URL 而不是 "localhost/127.0.0.1" 到“10.0.2.2”。
这是 Github 中的答案。
我遇到了同样的错误,Flutter 应用程序作为 grpc 客户端,C# 作为 grpc 服务器。问题出在服务器上,我使用“localhost”作为主机参数,更改为“0.0.0.0”,现在 运行 没问题。