#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. Java:多线程,CyclicBarrier同步器

    1. 背景 CyclicBarrier类,它允许一组线程互相等待,直到到达某个公共屏障点 (common barrier point).在涉及一组固定大小的线程的程序中,这些线程必须不时地互相等待,此 ...

  2. python进程间通信 实例

    python实现进程间通信简单实例 实例讲解了python实现两个程序之间通信的方法,具体方法:该实例采用socket实现,与socket网络编程不一样的是socket.socket(socket.A ...

  3. [svc]tomcat目录结构/虚拟主机/nginx反向代理cache配置

    tomcat目录文件 /usr/local/tomcat/bin/catalina.sh stop sleep 3 /usr/local/tomcat/bin/catalina.sh start to ...

  4. Push UIViewController with different orientation to previous

    转自:http://stackoverflow.com/questions/6695837/push-uiviewcontroller-with-different-orientation-to-pr ...

  5. UIView的alpha、hidden和opaque属性之间的关系和区别

    转自:http://blog.csdn.net/wzzvictory/article/details/10076323 作者:wangzz 原文地址:http://blog.csdn.net/wzzv ...

  6. 重载tostring()方法重要性

    曾经知道继承于object的类都有这个toString方法,重来没在项目中使用过这方法,同一时候也没对该方法进行重载.在如今这个公司上班,才知道了toString方法的重要性. 并且也懂得了重载tos ...

  7. nginx 前端调度 对后端的app的生存状态的检测

    # cat hosts.conf #app调试 upstream gl-appsrv_pools { server 10.1x0.2xx.1x0:8040; server 10.x9x.20.208: ...

  8. 把虚拟教练带回家,「EuMotus」想用AI实现高精度运动反馈系统

    https://36kr.com/p/5089139.html 无需穿戴设备,只需一个红外摄像头和+已安装好EuMotus专利软件的手提电脑 由政府主导的高达2200亿美金的健身与运动支出,15%的健 ...

  9. MATLAB学习之内存溢出的管理方法

    今天用Matlab跑程序,由于数据量太大,又出现 Out of memory. Type HELP MEMORY for your options.的问题.看到这篇文章非常实用,转过来方便查阅~ 用 ...

  10. (转)在Docker中运行Java:为了防止失败,你需要知道这些

    转自:https://mp.weixin.qq.com/s?__biz=MzA5OTAyNzQ2OA==&mid=2649693848&idx=1&sn=4e9ef7e2a9d ...