• 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 = 0;
        int datelen = 0;
        //char buf[2048] = { 0 };
        //接收消息
        len = recv(client_fd, (void *)&datelen, sizeof(int), 0);
        if (len <= 0)
        {
            printf("recv() failed and len[%d] errno[%s]!
    ",(int)len,strerror(errno));
            return;
        }
        if (len != sizeof(int))
        {
            printf("data error !
    ");
            return;
        }
        datelen = ntohl(datelen);
        printf("date len is[%d]
    ",datelen);
        char *buf = (char *)malloc(datelen);
        memset(buf, 0, datelen);
        char *pcIndex = buf;
        int reallen = 0;
        char gcTemp[1024] = { 0 };
        //获取消息长度
        while (true)
        {
            memset(gcTemp, 0, 1024);
            len = recv(client_fd, gcTemp, 1024, 0);
            if (len <= 0)
            {
                printf("peer is closed !
    ");
                return;
            }
            memcpy(pcIndex + reallen, gcTemp, len);
            reallen += len;
    
            if (datelen <= reallen)
            {
                printf("recv real data is [%d] 
    ", 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(), 1, 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 = 0;
        struct sockaddr_in addr;
        struct sockaddr_in peer_addr;
        int peer_fd = 0;
        socklen_t peeer_len = sizeof(struct sockaddr_in);
    
        fd = socket(AF_INET, SOCK_STREAM, 0);
        if (fd < 0)
        {
            printf("socket() failed 
    ");
            return -1;
        }
    
        {
            int on = 1;
            if (setsockopt(fd,SOL_SOCKET,SO_REUSEADDR,&on,sizeof(on)))
            {
                printf("setsockopt() failed 
    ");
                return -1;
            }
        }
        memset(&addr, 0, sizeof(struct sockaddr_in));
        addr.sin_family = AF_INET;
        addr.sin_port = htons(8888);
        addr.sin_addr.s_addr = htonl(INADDR_ANY);
    
        if (bind(fd, (struct sockaddr *)&addr, sizeof(struct sockaddr_in)))
        {
            printf("bind() failed 
    ");
            return -1;
        }
    
        if (listen(fd,SOMAXCONN))
        {
            printf("listen() failed 
    ");
            return -1;
        }
    
        peer_fd = accept(fd, (struct sockaddr *)&peer_addr, &peeer_len);
        if (peer_fd < 0)
        {
            printf("listen() failed 
    ");
            return -1;
        }
    
        while (true)
        {
            dosome(peer_fd);
            sleep(3);
        }
    
        close(fd);
        close(peer_fd);
    
        return 0;
    
    }
    
    int main()
    {
        server_listen();
        return 0;
    }
  • 相关阅读:
    Java第三方工具库/包汇总
    龙果开源支付系统介绍
    a标签href不跳转 禁止跳转
    使用LVS 实现负载均衡的原理。
    使用LVS实现负载均衡原理及安装配置详解
    这是一套Java菜鸟到大牛的学习路线之高级教程,由工作了10年的资深Java架构师整理。
    LoadRunner使用教程
    Mac OS启动服务优化高级篇(launchd tuning)禁用某些服务
    三种方式配置Mac OS X的启动项
    网络资源整理
  • 原文地址:https://www.cnblogs.com/zhanggaofeng/p/9689594.html
Copyright © 2020-2023  润新知