Communication between Mina server and C ++ client (2)

Source: Internet
Author: User

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

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.