#include <QCoreApplication>
#include <iostream>
#include <memory>
#include <string>
#include <grpcpp/grpcpp.h>
#include "helloworld.grpc.pb.h"
using grpc::Channel;
using grpc::ClientContext;
using grpc::Status;
using helloworld::HelloRequest;
using helloworld::HelloReply;
using helloworld::Greeter;
class GreeterClient {
public:
GreeterClient(std::shared_ptr<Channel> channel)
: stub_(Greeter::NewStub(channel)) {}
// Assembles the client's payload, sends it and presents the response back
// from the server.
std::string SayHello(const std::string& user) {
// Data we are sending to the server.
HelloRequest request;
request.set_name(user);
// Container for the data we expect from the server.
HelloReply reply;
// Context for the client. It could be used to convey extra information to
// the server and/or tweak certain RPC behaviors.
ClientContext context;
// The actual RPC.
Status status = stub_->SayHello(&context, request, &reply);
// Act upon its status.
if (status.ok()) {
return reply.message();
} else {
std::cout << status.error_code() << ": " << status.error_message()
<< std::endl;
return "RPC failed";
}
}
private:
std::unique_ptr<Greeter::Stub> stub_;
};
int main(int argc, char *argv[])
{
//QCoreApplication a(argc, argv);
//return a.exec();
// Instantiate the client. It requires a channel, out of which the actual RPCs
// are created. This channel models a connection to an endpoint (in this case,
// localhost at port 50051). We indicate that the channel isn't authenticated
// (use of InsecureChannelCredentials()).
GreeterClient greeter(grpc::CreateChannel(
"localhost:50051", grpc::InsecureChannelCredentials()));
std::string user("world");
std::string reply = greeter.SayHello(user);
std::cout << "Greeter received: " << reply << std::endl;
}
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
Qt_MinGW_64bit_gRPC_Server&Client_demo msys2配置gRPC,Qt 6.3.0 demo. ============================== 希望各位boss指正(QQ 278397935) 最烦要C币。 本demo 包含 server端和client端,也是参照前辈的博文。感谢挖井人。 含helloworld.grpc.pb.cc helloworld.pb.cc helloworld.proto helloworld.grpc.pb.h helloworld.pb.h,建议自己重新生成,以免因为环境差异导致编译失败。 http://t.csdn.cn/2h4ld
资源详情
资源评论
资源推荐
收起资源包目录
Qt_MinGW_64bit_gRPC_Server&Client_demo.rar (16个子文件)
QtgRPC-Server
main.cpp 2KB
helloworld.proto 383B
helloworld.pb.h 16KB
QtgRPC-Server.pro 3KB
QtgRPC-Server.pro.user 18KB
helloworld.grpc.pb.h 13KB
helloworld.grpc.pb.cc 4KB
helloworld.pb.cc 20KB
QtgRPC-Client
main.cpp 2KB
helloworld.proto 383B
helloworld.pb.h 16KB
QtgRPC-Client.pro 3KB
QtgRPC-Client.pro.user 18KB
helloworld.grpc.pb.h 13KB
helloworld.grpc.pb.cc 4KB
helloworld.pb.cc 20KB
共 16 条
- 1
QQ_278397935
- 粉丝: 98
- 资源: 13
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论5