栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

c++ 通过socket protobuf 传送vector 结构体数据

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

c++ 通过socket protobuf 传送vector 结构体数据

参考:

    protobuf 使用link专注于中台化代码生成器linkprotobuf repeated的用法link深入浅出:如何正确使用 protobuflinkHow to add vector to repeated field protobuf c++linkUbuntu下使用protobuf和socket实现服务器间消息传输linkProtobuf的C++使用笔记linkProtobuf学习笔记(二)proto类与vector,数组的相互转换linkprotobuf中repeated类型变量与C++ vector类型变量的相互赋值方法link链接protobuf遇到的一个坑,记录下来备忘一下link如何停止使用Protobuf 3输出错误消息“Can’t parse message of type because it is missing required fields”?link将字符串数组清空的操作link

代码:

    ch128driver.proto
syntax = "proto2";
package Pointframe;

message PointXYZRGBA_PROTO{
  optional float x = 1;
  optional float y = 2;
  optional float z = 3;
  optional int32 a = 4;
  optional int32 r = 5;
  optional int32 g = 6;
  optional int32 b = 7;
};
message POINTS_PROTO{
  repeated PointXYZRGBA_PROTO myPointXYZRGBA_PROTO = 1;
  required int32 curtime = 2;
}
    cmakelist.txt
cmake_minimum_required(VERSION 3.5)
PROJECT (protobuf_socket_test)
add_definitions(-std=c++11)

# Find required protobuf package
find_package(Protobuf REQUIRED)
if(PROTOBUF_FOUND)
    message(STATUS "protobuf library found")
else()
    message(FATAL_ERROR "protobuf library is needed but cant be found")
endif()

include_directories(${PROTOBUF_INCLUDE_DIRS})
include_directories(${CMAKE_CURRENT_BINARY_DIR})
PROTOBUF_GENERATE_CPP(PROTO_SRCS PROTO_HDRS ch128driver.proto)


ADD_EXECUTABLE(client client.cpp ${PROTO_SRCS} ${PROTO_HDRS})
target_link_libraries(client ${PROTOBUF_LIBRARIES})

ADD_EXECUTABLE(server server.cpp ${PROTO_SRCS} ${PROTO_HDRS})
target_link_libraries(server ${PROTOBUF_LIBRARIES})
    client.cpp
#include 
#include 
#include 
//for protobuf
#include "ch128driver.pb.h"
#include "utils.h"
//for socket
#include 
#include 
#include 
#include 
#include 
#include 

using namespace std;
//using namespace Test::protobuf ;

const int BUFFSIZE = 40000*32;

int main()
{
    //建立socket
    int socketfd ;
    struct sockaddr_in seraddr ;
    string hostip = "127.0.0.1";

    if((socketfd = socket(AF_INET,SOCK_STREAM,0)) > 0)
    {
        cout<<"create socket success..."< vp;

    std::vector vp;
    while(1)
    {
        int curtime = time(NULL) ;

        cout << "mypointxyzrgba.ByteSizeLong() :" << mypointxyzrgba.ByteSizeLong() << endl;
        cout << "mypoint_pro.ByteSizeLong()  " << mypoint_pro.ByteSizeLong() << endl;

        mypointxyzrgba.set_x(curtime*0.001);
        mypointxyzrgba.set_y(curtime*0.002);
        mypointxyzrgba.set_z(curtime*0.003);
        mypointxyzrgba.set_a(234);
        mypointxyzrgba.set_g(56);
        mypointxyzrgba.set_r(35);
        mypointxyzrgba.set_b(12);
        vp.push_back(mypointxyzrgba);

        mypoint_pro.set_curtime(curtime);
        mypoint_pro.mutable_mypointxyzrgba_proto()->CopyFrom({vp.begin(),vp.end()});

        char buff[BUFFSIZE];
//        std::string msg;
        mypoint_pro.SerializeToArray(buff,BUFFSIZE);
        cout << "mypoint_pro.ByteSizeLong() " << mypoint_pro.ByteSizeLong() << endl;
        cout << "sizeof(buff) :" << sizeof(buff) << endl;
        cout << "strlen(buff) :" << strlen(buff) << endl;
        if(send(socketfd,buff,strlen(buff),0) < 0)
        {
            cout< 
    server.cpp
#include 
#include 
#include 
#include 
//for protobuf
#include "ch128driver.pb.h"
#include "utils.h"
//for socket
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#include   // problem solved! it compiles!

using namespace std;
//using namespace Test::protobuf ;

const int BUFFSIZE = 40000*32;
const int QLEN = 10 ;

int main()
{

    int listenfd ;
    int connfd ;
    struct sockaddr_in seraddr ;
    //建立socket
    //AF_INET:IPv4因特网域
    //SOCK_STREAM:TCP链接
    //0:给定前两个参数,选择默认的协议
    listenfd = socket(AF_INET,SOCK_STREAM,0);
    if(listenfd < 0 )
    {
        cout<<"socket failed"< svp;

    //PointXYZRGBA mypoint;
    Pointframe::POINTS_PROTO mypoint_pro;
    std::vector svp;

    char buff[BUFFSIZE];


    while(1) {
        ;
//        cout << "sizeof(buff): " << sizeof(buff) << endl;
//        cout << "strlen(buff): " << sizeof(buff) << endl;
        if (recv(connfd, buff, sizeof(buff), 0) < 0) {
            cout << "recv failed ..." << endl;
            break;
        }
        cout << "recv scucess ..." << endl;


        mypoint_pro.ParseFromArray(buff,BUFFSIZE);
        cout << mypoint_pro.ByteSize() << endl;

        cout << mypoint_pro.mypointxyzrgba_proto().size()<< endl;
        cout << svp.size() << endl;
        cout << mypoint_pro.mypointxyzrgba_proto().Capacity() << endl;
//        cout << ((Pointframe::PointXYZRGBA_PROTO)svp.at(0)).a() << endl;
        cout << mypoint_pro.mypointxyzrgba_proto().data()<< endl;


        //cout << mypoint_pro.mypointxyzrgba_proto().<< endl;
        svp.insert(svp.end(), mypoint_pro.mypointxyzrgba_proto().begin(), mypoint_pro.mypointxyzrgba_proto().end());
        cout << mypoint_pro.curtime() <<" "  << svp.at(0).x() <<" " << svp.at(0).y() <<" " < 
    utils.h
//
// Created by wx on 2022/2/23.
//

#ifndef PROTOBUF_SOCKET_TEST_UTILS_H
#define PROTOBUF_SOCKET_TEST_UTILS_H
typedef struct POINTXYZRBA
{
    float x;
    float y;
    float z;
    int a;
    int r;
    int g;
    int b;
}PointXYZRGBA;
#endif //PROTOBUF_SOCKET_TEST_UTILS_H
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/743502.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号