3 #include <grpcpp/grpcpp.h>
9 #include "helloworld.grpc.pb.h"
12 using grpc::ClientContext;
14 using helloworld::Greeter;
15 using helloworld::HelloReply;
16 using helloworld::HelloRequest;
24 std::string
SayHello(
const std::string& user) {
27 request.set_name(user);
34 ClientContext context;
37 Status status =
stub_->SayHello(&context, request, &reply);
41 return reply.message();
43 std::cout << status.error_code() <<
": " << status.error_message() << std::endl;
49 std::unique_ptr<Greeter::Stub>
stub_;
52 int main(
int argc,
char** argv) {
58 std::string target_str;
59 std::string arg_str(
"--target");
61 std::string arg_val = argv[1];
62 size_t start_pos = arg_val.find(arg_str);
63 if (start_pos != std::string::npos) {
64 start_pos += arg_str.size();
65 if (arg_val[start_pos] ==
'=') {
66 target_str = arg_val.substr(start_pos + 1);
68 std::cout <<
"The only correct argument syntax is --target=" << std::endl;
72 std::cout <<
"The only acceptable argument is --target=" << std::endl;
76 target_str =
"localhost:50051";
78 GreeterClient greeter(grpc::CreateChannel(target_str, grpc::InsecureChannelCredentials()));
79 std::string user(
"world");
80 std::string reply = greeter.
SayHello(user);
81 std::cout <<
"Greeter received: " << reply << std::endl;
std::unique_ptr< Greeter::Stub > stub_
std::string SayHello(const std::string &user)
GreeterClient(std::shared_ptr< Channel > channel)
int main(int argc, char **argv)