Advertisement

Ubuntu下使用protobuf和socket实现消息传输

阅读量:

一、定义protobuf数据结构Test.proto

复制代码
    package Test.protobuf ;//包名:在生成对应的C++文件时,将被替换为名称空间,在代码中会有体现
    option optimize_for = SPEED ;//文件级别的选项,Protobuf优化级别
    //心跳信息数据结构
    message HeartInfo
    {
    required int32 curtime = 1;
    required string hostip = 2 ; 
    };
    
    
      
      
      
      
      
      
      
      
    

二、编写protobuf和socket进行双向通信代码

客户端client.cpp代码:

复制代码
    #include <iostream>
    #include <string>
    #include <ctime>
    //for protobuf
    #include "Test.pb.h" 
    //for socket
    #include <stdio.h>
    #include <sys/types.h>
    #inclu

全部评论 (0)

还没有任何评论哟~