Java C ++ hybrid communication (Collection)

Source: Internet
Author: User
1. in byte order, C ++ has different byte order on different platforms, x86 is little-Endian, and Solaris is big-Endian; java is big-Endian by default on all platforms. Therefore, when transmitting data such as short, Int, and long, it must be converted to the network Order (big-Endian) in C ++)

2. the most common character encoding in C ++ is MBCS, while Unicode is used in Java (and there are some differences from standard Unicode. For details, refer to the Java documentation ), therefore, do not upload strings unless necessary. You can upload text files instead. If you must transfer strings, you can only convert them yourself.

3. memory alignment. in C/C ++ network communication programs, the read/write structure is often used to conveniently exchange data. However, if you do not pay attention to it, there may be gaps in the structure, for example, struct a {int A; char c}; struct B {char a; int B}; there are gaps in both structures, if it is not stated that the Java program does not know the existence of the gap, it will cause errors during parsing by both parties. to eliminate gaps, you should be careful to arrange the structure members. We do not recommend using # pragma Pach (1) because it is not universal.

4. bit Field, unless careful, the structure size caused by bit field is related to the platform. The Bytes occupied by int A: 4 change with the platform and compiler (char: 4 relatively stable occupies 1 byte)

5. (may be platform-related) the transmission speed is different from the receiving speed. When C ++ transfers a large amount of data to Java, the C ++ side may have been passed out, java has been confiscated, leading to the loss of the last part of data. therefore, the project adopts a simple validation mechanism. After receiving the data, either party returns the 1-byte confirmation to prevent premature exit of C ++.

6. (may be platform-related) after Java establishes a connection with C ++ and transmits a piece of data to Java, if Java transmits a piece of data to C ++, C ++ receives only one byte of the data for the first time, and returns to normal after the first time.

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.