Use of Protobuf and demo (Java)

Source: Internet
Author: User

Project useful to Protobuf for cross-platform communication, I also want to write a demo learning practice, so there is this article.


* This demo is based on Java development.


1. System environment

Windows


2. Required software and Lib

1). Protoc.exe

2). Protobuf-java-2.4.1.jar


3.demo Introduction

Very simple program, based on Java development. The function is that the client sends "messages" (cell phone information) to the server. The server receives the message and prints it.


4. Development process

1). Defines the structure of the message, that is, the writing interface definition file (. proto file). In this example, the message contains "phone information."

File name Mobile.proto

The contents of the file are as follows:

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). Generate Mobile.java by defining the interface file

Execute command: Protoc--java_out=outputfile sourcefile

OutputFile and SourceFile in the above command refer to the output file and the source file, which need to be replaced with the actual file (path) name, such as:

Protoc--java_out=./src./proto/mobile.proto



3). Create Project, encode

Introduction of Protobuf-java-2.4.1.jar

Copy Mobile.java to Engineering

Write client, service-side code.

The specific code is as follows:

Client

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 (+). 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);     }}

Server side:

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];&nbsP;       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 ());     }}


Post-run service-side printing:

Receive:
Brand: "Apple"
Hardware {
Rom:16
Ram:2
Size:5
}
Software: "Camera"
Software: "Tecent"
Software: "Browser"
Software: "Player"


This is done, only for notes.

Use of Protobuf and demo (Java)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.