mina server communicates with c++ client 1

I recently learned about Apache MINA communication. I encountered some problems in the process of using it, and recorded some experiences.
When both the server and the client use the library provided by MINA, the communication is normal. When I changed the client to C++ code, I found that the position of the integer data in the binary stream sent by the client to the server was inverted.
C++ client hex: 0x00000013
MINA server hexadecimal: 0x13000000
After checking the online information, I found out that Java is big-endian by default on all platforms, while C++ has different byte orders on different platforms. It is little-endian on X86 and big-endian on solaris.
Pay attention to the problem:
1. Byte order
C++ has different byte order on different platforms, it is little-endian on X86, big-endian on solaris; and java is big-endian by default on all platforms, so when transferring data such as short, int, long When converting to network sequence (big-endian) in C++
2. Character encoding
The most common is to use mbcs on C++, while unicode is used on java (and there are some differences from standard unicode, you can refer to the java documentation), so don't pass strings unless necessary, you can pass text files instead, if you must pass I can only convert it myself.
3. Memory alignment. In C/C++ network communication programs, the way of reading and writing structures is often used to easily exchange data, but if you don’t pay attention, there may be gaps in the structure, such as struct A{ int a; char c }; struct B{ char a; int b }; There are gaps in these two structures, and if the java program does not know the existence of the gap, it will cause errors in parsing on both sides. To eliminate the gap, you should Arrange the members of the structure carefully, #pragma pach(1) is not recommended because there is no generality
4. Bit fields
Unless carefully arranged, the size of the structure caused by the bit field is platform-dependent, and the bytes occupied by int a:4 vary with the platform and compiler (char a:4 is relatively stable and occupies 1 byte)
5. (May be platform-dependent) The speed of transmission and reception is different. When C++ transmits a larger data to java, it may be that C++ has finished transmitting and exited, but java has not finished receiving it, resulting in the loss of the last part of the data. Therefore, a simple confirmation mechanism is used in the project. , either party sends back a 1-byte acknowledgment after receiving the data to prevent C++ from exiting prematurely
6. (May be platform-related) After java establishes a connection with C++ and after C++ transmits a piece of data to java, if java transmits a piece of data to C++, the first transmitted data C++ can only receive one byte, the first return to normal after
 
 
The C++ integer conversion code is as follows:
    void swap_4(unsigned long &x)    
    {    
        x = (x << 24) |    
        ((x << 8) & 0x00ff0000u) |    
        ((x >> 8) & 0x0000ff00u) |    
        (x >> 24);    
    }    
        
    int _tmain(int argc, _TCHAR* argv[])    
    {    
             
         unsigned long len = 19;    
         swap_4(len);    
    }  
 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326684111&siteId=291194637