protobuf 使用

先安装好protobuf编译器:https://code.google.com/p/protobuf/downloads/list

调用protoc编译器进行编译:protoc x.proto –cpp_out=./oo,请注意需要指出输出文件,C++语言使用–cpp_out指出,其它语言可以查看帮助protoc –help,输出目录oo是必须事先创建好的,否则会报输出目录下存在。

  编译成功后,将在oo目录下生成供应用调用的文件x.pb.h和x.pb.cc,编译自己代码的时候应当将x.pb.cc包含进去。

http://www.cnblogs.com/ghost240/archive/2012/07/04/2577054.html

http://xcd.blog.techweb.com.cn/archives/173.html

1, people.proto 

  1. package demo;  
  2.   
  3. message People {  
  4.   required string name = 1;  
  5.   required int32 id = 2;  
  6.   required string email = 3;  
  7. }  



2, 生成stub类 

  1. protoc --cpp_out=. people.proto  
  2. rprotoc people.proto  



3, C++服务器端server.cc 

  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3. #include <strings.h>  
  4. #include <unistd.h>  
  5. #include <sys/types.h>  
  6. #include <sys/socket.h>  
  7. #include <netinet/in.h>  
  8. #include <arpa/inet.h>  
  9. #include <string>  
  10. #include <iostream>  
  11. #include "people.pb.h"  
  12.   
  13. #define PORT 8888  
  14. #define MAXDATASIZE 20  
  15. #define BACKLOG 10  
  16.   
  17. using namespace std;  
  18.   
  19. int main()  
  20. {  
  21.   int listenfd, connectfd, numbytes;  
  22.   char buf[MAXDATASIZE];  
  23.   struct sockaddr_in server;  
  24.   struct sockaddr_in client;  
  25.   int sin_size;  
  26.   
  27.   listenfd = socket(AF_INET, SOCK_STREAM, 0);  
  28.   
  29.   int opt = SO_REUSEADDR;  
  30.   setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));  
  31.   
  32.   bzero(&server, sizeof(server));  
  33.   server.sin_family = AF_INET;  
  34.   server.sin_port = htons(PORT);  
  35.   server.sin_addr.s_addr = htonl(INADDR_ANY);  
  36.   
  37.   bind(listenfd, (struct sockaddr *)&server, sizeof(struct sockaddr));  
  38.   
  39.   listen(listenfd,BACKLOG);  
  40.   
  41.   while(1){  
  42.     sin_size = sizeof(struct sockaddr_in);  
  43.   
  44.     connectfd = accept(listenfd, (struct sockaddr *)&client, &sin_size);  
  45.   
  46.     numbytes = recv(connectfd, buf, MAXDATASIZE, 0);  
  47.     buf[numbytes] = '\0';  
  48.     string a = buf;  
  49.     cout << "You got a message from " << inet_ntoa(client.sin_addr) << endl;  
  50.     cout << "Client Message: " << a << endl;  
  51.     if(a == "GET PEOPLE") {  
  52.       string data;  
  53.       demo::People p;  
  54.       p.set_name("Hideto");  
  55.       p.set_id(123);  
  56.       p.set_email("hideto.bj@gmail.com");  
  57.       p.SerializeToString(&data);  
  58.       char bts[data.length()];  
  59.       strcpy(bts, data.c_str());  
  60.       send(connectfd, bts, sizeof(bts), 0);  
  61.     } else {  
  62.       send(connectfd, "Fucking client!\n", 16, 0);  
  63.     }  
  64.     close(connectfd);  
  65.   }  
  66.   
  67.   close(listenfd);  
  68.   return 0;  
  69. }  



4, C++客户端client.cc 

  1. #include <stdio.h>  
  2. #include <unistd.h>  
  3. #include <strings.h>  
  4. #include <stdlib.h>  
  5. #include <sys/types.h>  
  6. #include <sys/socket.h>  
  7. #include <netinet/in.h>  
  8. #include <netdb.h>  
  9. #include <string>  
  10. #include <iostream>  
  11. #include "people.pb.h"  
  12.   
  13. #define HOST "localhost"  
  14. #define PORT 8888  
  15. #define MAXDATASIZE 100  
  16.   
  17. using namespace std;  
  18.   
  19. int main(int argc, char ** argv)  
  20. {  
  21.   int fd, numbytes;  
  22.   char buf[MAXDATASIZE];  
  23.   struct hostent *he;  
  24.   struct sockaddr_in server;  
  25.     
  26.   if (argc != 2) {  
  27.     printf("Usage: %s \"COMMAND\"\n",argv[0]);  
  28.     exit(0);  
  29.   }   
  30.     
  31.   he = gethostbyname(HOST);  
  32.   fd = socket(AF_INET, SOCK_STREAM, 0);  
  33.   bzero(&server, sizeof(server));  
  34.   server.sin_family = AF_INET;  
  35.   server.sin_port = htons(PORT);  
  36.   server.sin_addr = *((struct in_addr *)he->h_addr);  
  37.   
  38.   connect(fd, (struct sockaddr *)&server, sizeof(struct sockaddr));  
  39.   
  40.   send(fd, argv[1], 20, 0);  
  41.   
  42.   numbytes = recv(fd, buf, MAXDATASIZE, 0);  
  43.   buf[numbytes] = '\0';  
  44.   string data = buf;  
  45.   demo::People p;  
  46.   p.ParseFromString(data);  
  47.   cout << "People: " << endl;  
  48.   cout << "Name: " << p.name() << endl;  
  49.   cout << "ID: " << p.id() << endl;  
  50.   cout << "Email: " << p.email() << endl;  
  51.   
  52.   close(fd);  
  53.   return 0;  
  54. }
makefile:

target:server client
server:server.cc
g++ -g -o server server.cc people.pb.cc -I. -I/usr/local/protobuf/include -L/usr/local/protobuf/lib -lprotobuf
client:client.cc
g++ -g -o client client.cc people.pb.cc -I. -I/usr/local/protobuf/include -L/usr/local/protobuf/lib -lprotobuf 
clean:
rm -f client server

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值