I have a server and a client. I wrote handlers for send button and receive. When I send a string I am receiving only first two chars and then some japanese chars of the string on the receive side. I wanna receive the entire string.Please help me . This is my code:

void CsocketDlg::OnClickedBsend()
{
    // TODO: Add your control notification handler code here
    int iLen;
    int iSent;
    //sync controls with variables
    UpdateData(TRUE);
    //if(m_dSetThresholdFineDfLB.Create()==IDOK)
        //m_strMessage = m_dSetThresholdFineDfLB.m_sFrameLB;    
    //UpdateData(FALSE);
    if(m_strMessage  != " ")
    {
        iLen = m_strMessage.GetLength();
        //send the message
        iSent = m_sConnectSocket.Send(LPCTSTR(m_strMessage),iLen);
        //were we able to send the message
        if(iSent == SOCKET_ERROR)
        {
        }
        else
        {
            //Add the message to the list box
            m_ctlSent.AddString(m_strMessage);
            //sync the variables with the control
            UpdateData(FALSE);
        }
    }
}

void CsocketDlg::OnReceive(void)
{
    //char *pBuf = new char[1025];
    TCHAR  *pBuf = new TCHAR[1025];
   int iBufSize = 1024;
    //int iBufSize = sizeof(pBuf);
   int iRcvd;
  // CString strRecvd;
   TCHAR *strRecvd = new TCHAR[1025];
   //receive the message
    iRcvd = m_sConnectSocket.Receive(pBuf, iBufSize);
    //did we receive anything?
      if(iRcvd == SOCKET_ERROR)
   {
   }
   else
   {
       //truncate the end of the message
       pBuf[iRcvd] = NULL;
       //copy the message to CString
      //LPCWSTR wcscpy(LPCWSTR strRecvd ,LPCWSTR pBuf);
        wcscpy(strRecvd,pBuf);

       //strRecvd = pBuf;
       //CString strRecvd(pBuf);
       //add the message to the received list box
       m_ctlRecvd.AddString(strRecvd);

      // m_ctlRecvd.InsertString(0,strRecvd);
       //sync the variables with the controls
       UpdateData(FALSE);
       //m_ctlRecvd.UpdateData(FALSE);
   }
}

Looks like it is sending char* (line 15) and receiving wchar_t* (line 40)? Is that right? I don't think that will work. Suggest you change OnClickedBsend() to convert the string from char* to wchar_t* before sending it.

Hi ancient dragon i am new to this area. Please be more specific more more. I cant understand s***! thanks for ur time anyways!

There are two kinds of strings -- standard strings are char* which are 1 byte per character. This is normal in English language and a few other languages too. The other kind of strings are UNICODE strings, or wchar_t*, where each character is represented in two or more bytes. All languages, that I know of, can be represented in UNICODE characters.

Look at line 15 of the code you posted. LPCTSTR is a Microsoft typecast for const char*. Now in line 33 you declare a pointer of type TCHAR, which is another Microsoft macro (tchar.h) that can be either char* or wchar_t* depending on whether you compile the program for UNICODE or not. line 50 calls the function wcscpy() which is UNICODE only function that is the same as strcpy() but takes two wchar_t* parameters instead of two char* parameters.

In otherwids, you code is mixing two very different character types, char* and wchar_t*. Both client and server must speak the same language (use the same character representation). What I would do if I were you would be change OnClickedBsend() to use UNICODE strings just like OnReceive().

Here is a much more complete explanation

Edited 2 Years Ago by Ancient Dragon

This question has already been answered. Start a new discussion instead.