Google protobuf的安装及使用
- 最近应为工作的需要,合作的部门提供了protobuf的接口,总结了一下使用的过程和方法如下:
- 下载protobuf-2.3.0:
- http://protobuf.googlecode.com/files/protobuf-2.3.0.zip
- 安装:
- unzip protobuf-2.3.0.zip
- cd protobuf-2.3.0
- ./configure
- make
- make check
- make install
- 结果:
- Libraries have been installed in:
- /usr/local/lib
- Head files hava been installed in:
- /usr/local/include/google/
- protobuf/
- 开始写.proto文件:
- BaseMessage.proto:
- message MessageBase
- {
- required int32 opcode = 1;
- // other: sendMgrId, sendId, recvMgrId, recvId, ...
- }
- message BaseMessage
- {
- required MessageBase msgbase = 1;
- }
- BaseMessage.proto是其它消息proto文件的基础,以容器模块的C2S_GetContainerInfo为例:
- ContainerMessage.proto:
- import "BaseMessage.proto";
- message C2SGetContainerInfoMsg
- {
- required MessageBase msgbase = 1;
- optional int32 containerType = 2;
- }
- .proto文件编写规则:
- 1)所有消息都需要包含msgbase这项,并编号都为1,即:
- required MessageBase msgbase = 1;
- 2)除了msgbase这项写成required外,其它所有项都写成optional。
- 编译 .proto 文件
- protoc -I=. --cpp_out=. BaseMessage.proto
- protoc -I=. --cpp_out=. ContainerMessage.proto
- 生成BaseMessage.pb.h、BaseMessage.pb.cc
- ContainerMessage.pb.h、ContainerMessage.pb.cc
- 将它们添加到工程文件中。
- 编写C++代码:
- 1)发送消息:
- C2SGetContainerInfoMsg msg;
- msg.mutable_msgbase()->set_opcode(C2S_GetContainerInfo);
- msg.set_containertype(1);
- std::string out = msg.SerializeAsString();
- send(sockfd, out.c_str(), out.size(), 0);
- 2)接收消息
- char buf[MAXBUF + 1];
- int len;
- bzero(buf, MAXBUF + 1);
- len = recv(new_fd, buf, MAXBUF, 0);
- if (len > 0)
- {
- printf("%d接收消息成功:'%s',共%d个字节的数据/n",
- new_fd, buf, len);
- BaseMessage baseMsg;
- std::string data = buf;
- baseMsg.ParseFromString(data);
- int opcode = baseMsg.mutable_msgbase()->opcode();
- printf("opcode=%d/n", opcode);
- switch (opcode)
- {
- case C2S_GetContainerInfo:
- {
- C2SGetContainerInfoMsg msg;
- msg.ParseFromString(data);
- printf("containerType=%d/n", msg.containertype());
- break;
- }
- default:
- {
- break;
- }
- }
- }
- else
- {
- if (len < 0)
- printf("消息接收失败!错误代码是%d,错误信息是'%s'/n",
- errno, strerror(errno));
- close(new_fd);
- return -1;
- }
- 编译C++代码:
- Need to link lib:
- protobuf
- pthread
- 参考:
- 1,http://www.360doc.com/content/10/0822/16/11586_47942017.shtml
- 2,http://code.google.com/p/protobuf/
原文地址:http://blog.csdn.net/ganghust/article/details/6115283
项目主页:http://code.google.com/p/protobuf/
下载:http://code.google.com/p/protobuf/downloads/list protobuf-2.4.1.tar.gz
1、./configure(注:默认可能会安装在/usr/local目录下,可以加--prefix=/usr来指定安装到/usr/lib下,可以免去路径的设置,路径设置见Linux命令pkg-config)
2、make
3、make check
4、make install(需要超级用户root权限)
Google protobuf的安装及使用的更多相关文章
- google protobuf安装与使用
google protobuf是一个灵活的.高效的用于序列化数据的协议.相比较XML和JSON格式,protobuf更小.更快.更便捷.google protobuf是跨语言的,并且自带了一个编译器( ...
- mac安装protobuf2.4.1时报错./include/gtest/internal/gtest-port.h:428:10: fatal error: 'tr1/tuple' file not found和google/protobuf/message.cc:175:16: error: implicit instantiation of undefined template
通过网上下载的protobuf2.4.1的压缩文件,然后进行安装,./configure和make时遇到了两个问题. 正常的安装步骤如下: ./configure make make check m ...
- caffe安装编译问题-ImportError: No module named google.protobuf.internal
问题描述 ~/Downloads/caffe$ python Python (default, Dec , ::) [GCC ] on linux2 Type "help", &q ...
- GOOGLE PROTOBUF开发者指南
原文地址:http://www.cppblog.com/liquidx/archive/2009/06/23/88366.html 译者: gashero 目录 1 概览 1.1 什么是pro ...
- google protobuf ios开发使用
简介: protobuf 即 google protocol buffer 是一种数据封装格式协议: 比如其他经常用的xml,json等格式:protobuf的优势是效率高,同样的一份数据使用prot ...
- google protobuf使用
下载的是github上的:https://github.com/google/protobuf If you get the source from github, you need to gener ...
- 在C语言环境下使用google protobuf
本文写给经常使用C编程且不喜欢C++而又要经常使用google protobuf的人. 经常写通讯程序的人对数据进行序列化或者反序列化时,可能经常使用google的protobuf(PB ...
- (原)python中import caffe提示no module named google.protobuf.internal
转载请注明出处: http://www.cnblogs.com/darkknightzh/p/5993405.html 之前在一台台式机上在python中使用import caffe时,没有出错.但是 ...
- VS下使用Google Protobuf完成SOCKET通信
如何在Windows环境下的VS中安装使用Google Protobuf完成SOCKET通信 出处:如何在Windows环境下的VS中安装使用Google Protobuf完成SOCKET通信 最近一 ...
随机推荐
- iOS开发——UIWebView
(已写好代码,待空闲更新……)
- css层叠顺序
优先级: 1.添加!important规则的分组享有最高优先级:会将所有不带!important的规则 例如:#id{border:6px solid black!important}高于#id{bo ...
- redis研究之watch
具体的看注释,代码如下: public static void TransTest() { RedisClient client1 = new RedisClient(host, port); Red ...
- PHP pear安装出现 Warning: require_once(Structures/Graph.php)...错误
今天在WINDOWS安装pear,一路无阻很顺利安装完成,接着想安装下pear email包来玩下,但接下来却报: Warning: require_once(Structures/Graph.php ...
- java PropertyChangeSupport委托帧听类的使用
要委托的Bean类写法. import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeListener; import ...
- Java学习笔记(基本数据类型和变量命名规则)
java基本数据类型 变量 1.变量就是可变的量. 2.常量就是不可变的量. 3.字面量:Java的变量和常量中存放的具体的数据成为字面量. 变量 命名规则: (1)首字母是英文字母.$或下划线,由字 ...
- 2、Charm Bracelet( poj 3624)简单0-1背包
题意:有n件手镯,总重量不能超过M,每个手镯有一个体重W[i]和魅力V[i],问在不超过M的情况下能获得的魅力总和 思路:把M当背包总容量,用0-1背包写 代码: #include <iostr ...
- mysql 查找包含特定名字的表
SELECT distinct TABLE_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME LIKE '%medias%'
- js 中对象的特性
用Object.definedproperties 一次性添加或修改多个属性的特性和值. <script> var obj ={} Object.defineProperties(obj, ...
- 支付宝开发(一)-认识php openssl RSA 非对称加密实现
获取支付宝公钥 本地服务器生成私钥和公钥 运用php中openssl相关函数加密解密验证身份 以下是php中openssl相关函数实现的验证,来自php官方demo //需要签名的数据 $data = ...