Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I wrote a simple socket program by following Sam21 learn VC++ 6.0 in 21 days. 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 letter of the string on the receive side. I wanna receive the entire string. This is my code:

Send::
C#
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);
        }
    }
}

Receive::
C#
void CsocketDlg::OnReceive(void)
{
    char *pBuf = new char[1025];
   int iBufSize = 1024;
   int iRcvd;
   CString strRecvd;
   //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
       strRecvd = pBuf;
       //add the message to the received list box
       m_ctlRecvd.AddString(strRecvd);
       //sync the variables with the controls
       UpdateData(FALSE);
   }
}
Posted
Comments
Jeevan83 22-Mar-14 14:50pm    
I have changed my receive code to following, now I can receive only first two chars and then some japanese chars. Please help me
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);
}
}
Richard MacCutchan 22-Mar-14 16:13pm    
You are sending ASCII characters but your receive code is trying to receive Unicode. You must make sure both ends are using the same character sets.

1 solution

Changed from use unicode to use multi-byte in project properties.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900