Sword protobuf学习四
#include <iostream>
#include <string>
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <netinet/in.h>
#include <arpa/inet.h> #include "test.pb.h" /* 这是proto文件生成的头文件 */ #include "include/google/protobuf/map.h" /* protobuf提供的map数据结构 */ using namespace std; void dosome(int client_fd)
{
ssize_t len = ;
int datelen = ;
//char buf[2048] = { 0 };
//接收消息
len = recv(client_fd, (void *)&datelen, sizeof(int), );
if (len <= )
{
printf("recv() failed and len[%d] errno[%s]!\n",(int)len,strerror(errno));
return;
}
if (len != sizeof(int))
{
printf("data error !\n");
return;
}
datelen = ntohl(datelen);
printf("date len is[%d]\n",datelen);
char *buf = (char *)malloc(datelen);
memset(buf, , datelen);
char *pcIndex = buf;
int reallen = ;
char gcTemp[] = { };
//获取消息长度
while (true)
{
memset(gcTemp, , );
len = recv(client_fd, gcTemp, , );
if (len <= )
{
printf("peer is closed !\n");
return;
}
memcpy(pcIndex + reallen, gcTemp, len);
reallen += len; if (datelen <= reallen)
{
printf("recv real data is [%d] \n", reallen);
break;
}
} commun::Say msg;
//反序列化数据
msg.ParseFromArray(buf, datelen); //获取基本类型数据方法
cout << msg.data() << endl; //获取二进制文件数据
char *pcFileData = (char *)malloc(msg.filesize());
if (pcFileData)
{
memcpy(pcFileData, msg.audio().c_str(), msg.filesize());
FILE *pfw = fopen("11.wav", "w");
fwrite(pcFileData, msg.filesize(), , pfw);
fclose(pfw);
cout << "file size is " << msg.filesize() << endl;
cout << "write file ok" << endl;
} cout << "show name" << endl; //数组类型变量的迭代器遍历--当前数组类型也可以下标遍历,但是怕出现越界错误,不使用
google::protobuf::internal::RepeatedPtrIterator<const string> it2;
it2 = msg.name().begin();
for (it2 = msg.name().begin(); it2 != msg.name().end(); ++it2)
{
cout << "name is " << *it2 << endl;
} cout << "show map" << endl; //map类型变量的迭代器遍历
google::protobuf::Map<google::protobuf::int32, string>::const_iterator it; for (it = msg.projects().begin(); it != msg.projects().end(); ++it)
{
cout << "key is " << it->first << " and value is " << it->second << endl;
} } int server_listen()
{
int fd = ;
struct sockaddr_in addr;
struct sockaddr_in peer_addr;
int peer_fd = ;
socklen_t peeer_len = sizeof(struct sockaddr_in); fd = socket(AF_INET, SOCK_STREAM, );
if (fd < )
{
printf("socket() failed \n");
return -;
} {
int on = ;
if (setsockopt(fd,SOL_SOCKET,SO_REUSEADDR,&on,sizeof(on)))
{
printf("setsockopt() failed \n");
return -;
}
}
memset(&addr, , sizeof(struct sockaddr_in));
addr.sin_family = AF_INET;
addr.sin_port = htons();
addr.sin_addr.s_addr = htonl(INADDR_ANY); if (bind(fd, (struct sockaddr *)&addr, sizeof(struct sockaddr_in)))
{
printf("bind() failed \n");
return -;
} if (listen(fd,SOMAXCONN))
{
printf("listen() failed \n");
return -;
} peer_fd = accept(fd, (struct sockaddr *)&peer_addr, &peeer_len);
if (peer_fd < )
{
printf("listen() failed \n");
return -;
} while (true)
{
dosome(peer_fd);
sleep();
} close(fd);
close(peer_fd); return ; } int main()
{
server_listen();
return ;
}
Sword protobuf学习四的更多相关文章
- Sword protobuf学习三
#include <iostream> #include <sys/types.h> /* See NOTES */ #include <sys/socket.h> ...
- Sword protobuf学习二
编写protobuf消息文件 文件格式: xxx.proto //标明使用哪个版本的protobuf,默认2.0版本 syntax = "proto3"; //类似于c++中的na ...
- Sword protobuf学习一
protobuf简介 Protocol Buffers,是Google公司开发的一种数据描述语言,类似于XML能够将结构化数据序列化,可用于数据存储.通信协议等方面. 它不依赖于语言和平台并且可扩展性 ...
- TweenMax动画库学习(四)
目录 TweenMax动画库学习(一) TweenMax动画库学习(二) TweenMax动画库学习(三) Tw ...
- SVG 学习<四> 基础API
目录 SVG 学习<一>基础图形及线段 SVG 学习<二>进阶 SVG世界,视野,视窗 stroke属性 svg分组 SVG 学习<三>渐变 SVG 学习<四 ...
- Android JNI学习(四)——JNI的常用方法的中文API
本系列文章如下: Android JNI(一)——NDK与JNI基础 Android JNI学习(二)——实战JNI之“hello world” Android JNI学习(三)——Java与Nati ...
- SCARA——OpenGL入门学习四(颜色)
OpenGL入门学习[四] 本次学习的是颜色的选择.终于要走出黑白的世界了~~ OpenGL支持两种颜色模式:一种是RGBA,一种是颜色索引模式. 无论哪种颜色模式,计算机都必须为每一个像素保存一些数 ...
- ZigBee学习四 无线+UART通信
ZigBee学习四 无线+UART通信 1) 协调器编程 修改coordinator.c文件 byte GenericApp_TransID; // This is the unique messag ...
- (转)SpringMVC学习(四)——Spring、MyBatis和SpringMVC的整合
http://blog.csdn.net/yerenyuan_pku/article/details/72231763 之前我整合了Spring和MyBatis这两个框架,不会的可以看我的文章MyBa ...
随机推荐
- Android设计元素-操作栏
原文链接:http://android.eoe.cn/topic/android_sdk 操作栏 - Action Bar “操作栏”对于 Android 应用来说是最重要的设计元素.它通常在应用运行 ...
- 【Android开发】Android应用程序目录结构
原文:http://android.eoe.cn/topic/summary Android开发之旅:组件生命周期吴秦 Android开发之旅:HelloWorld项目的目录结构 * HelloWor ...
- oracle11g exp导出问题:部分表导不出来
在oracle导出表的命令exp时候发现一个问题,就是部分表全然的导不出来,经检查发现仅仅要是表为空的都会导不出来. 在例如以下表中发现segment_created都为NO的是导不出来的,经查询后, ...
- golang gc 优化思路以及实例分析
一个即将上线的go 写的高频服务,压测的时候发现 gc 特别高,高到10%-15% 左右了,本文记录下优化 gc 的过程和和思路.线上环境1.10. 首先,查看gc 是否有异常,我们可以使用 gctr ...
- hibernate的flush()、refresh()、clear()针对一级缓存的操作的区别
首先session是有一级缓存的,目的是为了减少查询数据库的时间,提高效率,一级缓存的生命周期和session是一样的, session.flush()和session.clear()就针对sessi ...
- 【转】【WebStorm】利用WebStorm来管理你的Github
用webstorm上传代码时,首先要先下载git,网址一搜就可以搜到,然后开始配置webstorm,打开webstorm,在file-settings中直接搜索github,然后输入自己github的 ...
- LeetCode: Partition List 解题报告
Partition List Given a linked list and a value x, partition it such that all nodes less than x come ...
- Flask简介,安装,demo,快速入门
1.Flask简介 Flask是一个相对于Django而言轻量级的Web框架. 和Django大包大揽不同,Flask建立于一系列的开源软件包之上,这其中 最主要的是WSGI应用开发库Werkzeug ...
- java基础篇---I/O技术(二)
接着上篇http://www.cnblogs.com/oumyye/p/4314412.html java I/O流---内存操作流 ByteArrayInputStream和ByteArrayOut ...
- java基础篇---注解(Annotation)
一.概念 Annontation是Java5开始引入的新特征.中文名称一般叫注解.它提供了一种安全的类似注释的机制,用来将任何的信息或元数据(metadata)与程序元素(类.方法.成员变量等)进行关 ...