先安装好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.htmlhttp://xcd.blog.techweb.com.cn/archives/173.html
1, people.proto
- package demo;
- message People {
- required string name = 1;
- required int32 id = 2;
- required string email = 3;
- }
2, 生成stub类
- protoc --cpp_out=. people.proto
- rprotoc people.proto
3, C++服务器端server.cc
- #include <stdio.h>
- #include <stdlib.h>
- #include <strings.h>
- #include <unistd.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <arpa/inet.h>
- #include <string>
- #include <iostream>
- #include "people.pb.h"
- #define PORT 8888
- #define MAXDATASIZE 20
- #define BACKLOG 10
- using namespace std;
- int main()
- {
- int listenfd, connectfd, numbytes;
- char buf[MAXDATASIZE];
- struct sockaddr_in server;
- struct sockaddr_in client;
- int sin_size;
- listenfd = socket(AF_INET, SOCK_STREAM, 0);
- int opt = SO_REUSEADDR;
- setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
- bzero(&server, sizeof(server));
- server.sin_family = AF_INET;
- server.sin_port = htons(PORT);
- server.sin_addr.s_addr = htonl(INADDR_ANY);
- bind(listenfd, (struct sockaddr *)&server, sizeof(struct sockaddr));
- listen(listenfd,BACKLOG);
- while(1){
- sin_size = sizeof(struct sockaddr_in);
- connectfd = accept(listenfd, (struct sockaddr *)&client, &sin_size);
- numbytes = recv(connectfd, buf, MAXDATASIZE, 0);
- buf[numbytes] = '\0';
- string a = buf;
- cout << "You got a message from " << inet_ntoa(client.sin_addr) << endl;
- cout << "Client Message: " << a << endl;
- if(a == "GET PEOPLE") {
- string data;
- demo::People p;
- p.set_name("Hideto");
- p.set_id(123);
- p.set_email("hideto.bj@gmail.com");
- p.SerializeToString(&data);
- char bts[data.length()];
- strcpy(bts, data.c_str());
- send(connectfd, bts, sizeof(bts), 0);
- } else {
- send(connectfd, "Fucking client!\n", 16, 0);
- }
- close(connectfd);
- }
- close(listenfd);
- return 0;
- }
4, C++客户端client.cc
- #include <stdio.h>
- #include <unistd.h>
- #include <strings.h>
- #include <stdlib.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <netdb.h>
- #include <string>
- #include <iostream>
- #include "people.pb.h"
- #define HOST "localhost"
- #define PORT 8888
- #define MAXDATASIZE 100
- using namespace std;
- int main(int argc, char ** argv)
- {
- int fd, numbytes;
- char buf[MAXDATASIZE];
- struct hostent *he;
- struct sockaddr_in server;
- if (argc != 2) {
- printf("Usage: %s \"COMMAND\"\n",argv[0]);
- exit(0);
- }
- he = gethostbyname(HOST);
- fd = socket(AF_INET, SOCK_STREAM, 0);
- bzero(&server, sizeof(server));
- server.sin_family = AF_INET;
- server.sin_port = htons(PORT);
- server.sin_addr = *((struct in_addr *)he->h_addr);
- connect(fd, (struct sockaddr *)&server, sizeof(struct sockaddr));
- send(fd, argv[1], 20, 0);
- numbytes = recv(fd, buf, MAXDATASIZE, 0);
- buf[numbytes] = '\0';
- string data = buf;
- demo::People p;
- p.ParseFromString(data);
- cout << "People: " << endl;
- cout << "Name: " << p.name() << endl;
- cout << "ID: " << p.id() << endl;
- cout << "Email: " << p.email() << endl;
- close(fd);
- return 0;
- }
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