Reputation: 607
I changed the below code for sending Unicode string to client.
please check it and tell me why in client I can't recv any data when txt contains an Unicode character??
bool write_to_descriptor( int desc, wchar_t *txt, int length )
//bool write_to_descriptor( int desc, char *txt, int length )
{
int iStart;
int nWrite;
int nBlock;
if ( length <= 0 )
length = strlen(txt);
for ( iStart = 0; iStart < length; iStart += nWrite )
{
nBlock = UMIN( length - iStart, 4096 );
if ( ( nWrite = send( desc, txt + iStart, nBlock, 0 ) ) < 0 )
{ perror( "Write_to_descriptor" ); return FALSE; }
}
return TRUE;
}
Upvotes: 3
Views: 5314
Reputation: 4429
For TCP (and UDP) protocol there is no such thing as wchar_t
. And for function send there is no such thing as wchar_t
. There is only raw binary data - the sequence of bytes.
The problems of this code is the following:
length = strlen(txt);
- The function that calculates the length of zero-terminated wide string is wcslen
. For little-endian UTF16 string (that is wide string on WIN32 platform) if the first wide character is ASCII (or Latin1) character then strlen
returns 1 (because second byte is the high byte of the first wide character and it contains zero for ASCII char).
nWrite = send( desc, txt + iStart, nBlock, 0 )
You send
BYTES not characters (and especially not wide characters). So if you want to send wide string's binary representation (that is not portable and thus should not be sent over the network but would work if the client and server is on the same platform) you have to do it like this: nWrite = send( desc, txt + iStart, sizeof(wchar_t)*nBlock, 0 )
. When you send it like you do you send only half of your wide string. Of course the application on the other end of connection should be aware that binary representation of wide string for the specific platform is sent (not ASCII string or something).
Of course you should never do something like sending platform/compiler-specific internal binary wide string representation. You should use some well-documented network protocol (like telnet or HTTP or whatever) most of which use either ASCII or UTF-8 character encoding for text representation. So you should convert your wide strings to the representation required by the protocol of your choice.
Upvotes: 5