The mina server communicates with the c++ client 2

 

Original address: http://www.cppblog.com/chugf/archive/2011/07/05/150224.html

There is also the problem of encoding and transcoding in the communication between Java and C++. It is assumed that the communication encoding adopts UTF-8

When the C++ client sends, it needs to be converted into UTF-8 encoding, and then converted back to Unicode or GBK encoding after receiving the response message from the server

An example of a completed C++ client communication is given below:

    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 connect    
        CXSocket client;    
        client.initWinSocket();    
        client.setAddress("127.0.0.1");    
        client.setPort(1234);    
        client.setProtoType(TCP);    
        int ret = client.connect();    
        if(ret < 0)    
        {    
            getchar();    
            return 0;    
        }    
        
        // Add communication protocol header demo    
        // client send    
        char send[] = {"CXSocket first test case."};    
        
        string utf8;    
        Transcoding(send, CP_ACP, utf8, CP_UTF8);    
        
        XNET :: TPacketHeader sendHeader;    
        sendHeader.m_HeaderSig = 0xABCD;    
        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 recv    
        XNET :: 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;    
    }    

 

Guess you like

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