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;
}
5, Ruby客户端client.rb
require 'socket'
require 'people.pb.rb'
HOST = "localhost"
PORT = 8888
client = TCPSocket.open(HOST, PORT)
client.send("GET PEOPLE", 0)
client.close_write
s = client.read
p = Demo::People.new
p.parse_from_string s
p p
6, 使用g++编译
$ g++ server.cc people.pb.cc -o s -lprotobuf
$ g++ client.cc people.pb.cc -o c -lprotobuf
7, 运行
#启动server
./s
You got a message from 127.0.0.1
Client Message: GET PEOPLE
You got a message from 127.0.0.1
Client Message: GET PEOPLE
#运行c++的client
./c "GET PEOPLE"
People:
Name: Hideto
ID: 123
Email: hideto.bj@gmail.com
#运行Ruby的client
ruby client.rb
name: "Hideto"
id: 123
email: "hideto.bj@gmail.com"
分享到:
相关推荐
基于C++实现使用protobuf完成网络通信源码.zip基于C++实现使用protobuf完成网络通信源码.zip基于C++实现使用protobuf完成网络通信源码.zip基于C++实现使用protobuf完成网络通信源码.zip基于C++实现使用protobuf完成...
unity3d应用Protobuf实现Socket通信
NULL 博文链接:https://gqdy365.iteye.com/blog/2163076
整合Unity+Protobuf+Socket的一个游戏案例,里面包括服务器和客户端各一个socket,通过线程管理来保证多个连接请求同步。案例简单,但在实际开发中很常用。通过稍微修改,就可以用在实际项目里。编程语言是C#,都加了...
protobuf 在socket中运用 客户端 TestProtocolBufers电话薄例子 protobuf 源码库文件需要考到下面目录ClientTest
一个c++使用protobuf作为消息协议的一个小demo,从这个demo里你可以很好地理解进行socket编程中的数据包的设计以及数据的打包和解包。
获取protobuf源代码,使用cmake生成protobuf的Visual Studio工程,在UE4中使用protobuf进行网络通信,包含服务端与客户端的socket实现,以及数据的粘包和断包
测试脚本: Client.cs 测试内容: 1.连接服务器。 2.断开服务器。 3.发送游戏事件。...(注意:.Net 2.0 的库和 .Net 2.0 Subset的选择使用。代码中只需要维护 SocketManager.cs 中的两个静态序列化相关函数即可)
使用protobuf如何开发socket,描述如何使用proto文件,如何在socket中应用
Protocol Buffers(protobuf)是Google提供的一个开源序列化框架,这个demo将演示如何在Windows下VC++使用protobuf 配套文章地址:http://blog.csdn.net/mycwq/article/details/17606527
当时研究了一段时间,拿出来给大家分享! google protobuf 生成c++ c#无障碍通信环境配置及脚本
C++_protobuf 生成C++用的protobuf
基于C++ module库 Protobuf Zookeeper 实现的Rpc框架.zip基于C++ module库 Protobuf Zookeeper 实现的Rpc框架.zip基于C++ module库 Protobuf Zookeeper 实现的Rpc框架.zip基于C++ module库 Protobuf Zookeeper 实现...
使用google protobuf通过TCP传输文件
通过c++服务器和.net客户端的数据通信,详细讲解protobuf(含代码)
google protobuf c++使用手册,protobuf 的类型定义,及简单使用
protobuf 命名空间,包
VS版本:VS2019 ProtoBuffer版本:protobuf-3.8.0 C++最简VS工程 已生成Protoc和lib,提出头文件引用,所有引用使用安全的相对路径