第一步:安装linux 安装 protobuf (https://github.com/protocolbuffers/protobuf/releases)
第二步:编写protobuf 程序 (test.proto)
syntax = "proto3";
package Test.protobuf;
option optimize_for = SPEED ;
message HeartInfo
{
int32 curtime = 1;
string hostip = 2 ;
}
第三步:编译test.protobuf 得到 test.pb.cc 和 test.pb.h
protoc --cpp_out=./ test.proto
可以用Makefile
CC = g++ -g -D__LINUX__ -Wall -fPIC
AR = ar -r
PROJ_LIB = libTest.a
PROJ_OBJ = Test.pb.o
all:$(PROJ_LIB)
prepare:
protoc -I=./ --cpp_out=./ Test.proto
$(PROJ_LIB):$(PROJ_OBJ)
$(AR) $(PROJ_LIB) $(PROJ_OBJ)
.cc.o:
$(CC) -c $< -o $@
clean:
rm -f *.o
rm -f Test.pb.*
第四步:编写server.cpp,client.cpp (将生成的test.pb.h包含在头文件中)
server.cpp
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <bits/stdc++.h>
#include "test.pb.h"
using namespace std;
using namespace Test::protobuf;
int main(){
//创建套接字
int serv_sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
//将套接字和IP、端口绑定
struct sockaddr_in serv_addr;
memset(&serv_addr, 0, sizeof(serv_addr)); //每个字节都用0填充
serv_addr.sin_family = AF_INET; //使用IPv4地址
serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1"); //具体的IP地址
serv_addr.sin_port = htons(8888); //端口
bind(serv_sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr));
//进入监听状态,等待用户发起请求
listen(serv_sock, 20);
//接收客户端请求
struct sockaddr_in clnt_addr;
socklen_t clnt_addr_size = sizeof(clnt_addr);
int clnt_sock = accept(serv_sock, (struct sockaddr*)&clnt_addr, &clnt_addr_size);
//向客户端发送数据
// char str[100];
// std::cin >> str;
// write(clnt_sock, str, sizeof(str));
char buffer[1000];
HeartInfo serverHI;
read(clnt_sock, buffer, sizeof(buffer) - 1);
if (strlen(buffer) != 0) {
//printf("Message form client: %s\n", buffer);
serverHI.ParseFromArray(buffer, sizeof(buffer));
cout << serverHI.hostip() << endl;
cout << serverHI.curtime() << endl;
}
//关闭套接字
close(clnt_sock);
close(serv_sock);
return 0;
}
client.cpp
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <bits/stdc++.h>
#include "test.pb.h"
using namespace std;
using namespace Test::protobuf;
int main(){
//创建套接字
int sock = socket(AF_INET, SOCK_STREAM, 0);
//向服务器(特定的IP和端口)发起请求
struct sockaddr_in serv_addr;
memset(&serv_addr, 0, sizeof(serv_addr)); //每个字节都用0填充
serv_addr.sin_family = AF_INET; //使用IPv4地址
serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1"); //具体的IP地址
serv_addr.sin_port = htons(8888); //端口
connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr));
//读取服务器传回的数据
char buffer[40];
// read(sock, buffer, sizeof(buffer) - 1);
//
// printf("Message form server: %s\n", buffer);
//
// cin >> buffer;
// write(sock, buffer, sizeof(buffer));
HeartInfo newHI;
int curtime = time(NULL);
newHI.set_curtime(curtime);
newHI.set_hostip("127.0.0.1");
char buff[100];
newHI.SerializeToArray(buff, 100);
write(sock, buff, sizeof(buff));
//关闭套接字
close(sock);
return 0;
}
第五步:编译server.cpp client.cpp程序
g++ -o client client.cpp test.pb.cc -lprotobuf -lpthread
g++ -o server server.cpp test.pb.cc -lprotobuf -lpthread
或者
g++ test.pb.h test.pb.cc server.cpp -o server `pkg-config --cflags --libs protobuf` -lpthread
参考:
protobuf:https://blog.csdn.net/cjf_wei/article/details/52894560
还源:https://blog.csdn.net/xiangxianghehe/article/details/105688062