Java and C ++ communication and encoding and transcoding problems exist, assuming that the communication encoding using UTF-8
The C ++ client needs to be transcoded to a UTF-8 when sending the code. After receiving the server response message, it can be converted back to Unicode or GBK encoding.
The following is an example of the completed C ++ client communication:
Void transcoding (lpctstr SRC, uint srccode, string & DEST, uint destcode) {int Len = multibytetowidechar (srccode, 0, SRC,-1, null, 0 ); wchar * srctemp = new wchar [Len]; multibytetowidechar (srccode, 0, SRC,-1, srctemp, Len); Len = widechartomultibyte (destcode, 0, srctemp,-1, null, 0, null, null); char * desttemp = new char [Len]; widechartomultibyte (destcode, 0, srctemp,-1, desttemp, Len, null, null ); deST = desttemp; Delete [] srctemp; Delete [] desttemp;} int _ tmain (INT argc, _ tchar * argv []) {// client connectcxsocket client; client. initwinsocket (); client. setaddress ("127.0.0.1"); client. set port (1234); client. setprototype (TCP); int ret = client. connect (); If (Ret <0) {getchar (); Return 0 ;} // Add the communication protocol header demo // client sendchar send [] = {"first test case of cxsocket. "}; string utf8; transcoding (send, cp_acp, utf8, cp_utf8); xnet: tpacketheader sendheader; sendheader. m_headersig = 0 xabcd; swap_2 (sendheader. m_headersig); sendheader. m_length = (INT) utf8.length (); swap_4 (sendheader. m_length); char buffer [1024] = {0}; memcpy (buffer, & sendheader, xnet: tcp_headlen); memcpy (& buffer [xnet: tcp_headlen], utf8.c _ STR (), utf8.length (); client. sendbuf (buffer, (INT) (xnet: tcp_headlen + utf8.length (); // client recvxnet: tpacketheader recvheader; client. receivebuf (& recvheader, xnet: tcp_headlen); swap_2 (recvheader. m_headersig); swap_4 (recvheader. m_length); char * Recv = new char [recvheader. m_length + 1]; memset (Recv, 0, recvheader. m_length + 1); client. receivebuf (Recv, recvheader. m_length); string ANSI; transcoding (Recv, cp_utf8, ANSI, cp_acp); client. close (); client. uninitwinsocket (); getchar (); Return 0 ;}
Cxsocket: http://code.google.com/p/x-net/
Mina server: http://download.csdn.net/source/3418326