gpt4 book ai didi

c++ - GRPC 错误代码 12 客户端服务器应用程序 C++ [调试]

转载 作者:行者123 更新时间:2023-11-30 01:37:16 27 4
gpt4 key购买 nike

我需要使用 gRPC 在 C++ 中创建客户端服务器应用程序。我引用了以下tutorialexample在 gRPC 上制作一个客户端服务器应用程序,它看起来非常类似于存储库中的 helloworld 示例 (grpc/examples/cpp/helloworld/)。但是,当我运行服务器端 (manager.cc) 时,我收到一条消息说 Error code 12: RPC failed。请让我知道哪里出错了:

manager.cc(类似于greeter_server.cc):

#include<stdio.h>
#include<iostream>
#include <memory>
#include <string>
#include <grpcpp/grpcpp.h>
#include "nodes.grpc.pb.h"
#include<map>
#define MAX_NODES 20
using namespace std;
using grpc::Server;
using grpc::ServerBuilder;
using grpc::ServerContext;
using grpc::Status;
using nodes::joinrequest;
using nodes::nodejoin;
using nodes::serverresponse;
struct track
{
int nodes_having_it[MAX_NODES];
int last_serviced_by;
int tot_nodes;
};
#define MAX_NO_OF_NODES 15
static map<int,int> map_key_node;//Primary
static map<int, struct track> track_service;//Keep track of replicated nodes that have the key
static map<int, int> version_map;

list <int> nodes_joined;

struct NodeDS{
int nodeid;
int pred;
int succ;
int version;
static map<int,int> data_stored;//storage
};

class nodejoining final: public nodejoin::Service
{
Status joinCM(ServerContext* context, joinrequest* request,serverresponse* reply_CM)
{
cout << "called";
int node_id = request -> id();
nodes_joined.push_back(node_id);
//reply_CM.set_stat("done");
cout << "OK";
return Status::OK;
}
};

void Nodeserver()
{
std::string server_address("0.0.0.0:50051"); // As of now to test with a single node, have to figure out a way to give different addressed to different nodes.
nodejoining service;

ServerBuilder builder;
builder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
builder.RegisterService(&service);
std::unique_ptr<Server> server(builder.BuildAndStart());
std::cout << "Server listening on " << server_address << std::endl;
server->Wait();

}

int main()
{

Nodeserver();
std::hash<string> hashvalue;
string client_id = "Haha"; //As of now --- to be obtained from client later
long nodeid_hash = hashvalue(client_id);
cout << nodeid_hash << "\n";
int data = 100;
return 0;



}

node.cc(类似于greeter_client.cc):

#include<stdio.h>
#include<iostream>
#include <memory>
#include <string>
#include <grpcpp/grpcpp.h>
#include "nodes.grpc.pb.h"
#include<map>
#define MAX_NODES 20
using namespace std;

using grpc::Channel;
using grpc::ClientContext;
using grpc::Status;
using nodes::joinrequest;
using nodes::nodejoin;
using nodes::serverresponse;

class Node_init
{
public:
Node_init(std::shared_ptr<Channel> channel): stub_(nodejoin::NewStub(channel)) {}
std::string joinCM(const int& n_id)
{
joinrequest request_CM;
request_CM.set_id(n_id);
cout << "1" << "\n";

serverresponse reply_CM;
ClientContext context;
cout << "\n" << "2";

Status status = stub_->joinCM(&context, request_CM, &reply_CM);
cout << "\n" << "3";
if(status.ok())
{
cout<<"\n I have joined you";
return "Success";
}
else
{ std::cout << status.error_code() << ": " << status.error_message() << std::endl;
return "Failed";

}


}
private:
std::unique_ptr<nodejoin::Stub> stub_;
};

int main(int argc, char** argv)
{
Node_init nj(grpc::CreateChannel("localhost:50051",grpc::InsecureChannelCredentials()));
std::string reply = nj.joinCM(2);
std::cout << "\n" << reply;

}

nodes.proto(类似于 grpc/examples/protos/中的 helloworld.proto):

syntax = "proto3";

package nodes;

service nodejoin
{
rpc joinCM (joinrequest) returns (serverresponse) {}
}

message joinrequest
{
int32 id = 1;
}

message serverresponse
{
string stat = 1;
}

最佳答案

您对 joinCM 的定义中缺少一个常量

状态 joinCM(ServerContext* 上下文,joinrequest* 请求,serverresponse* reply_CM)

应该是

状态 joinCM(ServerContext* context, const joinrequest* request,serverresponse* reply_CM)
反而。编译器通过“隐藏重载的虚函数”警告来指示这一点:)

关于c++ - GRPC 错误代码 12 客户端服务器应用程序 C++ [调试],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49984987/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com