使用cmake利用grpc库生成一个通信案例(包含服务端和客户端)
时间: 2023-08-08 14:07:34 浏览: 260
好的,下面是使用CMake利用gRPC库生成一个通信案例(包含服务端和客户端)的步骤:
1. 安装gRPC库
首先需要在本地安装gRPC库,可以参考官方文档进行安装。
2. 创建CMake项目
使用CMake创建一个新的项目,并在项目中添加以下内容:
a. CMakeLists.txt文件
在项目根目录下创建一个CMakeLists.txt文件,内容如下:
```cmake
cmake_minimum_required(VERSION 3.10)
project(grpc_example)
set(CMAKE_CXX_STANDARD 11)
# Find gRPC
find_package(gRPC REQUIRED)
# Find Protobuf
find_package(Protobuf REQUIRED)
# Include generated *.pb.h files
include_directories(${CMAKE_CURRENT_BINARY_DIR})
# Generate *.pb.cc and *.pb.h files
protobuf_generate_cpp(PROTO_SRCS PROTO_HDRS
proto/example.proto
)
# Define server source files
set(SERVER_SOURCES
${CMAKE_CURRENT_SOURCE_DIR}/src/server.cpp
${PROTO_SRCS}
)
# Define client source files
set(CLIENT_SOURCES
${CMAKE_CURRENT_SOURCE_DIR}/src/client.cpp
${PROTO_SRCS}
)
# Define server executable
add_executable(server ${SERVER_SOURCES})
target_link_libraries(server gRPC::grpc++ gRPC::grpc++_reflection protobuf::libprotobuf)
# Define client executable
add_executable(client ${CLIENT_SOURCES})
target_link_libraries(client gRPC::grpc++ gRPC::grpc++_reflection protobuf::libprotobuf)
```
b. example.proto文件
在项目的proto目录下创建一个example.proto文件,内容如下:
```protobuf
syntax = "proto3";
package example;
// Define request message
message Request {
string message = 1;
}
// Define response message
message Response {
string message = 1;
}
// Define service
service ExampleService {
rpc SendMessage(Request) returns (Response) {}
}
```
c. server.cpp文件和client.cpp文件
在项目的src目录下分别创建一个server.cpp文件和client.cpp文件,内容如下:
server.cpp:
```cpp
#include <iostream>
#include <memory>
#include <string>
#include <grpcpp/grpcpp.h>
#include "example.grpc.pb.h"
using grpc::Server;
using grpc::ServerBuilder;
using grpc::ServerContext;
using grpc::Status;
using example::Request;
using example::Response;
using example::ExampleService;
// Server implementation
class ExampleServiceImpl final : public ExampleService::Service {
public:
Status SendMessage(ServerContext* context, const Request* request, Response* response) override {
std::string message = "Server received: " + request->message();
std::cout << message << std::endl;
response->set_message(message);
return Status::OK;
}
};
// Main function
int main() {
std::string server_address("0.0.0.0:50051");
ExampleServiceImpl service;
// Build server
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;
// Wait for server to shutdown
server->Wait();
return 0;
}
```
client.cpp:
```cpp
#include <iostream>
#include <memory>
#include <string>
#include <grpcpp/grpcpp.h>
#include "example.grpc.pb.h"
using grpc::Channel;
using grpc::ClientContext;
using grpc::Status;
using example::Request;
using example::Response;
using example::ExampleService;
// Client implementation
class ExampleClient {
public:
ExampleClient(std::shared_ptr<Channel> channel) : stub_(ExampleService::NewStub(channel)) {}
std::string SendMessage(const std::string& message) {
Request request;
request.set_message(message);
Response response;
ClientContext context;
// Call server
Status status = stub_->SendMessage(&context, request, &response);
if (status.ok()) {
return response.message();
} else {
return "RPC failed";
}
}
private:
std::unique_ptr<ExampleService::Stub> stub_;
};
// Main function
int main() {
std::string server_address("0.0.0.0:50051");
// Create client
ExampleClient client(grpc::CreateChannel(server_address, grpc::InsecureChannelCredentials()));
// Call server method
std::string response = client.SendMessage("Hello from client!");
std::cout << "Server responded with: " << response << std::endl;
return 0;
}
```
3. 构建项目
在项目根目录下执行以下命令构建项目:
```bash
mkdir build
cd build
cmake ..
make
```
4. 运行项目
在build目录下执行以下命令启动服务端:
```bash
./server
```
在另一个终端窗口下执行以下命令运行客户端:
```bash
./client
```
客户端将会向服务端发送一条消息,并在控制台上输出服务端的回复消息。
阅读全文