Java中的Protobuf使用例子(转)
来源:互联网 发布:mac开机后键盘没反应 编辑:程序博客网 时间:2024/06/07 00:00
protobuf接口文件的生成这里就不举例子,只举一下在Java中怎么使用。
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 --java_out=outputFile sourceFile
上述命令中outputFile 和 sourceFile 指 输出文件和源文件,需替换成实际文件(路径)名,如:
protoc --java_out=./src ./proto/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()); }}
运行后服务端打印:
Receive:
brand: "Apple"
hardware {
rom: 16
ram: 2
size: 5
}
software: "camera"
software: "tecent"
software: "browser"
software: "player"
转自:http://my.oschina.net/u/1162561/blog/364772
/** * int到byte[] * @param i * @return */ public static byte[] intToByteArray(int i) { byte[] result = new byte[4]; //由高位到低位 result[0] = (byte)((i >> 24) & 0xFF); result[1] = (byte)((i >> 16) & 0xFF); result[2] = (byte)((i >> 8) & 0xFF); result[3] = (byte)(i & 0xFF); return result; } /** * byte[]转int * @param bytes * @return */ public static int byteArrayToInt(byte[] bytes) { int value= 0; //由高位到低位 for (int i = 0; i < 4; i++) { int shift= (4 - 1 - i) * 8; value +=(bytes[i] & 0x000000FF) << shift;//往高位游 } return value; }
int num = Integer.valueOf(str).intValue();
- Java中的Protobuf使用例子(转)
- protobuf java中的使用
- Protobuf在Java中的使用
- Protobuf在java中使用例子
- golang使用protobuf例子
- protobuf使用例子
- protobuf在Java中的简单使用
- Protobuf在java项目中的使用
- protobuf 3.5 java使用介绍(一)
- ProtoBuf 的java使用
- ProtoBuf 的java使用
- ProtoBuf 的java使用
- protobuf 的java使用
- protoBuf的使用---java
- ProtoBuf 的java使用
- protobuf java使用
- Java 如何使用protobuf
- java中使用protobuf
- mac安装Matlab 2014b(转)
- FIX协议结构及工作流程(转)
- Python timedelta(转)
- fix协议中消息尾校验方法
- 一根管子就能种出全家人吃的菜
- Java中的Protobuf使用例子(转)
- java中ini文件工具(转)
- stunnel加密通道使用实践
- 用逆序字符串C
- python获取操作系统类型及编译为pyc
- 在Mac下用Eclipse和Rhino For Mac搭建RhinoPython编辑运行环境(转)
- 阿里云通过He.net添加IPV6(转)
- linux自动内存清理
- Hibernate初探之一对多