Click to See Complete Forum and Search --> : Missing characters


EirikO
July 4th, 2007, 02:14 AM
I use the following code to send a message from a editbox:


CString Msg = m_Msg;

DWORD Length = Msg.GetLength();

m_OutputCtrl.AddString(_T("Sending your message..."));

m_OutputCtrl.AddString(Msg);

//Msg += "\n\r";

int BytesSent = m_MySocket.Send(Msg, Length);

if(BytesSent > 0) {

m_OutputCtrl.AddString(_T("Sending OK"));

}



The problem is that the last characters disapears. I think ceil(length/2) characters comes through... Se table:

What I send | What comes through

a | a

ab | a

abc | ab

abcd | ab

abcde | abc

abcdef | abc

abcdefg | abcd

abcdefgh | abcd

abcdefghi | abcde

123456789123456 | 12345678

Can anyone please help me? The question is, I think, how to find number of bytes in a Cstring...

MikeAThon
July 6th, 2007, 07:30 PM
Sorry for the delay in responding.

You probably have a Unicode mismatch. The sending program might be a Unicode build. If this is the case, the since the GetLength() function returns the number of TCHARs in the string, and not the number of bytes, and since in a Unicode build the number of characters is only half the number of bytes, then your sending program would only send half the needed bytes.

Try this:
DWORD Length = Msg.GetLength() * sizeof(TCHAR);
Note that the receiving side must also expect Unicode, and that there might be a need to send the terminating NULL character too, in which case the code should be
DWORD Length = ( Msg.GetLength() + 1 ) * sizeof(TCHAR);

Mike