Research on communication technology between Java Server and C # client

Source: Internet
Author: User

Java server and C # Client communication technology research background and purpose:

ARPG Project requirements: You need to change your existing server from the authoring platform of C + + to the Java language.
In the process of analyzing the requirements, we find some problems that need to be researched and realized.

    1. Java and c+ language characteristics are very different, compared to the c+ and C # relationship, Java needs some C # style data structure and encoding format compatible;
    2. C # has an unsigned data type such as UShort Unint Java does not exist and requires a conversion of the data type;
    3. According to the development needs of the client's existing communication protocol can not be changed, so in Java for various types of compatible operations
    4. The class of the underlying communication message in the project is generated by an existing project called Protogen, generated in a way that generates a string literal class, where the original project can generate both C # and C + + classes (. cs and. h files), but does not generate the functionality of the migrated server Java related code and classes. It requires two development times.
Work tasks:

To sum up, the task has two points:

    1. Refactor the Java data flow input and output classes to make them compatible with the binary data structures transmitted by C # clients; (hereinafter referred to as Pjio)
    2. The Protogen was developed two times to enable it to generate Java's underlying protocol class (. java file) without destroying its existing ability to generate classes in other languages; (hereinafter referred to as Pgplus)
    3. Conduct testing and validation to ensure the effective and follow-up development of the first two tasks is carried out smoothly.

Technical issues and technical details encountered in the development of the work: Pjio:
    1. During the testing and development process, the data storage format for C # and C + + was found to be a small end type and Java was a big-endian type (that is, a byte-order problem) by consulting the data.

On almost all machines, multi-byte objects are stored as contiguous sequence of bytes. For example, in C, where a variable of type int has an X address of 0x100, the value of its corresponding address expression &x is 0x100. And the four bytes of x will be stored in the memory of the 0x100, 0x101, 0x102, 0x103 position. [1]
There are two general rules for the arrangement within the storage address. A multi-bit integer is arranged in the lowest or highest byte of its storage address. If the least significant bit is in front of the most significant bit, then the small end sequence is called, and the reverse is called the big endian . In a network application, the byte order is a factor that must be considered, because different machine types may use different standard byte-order, so they are transformed according to the network standard.
For example, assuming that the above variable x type is int, at address 0x100, its hexadecimal is 0x01234567, the address range is 0x100~0x103 bytes, and its internal ordering depends on the type of machine. The big-endian law starts from the first place: 0x100:01, 0x101:23,..。 And the small End method will be: 0x100:67, 0x101:45,..。

Big-endian sequence

Big- endian (English: Big-endian) or large-tailed sequence .

  • data in 8bit :

    In the

    Address increase Long direction   →

     

     

     

     

     

    ...

    0x0A

    0x0B

    0x0C

    0x0D

    ...

    Example, the highest bit byte is 0x0a  stored at the lowest memory address. The next byte 0x0b exists at the address at the back. is similar to the hexadecimal byte reading order from left to right.

  • data in 16bit :

    The

    Address Growth direction   →

     

     

     

     

     

    ...

    0x0a0b

    0x0c0d

    ...

     

     

     

    Highest 16bit unit 0x0a0b is stored low.

    Small-endian small-order (English: Little-endian) or Small-tailed order .

     

  • data in 8bit :

    The

    Address increase Long direction   →

     

     

     

     

     

    ...

    0x0D

    0x0C

    0x0B

    0x0A

    ...

    Lowest bit byte is 0x0d  stored at the lowest memory address. The subsequent bytes exist sequentially at the address at the back.

  • the data is in 16bit :

    address growth direction

    ...

    0x0c0d

    0x0a0b

    ...

    The lowest 16bit unit 0x0d0c is stored at low levels.

  • change the direction of the address growth :

Tables are more readable when you change the direction of the growth of an address, making it from right to left.

Address Growth Direction

...

0x0A

0x0B

0x0C

0x0D

...

The least significant bit (LSB) is where the 0x0d is stored at the lowest memory address. The subsequent bytes exist sequentially at the address at the back.

Address Growth Direction

...

0x0a0b

0x0c0d

...

The lowest 16bit unit 0x0c0d is stored at low levels.
The specific contents of this section are described in Https://zh.wikipedia.org/wiki/%E5%AD%97%E8%8A%82%E5%BA%8F
Therefore, before and after transmitting the message object in Pjio, the byte array of the message is reversed to be compatible with C # 's byte order, which is the main purpose of reconstructing the input and output stream of java.

    1. Java does not contain no characters, so it is replaced by the number of the corresponding width (byte length);

such as ushort>>short;

Pgplus:

1, this project is mainly encountered a problem, Java can not generate multiple public classes in the same file
There is also a language specification for camel-like nomenclature that differs from C #. C # functions are capitalized in the first letter, such as a. ReadInt16 ();
The method (function) in Java is lowercase a.readint16 ();

    1. For enumerations, enumerations in C # can give initial values such as:

And Java is not allowed, if not to achieve: the results are not satisfactory
So the enumeration is converted uniformly to a public class that contains only constants


Such as

Issues that are not resolved temporarily:

Java will have two bytes of 0 after reading a message transmitted by C #;

Research on communication technology between Java Server and C # client

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.