1. 最近应为工作的需要,合作的部门提供了protobuf的接口,总结了一下使用的过程和方法如下:
  2. 下载protobuf-2.3.0:
  3. http://protobuf.googlecode.com/files/protobuf-2.3.0.zip
  4. 安装:
  5. unzip protobuf-2.3.0.zip
  6. cd protobuf-2.3.0
  7. ./configure
  8. make
  9. make check
  10. make install
  11. 结果:
  12. Libraries have been installed in:
  13. /usr/local/lib
  14. Head files hava been installed in:
  15. /usr/local/include/google/
  16. protobuf/
  17. 开始写.proto文件:
  18. BaseMessage.proto:
  19. message MessageBase
  20. {
  21. required int32 opcode = 1;
  22. // other: sendMgrId, sendId, recvMgrId, recvId, ...
  23. }
  24. message BaseMessage
  25. {
  26. required MessageBase msgbase = 1;
  27. }
  28. BaseMessage.proto是其它消息proto文件的基础,以容器模块的C2S_GetContainerInfo为例:
  29. ContainerMessage.proto:
  30. import "BaseMessage.proto";
  31. message C2SGetContainerInfoMsg
  32. {
  33. required MessageBase msgbase = 1;
  34. optional int32 containerType = 2;
  35. }
  36. .proto文件编写规则:
  37. 1)所有消息都需要包含msgbase这项,并编号都为1,即:
  38. required MessageBase msgbase = 1;
  39. 2)除了msgbase这项写成required外,其它所有项都写成optional。
  40. 编译 .proto 文件
  41. protoc -I=. --cpp_out=. BaseMessage.proto
  42. protoc -I=. --cpp_out=. ContainerMessage.proto
  43. 生成BaseMessage.pb.h、BaseMessage.pb.cc
  44. ContainerMessage.pb.h、ContainerMessage.pb.cc
  45. 将它们添加到工程文件中。
  46. 编写C++代码:
  47. 1)发送消息:
  48. C2SGetContainerInfoMsg msg;
  49. msg.mutable_msgbase()->set_opcode(C2S_GetContainerInfo);
  50. msg.set_containertype(1);
  51. std::string out = msg.SerializeAsString();
  52. send(sockfd, out.c_str(), out.size(), 0);
  53. 2)接收消息
  54. char buf[MAXBUF + 1];
  55. int len;
  56. bzero(buf, MAXBUF + 1);
  57. len = recv(new_fd, buf, MAXBUF, 0);
  58. if (len > 0)
  59. {
  60. printf("%d接收消息成功:'%s',共%d个字节的数据/n",
  61. new_fd, buf, len);
  62. BaseMessage baseMsg;
  63. std::string data = buf;
  64. baseMsg.ParseFromString(data);
  65. int opcode = baseMsg.mutable_msgbase()->opcode();
  66. printf("opcode=%d/n", opcode);
  67. switch (opcode)
  68. {
  69. case C2S_GetContainerInfo:
  70. {
  71. C2SGetContainerInfoMsg msg;
  72. msg.ParseFromString(data);
  73. printf("containerType=%d/n", msg.containertype());
  74. break;
  75. }
  76. default:
  77. {
  78. break;
  79. }
  80. }
  81. }
  82. else
  83. {
  84. if (len < 0)
  85. printf("消息接收失败!错误代码是%d,错误信息是'%s'/n",
  86. errno, strerror(errno));
  87. close(new_fd);
  88. return -1;
  89. }
  90. 编译C++代码:
  91. Need to link lib:
  92. protobuf
  93. pthread
  94. 参考:
  95. 1,http://www.360doc.com/content/10/0822/16/11586_47942017.shtml
  96. 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的安装及使用的更多相关文章

  1. google protobuf安装与使用

    google protobuf是一个灵活的.高效的用于序列化数据的协议.相比较XML和JSON格式,protobuf更小.更快.更便捷.google protobuf是跨语言的,并且自带了一个编译器( ...

  2. 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 ...

  3. caffe安装编译问题-ImportError: No module named google.protobuf.internal

    问题描述 ~/Downloads/caffe$ python Python (default, Dec , ::) [GCC ] on linux2 Type "help", &q ...

  4. GOOGLE PROTOBUF开发者指南

    原文地址:http://www.cppblog.com/liquidx/archive/2009/06/23/88366.html 译者: gashero 目录 1   概览 1.1   什么是pro ...

  5. google protobuf ios开发使用

    简介: protobuf 即 google protocol buffer 是一种数据封装格式协议: 比如其他经常用的xml,json等格式:protobuf的优势是效率高,同样的一份数据使用prot ...

  6. google protobuf使用

    下载的是github上的:https://github.com/google/protobuf If you get the source from github, you need to gener ...

  7. 在C语言环境下使用google protobuf

    本文写给经常使用C编程且不喜欢C++而又要经常使用google protobuf的人.        经常写通讯程序的人对数据进行序列化或者反序列化时,可能经常使用google的protobuf(PB ...

  8. (原)python中import caffe提示no module named google.protobuf.internal

    转载请注明出处: http://www.cnblogs.com/darkknightzh/p/5993405.html 之前在一台台式机上在python中使用import caffe时,没有出错.但是 ...

  9. VS下使用Google Protobuf完成SOCKET通信

    如何在Windows环境下的VS中安装使用Google Protobuf完成SOCKET通信 出处:如何在Windows环境下的VS中安装使用Google Protobuf完成SOCKET通信 最近一 ...

随机推荐

  1. java经验总结二:ORA-08103: 对象不再存在

    问题发生的环境: 在springMvc+mybatis框架中,调用oracle的存储过程时,碰到的一个这样的异常: org.springframework.jdbc.UncategorizedSQLE ...

  2. Swift 学习手记1,pod 的 类库使用

    问题: 在Swift中,我们无法使用像Objective-c 一样的 #import 例如 在头部输入 #import <ReactiveCocoa/ReactiveCocoa.h> 是不 ...

  3. iOS-事务相关

    事务管理 事务(Transaction):1.构成单一逻辑工作单元的操作集合DBMS中的用户程序DBMS外的可执行程序对数据库的读/写操作序列2.读从数据库中读取数据,首先从磁盘中读到内存(Buffe ...

  4. C#2.0至4.0 的一些特性

    罗列清单备查 一.C#2.0 1. Partial class 分部类 file1.cs using System; public partial class MyClass { public voi ...

  5. javascript函数 第14节

    <html> <head> <title>function</title> </head> <body> 1.函数形式<b ...

  6. html页面布局 第8节

    页面布局: <html> <head> <title>页面布局</title> <style type="text/css"& ...

  7. win7下简单FTP服务器搭建

    本文介绍通过win7自带的IIS来搭建一个只能实现基本功能的FTP服务器,第一次装好WIN7后我愣是没整出来,后来查了一下网上资料经过试验后搭建成功,其实原理和步骤与windows前期的版本差不多,主 ...

  8. SQL Function(方法)

    1.为什么有存储过程(procedure)还需要(Function) fun可以再select语句中直接调用,存储过程是不行的. 一般来说,过程显示的业务更为复杂:函数比较有针对性. create f ...

  9. GitHub命令精简教程

    Github其实也可以作为文件分享的地方,但是免费空间只有300M,所以不能存放大文件,否则可以成为一个分享资源的下载站,而且非常方便. 常用命令: git add .   //添加所有的文件到索引 ...

  10. Activity组件的UI实现

    Activity组件的UI实现需要与WindowManagerService服务和SurfaceFlinger服务进行交互 1. Activity组件在启动完成后,会通过一个类型为Session的Bi ...