linux下安裝protobuf教程+示例
1 在網站 http://code.google.com/p/protobuf/downloads/list上可以下載 Protobuf 的原始碼。然後解壓編譯安裝便可以使用它了。
安裝步驟如下所示:
tar -xzf protobuf-2.1.0.tar.gz
cd protobuf-2.1.0
./configure --prefix=/usr/local/protobuf
make
make check
make install
2 > sudo vim /etc/profile
新增
export PATH=$PATH:/usr/local/protobuf/bin/
export PKG_CONFIG_PATH=/usr/local/protobuf/lib/pkgconfig/
儲存執行
source /etc/profile
同時 在~/.profile中新增上面兩行程式碼,否則會出現登入使用者找不到protoc命令
3 > 配置動態連結庫路徑
sudo vim /etc/ld.so.conf
插入:
/usr/local/protobuf/lib
4 > su #root 許可權
ldconfig
- 開始寫.proto檔案:
- BaseMessage.proto:
- message MessageBase
- {
- required int32 opcode = 1;
- // other: sendMgrId, sendId, recvMgrId, recvId, ...
- }
- message BaseMessage
- {
- required MessageBase msgbase = 1;
- }
- BaseMessage.proto是其它訊息proto檔案的基礎,以容器模組的C2S_GetContainerInfo為例:
- ContainerMessage.proto:
- import "BaseMessage.proto";
- message C2SGetContainerInfoMsg
- {
- required MessageBase msgbase = 1;
- optional int32 containerType = 2;
- }
- .proto檔案編寫規則:
- 1)所有訊息都需要包含msgbase這項,並編號都為1,即:
- required MessageBase msgbase = 1;
- 2)除了msgbase這項寫成required外,其它所有項都寫成optional。
- 編譯 .proto 檔案
- protoc -I=. --cpp_out=. BaseMessage.proto
- protoc -I=. --cpp_out=. ContainerMessage.proto
- 生成BaseMessage.pb.h、BaseMessage.pb.cc
- ContainerMessage.pb.h、ContainerMessage.pb.cc
- 將它們新增到工程檔案中。
- 編寫C++程式碼:
- 1)傳送訊息:
- C2SGetContainerInfoMsg msg;
- msg.mutable_msgbase()->set_opcode(C2S_GetContainerInfo);
- msg.set_containertype(1);
- std::string out = msg.SerializeAsString();
- send(sockfd, out.c_str(), out.size(), 0);
- 2)接收訊息
- char buf[MAXBUF + 1];
- int len;
- bzero(buf, MAXBUF + 1);
- len = recv(new_fd, buf, MAXBUF, 0);
- if (len > 0)
- {
- printf("%d接收訊息成功:'%s',共%d個位元組的資料/n",
- new_fd, buf, len);
- BaseMessage baseMsg;
- std::string data = buf;
- baseMsg.ParseFromString(data);
- int opcode = baseMsg.mutable_msgbase()->opcode();
- printf("opcode=%d/n", opcode);
- switch (opcode)
- {
- case C2S_GetContainerInfo:
- {
- C2SGetContainerInfoMsg msg;
- msg.ParseFromString(data);
- printf("containerType=%d/n", msg.containertype());
- break;
- }
- default:
- {
- break;
- }
- }
- }
- else
- {
- if (len < 0)
- printf("訊息接收失敗!錯誤程式碼是%d,錯誤資訊是'%s'/n",
- errno, strerror(errno));
- close(new_fd);
- return -1;
- }
- 編譯C++程式碼:
- Need to link lib:
- protobuf
- pthread
- 參考:
- 1,http://www.360doc.com/content/10/0822/16/11586_47942017.shtml
- 2,http://code.google.com/p/protobuf/
Server:
-
#include "stdafx.h"
-
#include <stdio.h>
-
#include <Winsock2.h>
-
#include <windows.h>
-
#include <string>
-
-
#include "msg.pb.h"
-
-
#pragma comment(lib,"ws2_32.lib")
-
#pragma commen (lib,"libprotobuf.lib")
-
-
using namespace std;
-
int _tmain(int argc, _TCHAR* argv[])
-
{
-
WORD wVersionRequested;
-
WSADATA wsaData;
-
int err;
-
-
wVersionRequested = MAKEWORD(1,1);
-
err = WSAStartup(wVersionRequested,&wsaData);
-
if ( err != 0)
-
return 0;
-
-
if ( LOBYTE( wsaData.wVersion) != 1 || HIBYTE(wsaData.wVersion) != 1)
-
{
-
WSACleanup();
-
return 0;
-
}
-
-
SOCKET sockSrv = socket(AF_INET,SOCK_STREAM,0);
-
-
SOCKADDR_IN addrSrv;
-
addrSrv.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
-
addrSrv.sin_family = AF_INET;
-
addrSrv.sin_port = htons(8000);
-
-
bind(sockSrv,(SOCKADDR*)&addrSrv,sizeof(SOCKADDR));
-
printf("Bind OK...\n");
-
-
listen(sockSrv,5);
-
printf("Listen OK ...\n");
-
-
SOCKADDR_IN addrClient;
-
int len = sizeof(SOCKADDR);
-
-
while(1)
-
{
-
SOCKET sockConn = accept(sockSrv,(SOCKADDR*)&addrClient,&len);
-
printf("Receive data...\n");
-
-
char recvBuf[500];
-
memset(recvBuf,0,500);
-
recv(sockConn, recvBuf, 500, 0);
-
-
test::Vo_CharacterInfo Info;
-
Info.ParseFromString(string(recvBuf);
-
closesocket(sockConn);
-
printf("Close Socket...\n");
-
}
-
-
system("pause");
-
return 0;
- }
Client:
-
// Client.cpp : 定義控制檯應用程式的入口點。
-
//
-
-
#include "stdafx.h"
-
#include "msg.pb.h"
-
#include <stdio.h>
-
#include <windows.h>
-
#include <fstream>
-
#include <string>
-
-
#pragma comment(lib,"ws2_32.lib")
-
#pragma comment(lib,"libprotobuf.lib")
-
-
using namespace std;
-
-
int _tmain(int argc, _TCHAR* argv[])
-
{
-
WORD wVersionRequested;
-
WSADATA wsaData;
-
int err;
-
wVersionRequested = MAKEWORD(1,1);
-
-
err = WSAStartup( wVersionRequested, &wsaData);
-
if ( err != 0)
-
{
-
return 0;
-
}
-
if ( LOBYTE(wsaData.wVersion)!=1 || HIBYTE(wsaData.wVersion != 1))
-
{
-
WSACleanup();
-
return 0;
-
}
-
-
SOCKET sockClient = socket(AF_INET,SOCK_STREAM,0);
-
-
SOCKADDR_IN addrSrv;
-
addrSrv.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
-
addrSrv.sin_family = AF_INET;
-
addrSrv.sin_port = htons(8000);
-
-
connect(sockClient,(SOCKADDR*)&addrSrv,sizeof(SOCKADDR));
-
printf("Connect Successful...\n");
-
test::Vo_CharacterInfo Info;
Info.set_charid(123);
- Info.set_charname("name");
std::string info;
Info.SerializeToString(&info);
send(sockClient, info.data(), info.size(),0);
printf("Send Successful...\n");
closesocket(sockClient);
WSACleanup();
system("pause");
return 0;
}
相關文章
- protobuf安裝-Linux篇Linux
- Ubuntu下 解除安裝protobuf並安裝指定版本的protobufUbuntu
- Linux下安裝svn教程Linux
- Linux下怎麼安裝.deb格式的安裝包?Linux系統下.deb格式安裝包的安裝教程Linux
- Linux系統下安裝使用anaconda教程。Linux
- 在Linux下安裝配置Cntlm代理教程Linux
- python 安裝protobufPython
- wsl中ubuntu20.04下安裝google protobufUbuntuGo
- Linux 環境下如何安裝部署 RocketMQ 教程LinuxMQ
- ADS安裝教程(Linux)Linux
- Linux Debian安裝教程Linux
- Parrot Linux安裝教程Linux
- Linux的安裝教程Linux
- LevelDB C++教程: Linux下編譯與安裝C++Linux編譯
- linux下安裝snap安裝工具Linux
- Windows10 安裝 protobufWindows
- centos7安裝protobufCentOS
- Linux Ubuntu安裝配置教程LinuxUbuntu
- linux 下安裝 jdkLinuxJDK
- linux下安裝dockerLinuxDocker
- linux下nginx安裝LinuxNginx
- Linux下安裝OpenCVLinuxOpenCV
- Linux下安裝nginxLinuxNginx
- linux下安裝elasticsearchLinuxElasticsearch
- Linux 下安裝 RedisLinuxRedis
- Linux下安裝RedisLinuxRedis
- InfluxDB Linux 下安裝Linux
- mysql linux下安裝MySqlLinux
- Linux下安裝RabbitMQLinuxMQ
- Linux下安裝pymysqlLinuxMySql
- Linux下安裝jiebaLinuxJieba
- Linux下安裝GensimLinux
- Linux下SuperLU安裝Linux
- Linux下安裝GitLinuxGit
- linux下安裝pythonLinuxPython
- Linux 下安裝 ComposerLinux
- Linux下expect安裝Linux
- win10下linux kali系統安裝教程_win10下linux kali系統安裝步驟【圖文】Win10Linux
- c++ protobuf安裝記錄C++