#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 Concurrency》读书笔记,构建线程安全应用程序

    1. 什么是线程安全性 调用一个函数(假设该函数是正确的)操作某对象常常会使该对象暂时陷入不可用的状态(通常称为不稳定状态),等到操作完全结束,该对象才会重新回到完全可用的状态.如果其他线程企图访问一 ...

  2. Java 里如何实现线程间通信

    正常情况下,每个子线程完成各自的任务就可以结束了.不过有的时候,我们希望多个线程协同工作来完成某个任务,这时就涉及到了线程间通信了. 本文涉及到的知识点:thread.join(), object.w ...

  3. My new English

    作为一个程序员,不管你觉得是高端大气上档次,还是低调奢华有内涵.有一点不能否认,就是英语对于一个爱学习.想不断提高自己的程序员来说是非常重要的.不管是看英文文档,看英文的视频资料,还是想出国工作,与全 ...

  4. [AWS vs Azure] 云计算里AWS和Azure的探究(1)

    转自:http://www.cnblogs.com/hotcan/archive/2013/01/31/2886794.html 云计算里AWS和Azure的探究(1) 全球领先的云的计算平台主要有两 ...

  5. psycopg使用

    1.使用示例 import psycopg2 # 建立数据库连接 conn = psycopg2.connect("dbname=test user=postgres") # 开启 ...

  6. 李洪强iOS经典面试题

    李洪强iOS经典面试题 1. struct和class的区别 swift中,class是引用类型,struct是值类型.值类型在传递和赋值时将进行复制,而引用类型则只会使用引用对象的一个"指 ...

  7. Linux命令格式及帮助命令详解

    昨天看了一个教程,关于Linux命令的,本来以为当是复习随便看看的,结果看了不禁汗颜,这个真挺有学问的,很多东西都是我还不知道的,故此做总结,此文适合于对Linux命令有一定了解但又不是很深入的孩子, ...

  8. 【linux】dpkg info修复及dpkg: warning: files list file for package

    mv /var/lib/dpkg/info /var/lib/dpkg/info.bak //现将info文件夹更名 sudo mkdir /var/lib/dpkg/info //再新建一个新的in ...

  9. PD003-NET通用后台系统

    PD003-NET通用后台系统 开发语言.Net 成品成品 前端技术jquery 数据库sql server .net 通用后台框架   详细信息 基于EF+MVC+Bootstrap构建通用后台管理 ...

  10. SQL Server 数据库自建表

    sysobjects是系统自建的表,里面存储了在数据库内创建的每个对象(约束.默认值.日志.规则.存储过程等). 列名 数据类型 描述 name sysname 对象名 id int 对象标识号 xt ...