iPhone Swift gRPC 客户端从 Windows C# gRPC 服务器获取“连接被拒绝”
Posted
技术标签:
【中文标题】iPhone Swift gRPC 客户端从 Windows C# gRPC 服务器获取“连接被拒绝”【英文标题】:iPhone Swift gRPC client gets "Connection refused" from Windows C# gRPC Server 【发布时间】:2021-02-12 20:48:36 【问题描述】:我有一个在 Windows 10 中运行的用 C# 编写的 gRPC 服务器。我可以连接到它并在同一台 PC 上使用 C# gRPC 客户端调用服务,但是当我尝试从运行 Swift 中的简单客户端的 iPhone 连接时,应用报告
2021-02-12 12:28:48.639854-0800 grpc-Swift[53758:732067] [connection] nw_socket_handle_socket_event [C5:1] Socket SO_ERROR [61: Connection refused]
当我在 Windows PC 上的同一端口上运行 Golang 服务器时,我能够从手机连接到它,因此这不是布线或防火墙问题。我错过了什么??
.proto 文件
syntax = "proto3";
package chat;
option go_package = "github.com/monkrus/grpc-from0;grpc_from0";
message Message
string body = 1;
service ChatService
rpc SayHello(Message) returns (Message)
c#服务器:
using System.Threading.Tasks;
using Chat;
using Grpc.Core;
namespace ChatServer
public class ChatServiceImpl : ChatService.ChatServiceBase
public override Task<Message> SayHello( Message request, ServerCallContext context )
Message reply = new Message Body = "Got it!" ;
return Task.FromResult( reply );
class Program
static int port = 2319;
static void Main( string[] args )
Console.WriteLine( $"Starting Chat Server on port port" );
Server server = new Server
Services = ChatService.BindService( new ChatServiceImpl() ) ,
Ports = new ServerPort( "localhost", port, ServerCredentials.Insecure )
;
server.Start();
Console.WriteLine( "Server started. Press any key to exit." );
Console.ReadKey();
server.ShutdownAsync().Wait();
iPhone 客户端 ViewController.swift:
import UIKit
import GRPC
class ViewController: UIViewController
var client:Chat_ChatServiceClient?
override func viewDidLoad()
super.viewDidLoad()
setupConnection()
func setupConnection()
let eventGroup = PlatformSupport.makeEventLoopGroup(loopCount: 1)
let channel = ClientConnection.insecure(group: eventGroup).connect(host:
"192.168.86.44", port: 2319 )
self.client = Chat_ChatServiceClient(channel: channel)
@IBAction func sendMessage()
createMessage()
func createMessage()
var message = Chat_Message()
message.body = "Hello from swift"
let x = client?.sayHello(message).response
x?.whenSuccess( (message) in print("response from server: \(message.body)") )
在 Windows 上运行的 golang 服务器:
package main
import (
"log"
"net"
"fmt"
"go-grpc-tutorial/chat"
"google.golang.org/grpc"
)
func main()
fmt.Println( "Listening on port 2319..." )
lis, err := net.Listen("tcp", ":2319")
if err != nil
log.Fatalf("Failed to listen on port 2319: %v", err)
s := chat.Server
grpcServer := grpc.NewServer()
chat.RegisterChatServiceServer(grpcServer, &s)
if err := grpcServer.Serve(lis); err != nil
log.Fatalf("Failed to serve grpc server over port 2319: %v", err)
【问题讨论】:
【参考方案1】:找到了!我从
更改了服务器初始化Ports = new ServerPort( "localhost", port, ServerCredentials.Insecure )
到
Ports = new ServerPort( "0.0.0.0", port, ServerCredentials.Insecure )
一切都按预期工作。
如果我在客户端网络上使用服务器的 IP 地址 - 即“192.168.86.44”,它也可以工作。很想听听这个数字是如何解释的——如果“localhost”是一个与客户端不同的网络?如果“0.0.0.0”只是一个通配符...?
【讨论】:
以上是关于iPhone Swift gRPC 客户端从 Windows C# gRPC 服务器获取“连接被拒绝”的主要内容,如果未能解决你的问题,请参考以下文章