#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学习四的更多相关文章

  1. Sword protobuf学习三

    #include <iostream> #include <sys/types.h> /* See NOTES */ #include <sys/socket.h> ...

  2. Sword protobuf学习二

    编写protobuf消息文件 文件格式: xxx.proto //标明使用哪个版本的protobuf,默认2.0版本 syntax = "proto3"; //类似于c++中的na ...

  3. Sword protobuf学习一

    protobuf简介 Protocol Buffers,是Google公司开发的一种数据描述语言,类似于XML能够将结构化数据序列化,可用于数据存储.通信协议等方面. 它不依赖于语言和平台并且可扩展性 ...

  4. TweenMax动画库学习(四)

    目录            TweenMax动画库学习(一)            TweenMax动画库学习(二)            TweenMax动画库学习(三)            Tw ...

  5. SVG 学习<四> 基础API

    目录 SVG 学习<一>基础图形及线段 SVG 学习<二>进阶 SVG世界,视野,视窗 stroke属性 svg分组 SVG 学习<三>渐变 SVG 学习<四 ...

  6. Android JNI学习(四)——JNI的常用方法的中文API

    本系列文章如下: Android JNI(一)——NDK与JNI基础 Android JNI学习(二)——实战JNI之“hello world” Android JNI学习(三)——Java与Nati ...

  7. SCARA——OpenGL入门学习四(颜色)

    OpenGL入门学习[四] 本次学习的是颜色的选择.终于要走出黑白的世界了~~ OpenGL支持两种颜色模式:一种是RGBA,一种是颜色索引模式. 无论哪种颜色模式,计算机都必须为每一个像素保存一些数 ...

  8. ZigBee学习四 无线+UART通信

    ZigBee学习四 无线+UART通信 1) 协调器编程 修改coordinator.c文件 byte GenericApp_TransID; // This is the unique messag ...

  9. (转)SpringMVC学习(四)——Spring、MyBatis和SpringMVC的整合

    http://blog.csdn.net/yerenyuan_pku/article/details/72231763 之前我整合了Spring和MyBatis这两个框架,不会的可以看我的文章MyBa ...

随机推荐

  1. SharePreference 注册 registerOnSharedPreferenceChangeListener 无法回调的问题

    以前一直没有用过 registerOnSharedPreferenceChangeListener 回调方法,今天用到了,就设置了下,结果发现不起作用,因为一直没有回调. 代码: mSp = this ...

  2. 为你的网站加上SSL,可以使用HTTPS进行访问

    首先,我们使用的是nginx 将域名证书文件1_www.domain.com_bundle.crt .私钥文件2_www.domain.com.key保存到同一个目录,例如/usr/local/ngi ...

  3. 【Unity】7.5 移动设备输入

    分类:Unity.C#.VS2015 创建日期:2016-04-21 一.简介 在iOS和Android系统中,操作都是通过触摸来完成的.Input类中对触摸操作的方法或变量如下图所示: 通过GetT ...

  4. 【Unity】2.5 场景视图(Scene)

    分类:Unity.C#.VS2015 创建日期:2016-03-29 一.场景视图(Scene View)导航 场景视图 (Scene View) 是你的交互式沙箱.你可以使用场景视图 (Scene ...

  5. Retina屏的移动设备如何实现真正1px的线

    前些日子总被人问起 iOS Retina 屏,设置 1px 边框,实际显示 2px,如何解决?原来一直没在意,源于自己根本不是像素眼……今天仔细瞅了瞅原生实现的边框和CSS设置的边框,确实差距不小…… ...

  6. 压力测试工具ab及centos下单独安装方法 nginx和tomcat静态资源的性能测试

    Apache安装包中自带的压力测试工具Apache Benchmark(简称ab)简单易用,这里采用ab作为压国测试工具. 独立安装: ab运行需要信赖apr-util包: # yum install ...

  7. 沐雪多用户微信公众平台开发源码,商城小程序源码(2018年最新的asp.net C# 微信源码,小程序源码)

    现售价5400元,就可以搭建自己的微信平台啦 购买地址:https://item.taobao.com/item.htm?id=539102325336 该系统是由[上海沐雪网络]独家授权销售,其他地 ...

  8. [CoreOS 转载] CoreOS实践指南(三):系统服务管家Systemd

    转载:http://www.csdn.net/article/2015-01-08/2823477 摘要:CoreOS是采用了高度精简的系统内核及外围定制的操作系统.ThoughtWorks的软件工程 ...

  9. 图解zookeeper FastLeader选举算法【转】

    转自:http://codemacro.com/2014/10/19/zk-fastleaderelection/ zookeeper配置为集群模式时,在启动或异常情况时会选举出一个实例作为Leade ...

  10. Android.mk高级写法

    转:http://blog.csdn.net/langresser_king/article/details/8275291 原本只是想记录一些常用的使用技巧,但是越写越得意(>_<),忍 ...