当前位置:文档之家› PROTOBUF使用例子

PROTOBUF使用例子

if (server != null) { try { server.close(); } catch (IOException e) { e.printStackTrace(); }
} if (socket != null) {
try { socket.close();
} catch (IOException e) { e.printStackTrace();
// 通过 toByteArray 方法序列化成二进制数据
byte[] bytes = builder.build().toByteArray();
int length = bytes.length;
ous.write(length); ous.write(bytes); ous.flush();
System.out.println("length=" + length);
private final static int PORT = 8080;
public static void main(String[] args) { ServerSocket server = null; InputStream ins = null; OutputStream ous = null; Socket socket = null; try { server = new ServerSocket(PORT);
} finally {
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (ins != null) {
try {
ins.close();
} catch (IOException e) {
System.out.println("extInfo="
+
helloworld.getExtInfo().getInfo());
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
public static void main(String[] args) {
Socket socket = null; InputStream ins = null; OutputStream ous = null; try {
socket = new Socket(HOST_NAME, PORT); socket.setKeepAlive(true); socket.setSoTimeout(10000); ous = socket.getOutputStream(); ins = socket.getInputStream();
1.1. proto 文件编写
Protobuf 中的 proto 文件编写,比如定义一个 HelloWorld.proto 文件 // HelloWorld.proto 文件为: // 定义生成 java 文件所在的包名 option java_package = "com.helloworld.protocol"; // 生成对应外部类名称 option java_outer_classname = "HelloWorldProtoc";
Protobuf 使用例子
1. protobuf 使用整理
protobuf 序列化反序列化的一种解决方案,protobuf 处理成二进制数据流,相比较 xml/json 更加节省数据流量。 protobuf 是 google 提出的解决方案,有比较多的互联网公司采用此种解决方案, protobuf 只支持 java/python/php 支持语言相对比较少。 protobuf 提供了 protobuf-java-xx.jar 工具包处理,要求开发者定义.proto 文件, 然后进行执行编译成对应语言版本的源文件,比如 java 是编译生成.java 源文件。
} }
} } 服务器端输出日志为: server start up success num: 1 num2: 2 extInfo {
num: 1 num2: 2 info: "hello world ext" }
server write success 客户端输出日志为: length=27 builder=num: 1 num2: 2 extInfo {
protobuf生成源 文件.rar
若在 client 端-server 端开发过程中,比如客户端使用的是 Android 开发,则可以将 生成的 protocol 源文件拷贝给客户端开发了。通过 HelloWorldProtoc 文件进行数据 携带传输。
1.3. 通过 socket 进行通信
下载官方提供的 protobuf-java.xx.jar 包,然后就可以进行开发工作了,简单采用 socket 进行处理 client 端请求,server 端进行应答处理。 处理过程为: Client 发出请求>>>用 HelloWorldProto 进行携带数据,转换成二进制数据流 Server 端接收请求>>>反序列化>>>对象>>>序列化>>>传输回给客户端 代码压缩包为:
System.out.println("builder="
+
builder.build().toString());
// 先读 length 长度,然后读 byte[]字节数组 int readLength = ins.read(); byte[] readBytes = new byte[readLength]; ins.read(readBytes, 0, readLength);
ous.write(length); ous.write(bytes); ous.flush();
System.out.println("server write success"); } catch (IOException e) {
e.printStackTrace(); } finally {
System.out.println("server start up success");
socket = server.accept(); ins = socket.getInputStream(); ous = socket.getOutputStream();
int length = ins.read(); byte[] bytes = new byte[length]; ins.read(bytes, 0, length); // 这种读法导致阻塞了 // while (ins.read(bytes) != -1) { // } HelloWorld helloworld = HelloWorld.parseFrom(bytes); System.out.println(helloworld.toString());
enum SexType{ MALE=0;// 0-男性 FEMALE=1;// 1-女性 UNKNOWN=2;// 2-未知
}
message HelloWorldExt{ required int32 num=1; optional int64 num2=2; optional string info=3;
message HelloWorld{ // 定义必须属性,类型为 int32 required int32 num=1; // 定义可选属性,类型为 int64 optional int64 num2=2; // 定义可选属性,类型为 string optional string info=3; // 定义为 list,list 里边 item 类型为 string repeated string mobileList=4; // 定义枚举类型,设定 default 默认值为 MALE optional SexType sexType=5[default=MALE]; // 定义一个 message 对象 optional HelloWorldExt extInfo=6;
2. Jprotobuf 使用整理
2.1. jprotobu不用面对 proto 文件的
新开发模式,只需要简单在 pojo 上增加注解即可采用 jprotobuf 传输数据。 考虑这样一个场景,若客户端为 android 或者 ios,不定义好.proto 文件客户端如何进 行开发,android 还比较好办,但是 ios 如何做到呢。
e.printStackTrace();
}பைடு நூலகம்
}
if (ous != null) {
try {
ous.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
} // HelloWorldServer.java 文件为: public class HelloWorldServer {
} }
1.2. 执行生成 java 文件
下载到 protobuf 生成 exe 文件,名称为:protoc.exe 可执行文件 编写生成.java 文件脚本为:gen-test.bat 内容为: protoc --java_out=./ HelloWorld.proto pause 生成.java 文件放在当前目录下的 com/helloworld/protocol 文件夹目录下
相关主题