Protobuf的使用
2017-04-04 23:47
302 查看
1.系统环境
Windows
2.所需软件和lib
软件下载路径:http://download.csdn.net/download/xiao__gui/7586617
1). protoc.exe
2). protobuf-java-2.4.1.jar
3.demo简介
很简单的程序,基于java开发。功能是客户端把 "消息"(手机信息) 发送给 服务端。服务端收到消息后打印。
4.开发流程
1). 定义"消息"的结构,即书写接口定义文件(.proto文件)。本例中,消息包含了"手机信息"。
文件名 mobile.proto
文件内容如下:
message MobilePhone{
required string brand = 1 ;
required Hardware hardware = 2;
repeated string software = 3;
}
message Hardware {
required int32 rom = 1;
required int32 ram = 2;
required int32 size = 3 ;
}
2).通过定义的接口文件,生成Mobile.java
执行命令: protoc.exe --java_out=outputFile sourceFile
上述命令中outputFile 和 sourceFile 指 输出文件和源文件,需替换成实际文件(路径)名,如:
protoc.exe --java_out=E:\java mobile.proto
3).创建工程,编码
引入protobuf-java-2.4.1.jar
拷贝Mobile.java至工程
书写客户端,服务端代码。
具体代码如下:
客户端
服务端:
运行后服务端打印:
msg body len:48
Receive:
brand: "Apple"
hardware {
rom: 16
ram: 2
size: 5
}
software: "camera"
software: "tecent"
software: "browser"
software: "player"
Windows
2.所需软件和lib
软件下载路径:http://download.csdn.net/download/xiao__gui/7586617
1). protoc.exe
2). protobuf-java-2.4.1.jar
3.demo简介
很简单的程序,基于java开发。功能是客户端把 "消息"(手机信息) 发送给 服务端。服务端收到消息后打印。
4.开发流程
1). 定义"消息"的结构,即书写接口定义文件(.proto文件)。本例中,消息包含了"手机信息"。
文件名 mobile.proto
文件内容如下:
message MobilePhone{
required string brand = 1 ;
required Hardware hardware = 2;
repeated string software = 3;
}
message Hardware {
required int32 rom = 1;
required int32 ram = 2;
required int32 size = 3 ;
}
2).通过定义的接口文件,生成Mobile.java
执行命令: protoc.exe --java_out=outputFile sourceFile
上述命令中outputFile 和 sourceFile 指 输出文件和源文件,需替换成实际文件(路径)名,如:
protoc.exe --java_out=E:\java mobile.proto
3).创建工程,编码
引入protobuf-java-2.4.1.jar
拷贝Mobile.java至工程
书写客户端,服务端代码。
具体代码如下:
客户端
package com.nevermore.client; import java.net.Socket; import com.nevermore.domain.Mobile; public class Client { /** * @param args */ public static void main(String[] args) throws Exception { // TODO Auto-generated method stub Socket socket = new Socket("127.0.0.1",3030); Mobile.MobilePhone.Builder builder = Mobile.MobilePhone.newBuilder(); Mobile.Hardware.Builder hardware = Mobile.Hardware.newBuilder(); hardware.setRam(2).setRom(16).setSize(5); builder.setHardware(hardware) .setBrand("Apple") .addSoftware("camera") .addSoftware("tecent") .addSoftware("browser") .addSoftware("player"); byte[] messageBody = builder.build().toByteArray(); int headerLen = 1; byte[] message = new byte[headerLen+messageBody.length]; message[0] = (byte)messageBody.length; System.arraycopy(messageBody, 0, message, 1, messageBody.length); System.out.println("msg len:"+message.length); socket.getOutputStream().write(message); } }
服务端:
import java.net.ServerSocket; import java.net.Socket; import com.nevermore.domain.Mobile; import com.nevermore.domain.Mobile.MobilePhone; public class Server { /** * @param args */ public static void main(String[] args) throws Exception { // TODO Auto-generated method stub ServerSocket serverSock = new ServerSocket(3030); Socket sock = serverSock.accept(); byte[] msg = new byte[256]; sock.getInputStream().read(msg); int msgBodyLen = msg[0]; System.out.println("msg body len:"+msgBodyLen); byte[] msgbody = new byte[msgBodyLen]; System.arraycopy(msg, 1, msgbody, 0, msgBodyLen); MobilePhone phone = Mobile.MobilePhone.parseFrom(msgbody); System.out.println("Receive:"); System.out.println(phone.toString()); } }
运行后服务端打印:
msg body len:48
Receive:
brand: "Apple"
hardware {
rom: 16
ram: 2
size: 5
}
software: "camera"
software: "tecent"
software: "browser"
software: "player"
相关文章推荐
- google protobuf java 简单使用
- google protobuf 使用心德
- Silverlight Tcp通讯中使用Protobuf.net
- Google Protobuf 使用介绍 .
- java中使用Protobuf的实例(Demo)
- protobuf使用
- 基于netty的项目中使用protobuf,巧妙定义proto完成不同消息的编码和解码处理
- Beetle使用Protobuf.net进行对象序列化传输
- linux下Google的Protobuf安装及使用笔记
- 如何在Linux环境下在CodeBlock中安装使用Google Protobuf
- java中使用protobuf序列化(反序列化)
- protobuf安装与使用
- 使用protobuf定义消息
- Google protobuf的安装及使用
- erlang中使用google protobuf进行通信
- [转]Android上GTalk以及Push机制的XMPP数据选择使用protobuf格式而非XML格式
- Google Protobuf 使用介绍
- Google Protobuf的使用
- c#序列化与反序列化通用方法, 使用protobuf-net实现
- ProtoBuf 的java使用