赞
踩
安装
https://www.jianshu.com/p/ea1cdd33f2b3
也可
sudo apt-get install protobuf-c-compiler protobuf-compiler
protoc --version
msg.proto
syntax = "proto3";
package tutorial;
message Person {
string name = 1;
int32 id = 2; // Unique ID number for this person.
string email = 3;
}
编译
/usr/local/bin/protoc -I=./ --cpp_out=./ msg.proto
生成
msg.pb.h,msg.pb.cc
server.cpp
使用 ParseFromString 接口
#include<stdio.h> #include<stdlib.h> #include<sys/socket.h> #include<arpa/inet.h> #include<unistd.h> #include<string.h> #include<string> #include<iostream> #include"msg.pb.h" using namespace std; #define SIZE 300 int main(int argc,char *argv[]) { int server_sock,client_sock,r; struct sockaddr_in server_addr,client_addr; socklen_t len; char buf[SIZE]; if(argc!=2) { printf("need port\n"); return 1; } int port = atoi(argv[1]); server_sock = socket(AF_INET,SOCK_STREAM,0); memset(&server_addr,0,sizeof server_addr); server_addr.sin_family=AF_INET; server_addr.sin_addr.s_addr=inet_addr("0.0.0.0");//htonl(INADDR_ANY) server_addr.sin_port=htons(port)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。