六、通讯录4.0实现—⽹络版
简易版本
服务端完整版本
客户端完整版本
Protobuf还常⽤于通讯协议、服务端数据交换场景。那么在这个⽰例中,我们将实现⼀个⽹络版本的
通讯录,模拟实现客⼾端与服务端的交互,通过Protobuf来实现各端之间的协议序列化。
需求如下:
-
客⼾端可以选择对通讯录进⾏以下操作:
-
- 新增⼀个联系⼈
-
- 删除⼀个联系⼈
-
- 查询通讯录列表
-
- 查询⼀个联系⼈的详细信息
-
服务端提供增删查能⼒,并需要持久化通讯录。
-
客⼾端、服务端间的交互数据使⽤Protobuf来完成。
如下图:
- 客户端要有一个菜单,新增一个联系人…
- 每个功能都有一对请求和响应协议.
- 例如实现新增一个联系人,首先我们要设计message, 中间是网络传输.
- 然后安装图形序号执行,客户端完成:1,2,3,7 ; 服务端完成:1,4.5,6
1. 环境搭建
1.1 安装Httplib库
Httplib库:cpp-httplib是个开源的库,是⼀个c++封装的http库,使⽤这个库可以在linux、windows平台下完成http客⼾端、http服务端的搭建。
使⽤起来⾮常⽅便,只需要包含头⽂件 httplib.h
即可。编译程序时,需要带上-lpthread
选项。
源码库地址:https://github.com/yhirose/cpp-httplib
镜像仓库:https://gitcode.net/mirrors/yhirose/cpp-httplib?utm_source=csdn_github_accelerator
1.1升级 gcc
$ gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/4.8.5/lto-wrapper
Target: x86_64-redhat-linux
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --
infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-
bootstrap --enable-shared --enable-threads=posix --enable-checking=release --with-system-
zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --
enable-linker-build-id --with-linker-hash-style=gnu --enable-languages=c,c++,objc,obj-
c++,java,fortran,ada,go,lto --enable-plugin --enable-initfini-array --disable-libgcj --
with-isl=/builddir/build/BUILD/gcc-4.8.5-20150702/obj-x86_64-redhat-linux/isl-install --
with-cloog=/builddir/build/BUILD/gcc-4.8.5-20150702/obj-x86_64-redhat-linux/cloog-install -
-enable-gnu-indirect-function --with-tune=generic --with-arch_32=x86-64 --build=x86_64-
redhat-linux
Thread model: posix
gcc version 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
cpp-httplib 用老的编译器,要么编译不通过,要么直接运行报错
百度搜索:scl gcc devsettool升级gcc
//安装scl
$ sudo yum install centos-release-scl scl-utils-build
//安装新版本gcc,这里也可以把7换成8或者9,我用的是9,也可以都安装
$ sudo yum install -y devtoolset-7-gcc devtoolset-7-gcc-c++
$ ls /opt/rh/
//启动: 细节,命令行启动只能在本会话有效
$ scl enable devtoolset-7 bash
$ gcc -v
//可选:如果想每次登陆的时候,都是较新的gcc,需要把上面的命令添加到你的~/.bash_profile中
$ cat ~/.bash_profile
# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# User specific environment and startup programs
PATH=$PATH:$HOME/.local/bin:$HOME/bin
export PATH
#添加下面的命令,每次启动的时候,都会执行这个scl命令
scl enable devtoolset-7 bash
or
scl enable devtoolset-8 bash
or
scl enable devtoolset-9 bash
2. 搭建简单的服务器
用来测试httplib
Client.cpp
#include"httplib.h"
#include<iostream>
#include<string>
using namespace std;
using namespace httplib;
const string IP = "127.0.0.1";// 监听所有的ip
//const string IP = "192.139.99.192";
const int PORT = 6666;
int main()
{
Client client(IP.c_str(),PORT);
// 传输./test-Post 资源
Result res1 = client.Post("./test-Post");
if(res1->status ==200){
cout<<"post sucess Post"<<endl;
}
// 请求./test-Post 资源
Result res2 = client.Get("./test-Get");
if(res2->status ==200){
cout<<"get sucess Get"<<endl;
}
}
Server.cpp
#include"httplib.h"
#include<iostream>
#include<string>
using namespace std;
using namespace httplib;
const string IP = "127.0.0.1";
//const string IP = "0.0.0.0";
//const string IP = "192.139.99.192";
const int PORT = 6666;
int main()
{
Server svr;
// 注册post处理方法
svr.Post("./test-Post",[](const Request& req ,Response& res){
cout<< "server test Post"<<endl;
res.status=200;
});
svr.Get("./test-Get",[](const Request& req ,Response& res){
cout<< "server test Get"<<endl;
res.status=200;
});
svr.listen(IP.c_str(),PORT);
cout<<"sucess"<<endl;
}
3. 约定双端交互接⼝
定制http协议
新增⼀个联系⼈:
[请求]
Post /contacts/add
Content-Type: application/protobuf
AddContactRequest
[响应]
Content-Type: application/protobuf
AddContactResponse
删除⼀个联系⼈:
[请求]
Post /contacts/del
Content-Type: application/protobuf
DelContactRequest
[响应]
Content-Type: application/protobuf
DelContactResponse
查询通讯录列表:
[请求]
GET /contacts/find-all
[响应]
Content-Type: application/protobuf
FindAllContactsResponse
查询⼀个联系⼈的详细信息:
[请求]
Post /contacts/find-one
Content-Type: application/protobuf
FindOneContactRequest
[响应]
Content-Type: application/protobuf
FindOneContactResponse
4. 代码实现客户端
这里我们只实现了新增⼀个联系⼈模块,完整代码在码云.
add_contact.proto
syntax="proto3";
package add_contact;
message AddContactReq{
string name = 1 ;
int32 age = 2 ;
message Phone{
string number = 1;
enum PhoneType{
MP=0;
TEL=1;
}
PhoneType type =2;
}
repeated Phone phones = 3;
}
message AddContactResp{
bool success = 1; // 添加联系人是否成功
string error_desc =2 ;// 错误信息
string uid =3 ; // 联系人序号
}
ContactException.h:定义异常类
#include<string>
class ContactException
{
private:
std::string message;
public:
ContactException(std::string str="A problem"):message(str){}
std::string what()const {return message; }
};
main.cc
#include "httplib.h"
#include "contactException.h"
#include "add_contact.pb.h"
#include <iostream>
#include <string>
using namespace std;
using namespace httplib;
const string IP = "127.0.0.1"; // 监听所有的ip
// const string IP = "192.139.99.192";
const int PORT = 6666;
void menu();
void addContact();
void buildAddContactReq(add_contact::AddContactReq *req);
int main()
{
while (true)
{
enum OPTION{QUIT = 0,ADD,DEL,FIND_ALL,FIND_ONE};
menu();
cout << "--------请选择:";
int choose;
cin >> choose;
cin.ignore(256, '\n');
try
{
switch (choose)
{
case ADD:
addContact();
break;
case DEL:
break;
case FIND_ALL:
break;
case FIND_ONE:
break;
case QUIT:
cout << "程序退出" << endl;
exit(0);
break;
default:
break;
}
}
catch (ContactException &e)
{
cout << "--->操作通讯录时发生异常" << endl
<< "--->异常信息:" << e.what() << endl;
}
}
}
void addContact()
{
Client client(IP, PORT);
//构造 req
add_contact::AddContactReq req;
buildAddContactReq(&req);
// 序列化 req
string req_str;
if (!req.SerializePartialToString(&req_str))
{
throw ContactException("req 序列化失败");
}
// 发起post调用
auto ret = client.Post("/contacts/add", req_str, "Content-Type: application/protobuf");
if (!ret)
{
string err_desc;
err_desc.append("Post /contacts/add 请求失败! 错误信息:")
.append(/*httplib::to_string(ret.error())当前httplib没有该函数*/
to_string(ret.error()));
throw ContactException(err_desc);
}
// 方序列号resp
add_contact::AddContactResp resp;
bool parse = resp.ParseFromString(ret->body);
if(!parse){
throw ContactException("反序列化失败!");
}
if (ret->status != 200 && !parse)
{
string err_desc;
err_desc.append("/contacts/add 调用失败")
.append(std::to_string(ret->status))
.append("(")
.append(ret->reason)
.append(")")
.append(resp.error_desc());
throw ContactException(err_desc);
}
else if (ret->status != 200)
{
string err_desc;
err_desc.append("/contacts/add 调用失败")
.append(std::to_string(ret->status))
.append("(")
.append(ret->reason)
.append(")")
.append("错误信息:")
.append(resp.error_desc());
throw ContactException(err_desc);
}
else if (!resp.success())
{
string err_desc;
err_desc.append("/contacts/add 结果异常\t异常原因")
.append(std::to_string(ret->status))
.append("(")
.append(resp.error_desc())
.append(")");
throw ContactException(err_desc);
}
// 结果打印
cout<<"新添加的联系人"<<resp.uid()<<"成功"<<endl;
}
void buildAddContactReq(add_contact::AddContactReq* req)
{
cout << "请输入姓名:";
string name;
getline(std::cin,name);
cout << "请输入年龄:";
int age;
cin >> age;
cin.ignore(256, '\n');
int i = 1;
req->set_age(age);
req->set_name(name);
while (true)
{
cout << "请输入手机号码" << i++ << ":";
string number;
getline(std::cin,number);
if (number.empty())
{
//cout << "输入联系人完成\n";
break;
}
add_contact::AddContactReq_Phone *phone = req->add_phones();
phone->set_number(number);
cout << "请输入手机类型(ML:0,TEL:1):";
int type;
cin >> type;
cin.ignore(256, '\n');
phone->set_type((add_contact::AddContactReq_Phone_PhoneType)type);
}
}
void menu()
{
std::cout << "-----------------------------------------------------" << std::endl
<< "--------------- 请选择对通讯录的操作 ----------------" << std::endl
<< "------------------ 1、新增联系⼈ --------------------" << std::endl
<< "------------------ 2、删除联系⼈ --------------------" << std::endl
<< "------------------ 3、查看联系⼈列表 ----------------" << std::endl
<< "------------------ 4、查看联系⼈详细信息 ------------" << std::endl
<< "------------------ 0、退出 --------------------------" << std::endl
<< "-----------------------------------------------------" << std::endl;
}
5. 代码实现服务端
add_contact.proto
syntax="proto3";
package add_contact;
message AddContactReq{
string name = 1 ;
int32 age = 2 ;
message Phone{
string number = 1;
enum PhoneType{
MP=0;
TEL=1;
}
PhoneType type =2;
}
repeated Phone phone = 3;
}
message AddContactResp{
bool success = 1; // 添加联系人是否成功
string error_desc =2 ;// 错误信息
string uid =3 ; // 联系人唯一序号
}
utils.h 工具类文章来源:https://www.toymoban.com/news/detail-518101.html
#include <iostream>
#include <sstream>
#include <random>
namespace Contact_Utils
{
class Utils
{
public:
/// 生成一个唯一标识符,用于赋值uid
static std::string generateUUID(size_t len)
{
// 使用随机数生成器生成随机数种子
std::random_device device;
std::mt19937 generator(device());
// 使用16进制表示的48位的随机数
// std::hex是C++中的一个std::ios_base标志,用于指定输出流以十六进制形式输出整数。当使用该标志时,输出流中的整数将以十六进制表示。例如,当输出整数0x123时,使用std::hex标志会将其输出为字符串"123",而不是"291"。在上述示例代码中,std::hex被用于指定std::ostringstream以十六进制形式输出整数,从而生成16进制表示的48位唯一标识符。
std::ostringstream uuid;
uuid << std::hex;
for (size_t i = 0; i < len; ++i)
{
uuid << (generator() & 0xf);
}
return uuid.str();
}
};
}
main.cc文章来源地址https://www.toymoban.com/news/detail-518101.html
#include "httplib.h"
#include "add_contact.pb.h"
#include "contactException.h"
#include <iostream>
#include <string>
#include"utils.h"
using namespace std;
using namespace httplib;
const string IP = "127.0.0.1";
// const string IP = "0.0.0.0";
// const string IP = "192.139.99.192";
const int PORT = 6666;
void printContact(add_contact::AddContactReq &request);
int main()
{
// 接收请求
Server svr;
// 处理请求
// 注册post的回调函数
svr.Post("/contacts/add", [](const Request &req, Response &resp)
{
cout<<"收到Post请求!"<<endl;
add_contact::AddContactResp response;
add_contact::AddContactReq request;
try
{
if(!request.ParseFromString(req.body)){
throw ContactException("方序序列化失败!");
}
// 持久化联系人
printContact(request);
// 构造 response : res.body
response.set_success(true);
response.set_uid(Contact_Utils::Utils::generateUUID(12));
// 序列化 response
string response_str;
if(!response.SerializePartialToString(&response_str))
{
throw ContactException("序列化失败!");
}
resp.status=200;
resp.body=response_str;
resp.set_header("Content-Type","application/protobuf");
}
catch (ContactException &e)
{
resp.status=500;
response.set_success(false);
response.set_error_desc(e.what());
string response_str;
if(response.SerializePartialToString(&response_str)){
resp.body=response_str;
resp.set_header("Content-Type","application/protobuf");
}
cout<<"/contacts/add 发生异常,异常信息:"<<e.what()<<endl;
} });
// 生成resp,
// 并发送resp,
svr.listen(IP.c_str(), PORT);
}
void printContact(add_contact::AddContactReq &req)
{
cout << "添加联系人成功\n-->姓名:" << req.name() << "-->年龄:" << req.age() << endl;
cout << "-----电话号码-------" << endl;
for (int i = 0; i < req.phone_size(); i++)
{
cout << "电话" << i << ":" << req.phone(i).number() << "(type:" <<req.phone(i).PhoneType_Name(req.phone(i).type())<< ")" << endl;
}
}
到了这里,关于网络通讯录服务器的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!