window 下 grpc c+ c#通信

vcpkg安装见

https://vcpkg.io/

vcpkg安装包

.\vcpkg.exe install grpc:x64-windows-static #静态库
.\vcpkg.exe install grpc:x64-windows         #动态库

创建 proto文件


syntax = "proto3";
// The greeting service definition.
service Greeter {
  // Sends a greeting
  rpc SayHello (HelloRequest) returns (HelloReply) {}
}

// The request message containing the user's name.
message HelloRequest {
  string name = 1;
}

// The response message containing the greetings
message HelloReply {
  string message = 1;
}

vcpkg安装gprc过后 分别在下面对应的路径下可以找到protoc.exe  和插件的exe

我的vcpkg在c:\dev\lib\vcpkg下

生成c++代码

C:\dev\lib\vcpkg\installed\x64-windows\tools\protobuf\protoc.exe --cpp_out=. --grpc_out=.  .\dwgtool.proto --plugin=protoc-gen-grpc=C:\dev\lib\vcpkg\installed\x64-windows\tools\grpc\grpc_cpp_plugin.exe

生成c#代码

C:\dev\lib\vcpkg\installed\x64-windows\tools\protobuf\protoc.exe --cpp_out=. --grpc_out=.  .\dwgtool.proto --plugin=protoc-gen-grpc=C:\dev\lib\vcpkg\installed\x64-windows\tools\grpc\grpc_csharp_plugin.exe

c++服务端代码

#include <iostream>
#include <memory>
#include <string>

#include <grpcpp/ext/proto_server_reflection_plugin.h>
#include <grpcpp/grpcpp.h>
#include <grpcpp/health_check_service_interface.h>

#include "grpc/dwgtool.grpc.pb.h"
using namespace grpc;
#pragma comment(lib,"ws2_32.lib")
class GreeterServiceImpl final : public Greeter::Service {
    Status SayHello(ServerContext* context, const HelloRequest* request,
        HelloReply* reply) override {
        // ...
        reply->set_message("dsadsa");
        return Status::OK;
    }
};


void RunServer() {
    std::string server_address("0.0.0.0:50051");
    GreeterServiceImpl service;

    ServerBuilder builder;
    // Listen on the given address without any authentication mechanism.
    builder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
    // Register "service" as the instance through which we'll communicate with
    // clients. In this case it corresponds to an *synchronous* service.
    builder.RegisterService(&service);
    // Finally assemble the server.
    std::unique_ptr<Server> server(builder.BuildAndStart());
    std::cout << "Server listening on " << server_address << std::endl;

    // Wait for the server to shutdown. Note that some other thread must be
    // responsible for shutting down the server for this call to ever return.
    server->Wait();
}

int main()
{
    RunServer();
    std::cout << "Hello World!\n";
}

c#客户端代码

新建个控制台的c#项目

然后安装包

Install-Package Grpc.Net.Client
Install-Package Google.Protobuf
Install-Package Grpc.Tools

 c#的main代码

using System.Threading.Tasks;

using Grpc.Net.Client;


// The port number must match the port of the gRPC server.
using var channel = GrpcChannel.ForAddress("http://localhost:50051");
var client = new Greeter.GreeterClient(channel);
var reply = client.SayHello(
                  new HelloRequest { Name = "GreeterClient" });
Console.WriteLine("Greeting: " + reply.Message);
Console.WriteLine("Press any key to exit...");
Console.ReadKey();
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值