PROTOBUF discussion of communication between C # Server and Java client based on Networkcomms V3 communication framework

Source: Internet
Author: User

Http://www.cnblogs.com/csdev/p/4696686.html

In the previous communication between C # Server and Java client based on Networkcomms V3 communication Framework, the passing of characters in C # side and Java side communication was discussed, and a friend mentioned that if an integer of type int was passed, the encoding problem would occur.

Found an article on the internet, "using Protobuf for C # and Java communication," to learn, using PROTOBUF encoding, passing data, as if this can avoid coding problems.

(Although the coding problem is solved, but the sticky packet problem is not resolved, an experienced friend introduced how to solve the problem of sticky bag)

Server-side based Networkcomms V3 C # communication framework. The NETWORKCOMMS communication Framework is a commercial version and is not available in this article.

Quote Original: http://blog.csdn.net/wangpei421/article/details/8004903

C # and Java communication with Protobuf a previous article on Protobuf was written, mainly the implementation of the. NET version. Today is mainly about how to use Protobuf to define a protocol to enable C # to communicate with Java both (this communication refers to both data protocols, not communication protocols.) PROTOBUF application Scenario Personally, it is primarily used for data interaction and sharing, and this situation requires both parties to develop a specific data structure. Then use PROTOBUF to define a data structure, and then everyone from this profile, each to generate their own programming language corresponding code files, and then use the code to both sides of the data processing. Then, as long as all adhere to the data file format, data sharing can be achieved platform. If the data description file has been modified, as long as a certain rules, then the original data can be used in a compatible. This is what makes it a platform-agnostic file that works with the adaptation conversions between platform and language-related data objects, just like many XML parsers. Implementing C # and Java Communication Step 1. Define the protocol create a file with a. Proto suffix, this article creates a message file named Msg.proto, with the following information: Package tutorial;option java_package = " Com.protobuftest.protobuf "; (Package name when generating Java class; C # class namespace) Option Java_outer_classname = "Personprobuf";  (Generate class name for Java, C # Class) message person {Required String name = 1;  Required Int32 id = 2;  Optional String email = 3;    Enum Phonetype {MOBILE = 0;    HOME = 1;  Work = 2;    } message PhoneNumber {Required String number = 1;  Optional Phonetype type = 2 [default = HOME];  } repeated PhoneNumber phone = 4;          Message Countryinfo {Required String name = 1; Required String code = 2;          Optional int32 number = 3;  }}message AddressBook {repeated person person = 1;} Package in Java represents the name of this file, which represents the namespace of the file in C #, the message represents a class, required is required for the field, optional represents the field is optional, and you can set a default value for it, the default value format: [ Defalut=home], which is an enumeration type.

Quote Original:

 2. Both sides compile the. proto file (This article mainly uses the 2.4.1 version of Protobuf) proto can be compiled under Linux, Windows. Protobuf Download list Please refer to: http://code.google.com/p/protobuf/downloads/list, select the win version of the download.           After extracting it, you will get a Protoc.exe file, and you can start compiling it now. The steps for compiling Java are as follows: (1) Open command tool cmd (2) put the Protoc.exe file in the D:/protoc directory, CD to the directory CD D:/PROTOC (3) input Protoc.exe--java_ou T=./Msg.proto This will find a folder in D:/protoc directory Com\protobuftest\protobuf, that is, the proto of the package named directory, will produce a Msg.java file, when this file is Can be used in our Java or Android engineering. (4) Download a Protobuf-java-2.4.1.jar jar package reference to your Java and Android projects, then you can use your protobuf. C # Compilation steps: the. NET version of Protobuf comes from the proto community, with two versions. A version called Protobuf-net, the official site: http://code.google.com/p/protobuf-net/on the wording of C # consistent wording. Another version is called Protobuf-csharp-sport, the official site: http://code.google.com/p/protobuf-csharp-port/writing on the Java with the use of very similar, compared to follow Google The original ecological wording, cross-platform selection of this version is better.                Because you will find that almost as much as Java, this version is also used in this article. http://code.google.com/p/protobuf-csharp-port/Download the win version, compile the steps as follows: (1) put the proto file in your extracted directory and Protoc.exe, ProtoGen.exe, ProtoGen.exe.config, GOOGLE.PROTOCOLBUFFERS.Dll put it together. Other files can be deleted or backed up.           (2) Open command line cmd, locate in the corresponding directory. Input: Protoc--descriptor_set_out=msg.protobin--include_imports Msg.proto msg.protobin is the Prtobobin file to be generated, you can make Use this bin file to generate CS file and then input Protogen msg.protobin Use the bin file to generate CS file, so you can get the Msg.cs csharp version of the file, while in VS use to introduce Google.proto ColBuffers.dll.

Busy all the afternoon, the relevant tools are gathered up, proto version and the reference to the version number of the article is not a version, you can use

C # related PROTOBUF file

Java Related PROTOBUF files

Server-side code:

View Code

Client

Server-side:

Java code

View Code

C # project file (without communication library) communication library is the same with other libraries.

PROTOBUF discussion of communication between C # Server and Java client based on Networkcomms V3 communication framework

Related Article

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.