protobuf c++例子

1, people.proto 

Ruby代码  收藏代码
  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类 

Ruby代码  收藏代码
  1. protoc --cpp_out=. people.proto  
  2. rprotoc people.proto  



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

C++代码  收藏代码
  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 

C++代码  收藏代码
  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. }  



5, Ruby客户端client.rb 

Ruby代码  收藏代码
  1. require 'socket'  
  2. require 'people.pb.rb'  
  3.   
  4. HOST = "localhost"  
  5. PORT = 8888  
  6.   
  7. client = TCPSocket.open(HOST, PORT)  
  8. client.send("GET PEOPLE", 0)  
  9. client.close_write  
  10. s = client.read  
  11. p = Demo::People.new  
  12. p.parse_from_string s  
  13. p p  



6, 使用g++编译 

C++代码  收藏代码
  1. $ g++ server.cc people.pb.cc -o s -lprotobuf  
  2. $ g++ client.cc people.pb.cc -o c -lprotobuf  



7, 运行 

Ruby代码  收藏代码
    1. #启动server  
    2. ./s  
    3.   
    4. You got a message from 127.0.0.1  
    5. Client Message: GET PEOPLE  
    6. You got a message from 127.0.0.1  
    7. Client Message: GET PEOPLE  
    8.   
    9. #运行c++的client  
    10. ./c "GET PEOPLE"  
    11.   
    12. People:  
    13. Name: Hideto  
    14. ID: 123  
    15. Email: hideto.bj@gmail.com  
    16.   
    17.   
    18. #运行Ruby的client  
    19. ruby client.rb  
    20.   
    21. name: "Hideto"  
    22. id: 123  
    23. email: "hideto.bj@gmail.com"  
 
原文地址:https://www.cnblogs.com/ghost240/p/2577054.html