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 ...
随机推荐
- error_page 改变状态码为新的状态码,并显示指定内容
server { listen 80; server_name www.espressos.com; location / { root /data0/www/bbs; index index.htm ...
- cmd命令操作Oracle数据库
//注意cmd命令执行的密码字符不能过于复杂 不能带有特殊符号 以免执行不通过 譬如有!@#¥%……&*之类的 所以在Oracle数据库设置密码是不要太复杂 /String Database ...
- Quartz.Net定时任务EF+MVC版的web服务
之前项目采用JAVA 的 Quartz 进行定时服调度务处理程序,目前在.NET下面使用依然可以完成相同的工作任务,其实什么语言不重要,关键是我们要学会利用语言实现价值.它是一个简单的执行任务计划的组 ...
- 【Java】Java复习笔记-三大排序算法,堆栈队列,生成无重复的随机数列
冒泡排序 package com.lcw.bubble; public class BubbleSort { /** * 冒泡排序 * @param args * @author 成鹏致远 */ pu ...
- 【驱动】linux设备驱动·扫盲
linux设备驱动 Linux系统把设备驱动分成字符设备.块设备和网络设备三种类型. 内核为设备驱动提供了注册和管理的接口,设备驱动还可以使用内核提供的其他功能以及访问内核资源. PCI局部总线 早期 ...
- FIDDLER的使用方法及技巧总结(连载一)FIDDLER快速入门及使用场景
FIDDLER的使用方法及技巧总结 一.FIDDLER快速入门及使用场景 Fiddler的官方网站:http://www.fiddler2.com Fiddler的官方帮助:http://docs.t ...
- 【转】asp.net中@page指令的属性Inherits、Src、CodeBehind区别
Inherits.Src.CodeBehind 在 ASP.NET 中使用代码隐藏方法来设计Web 窗体,可使页代码能够更清晰地从 HTML 内容中分离到完全单独的文件中. 通常一个 @page 指令 ...
- MDK 的编译过程及文件类型全解
MDK 的编译过程及文件类型全解 ------(在arm9的开发中,这些东西都是我们自己搞定的,但是在windows上,IDE帮我们做好了,了解这些对深入开发是很有帮助的,在有arm9开发的基础上,下 ...
- Excel中功能清单转Project任务
接:http://www.cnblogs.com/vipsoft/p/8495220.html 1.在E列输入公式: =IF(LEN(A2)>1,1,IF(LEN(B2)>1,2,IF(L ...
- Eigen教程(6)
整理下Eigen库的教程,参考:http://eigen.tuxfamily.org/dox/index.html 高级初始化方法 本篇介绍几种高级的矩阵初始化方法,重点介绍逗号初始化和特殊矩阵(单位 ...