CAsyncSocket and _UNICODE 
Author Message
 CAsyncSocket and _UNICODE

Hi all,
I have a remark about CAsyncSocket and _UNICODE I can't handle on my
own! Please consider the following example for the 0nReceive method I
found on msdn

http://www.*-*-*.com/

   // example from msdn
   // ...

   TCHAR buff[4096];
   int nRead;
   nRead = Receive(buff, 4096);

   // ...

   buff[nRead] = 0; //terminate the string
   CString szTemp(buff);

   // ...

Assume that the _UNICODE flag is set (sizeof(TCHAR)=sizeof(wchar_t)=2
bytes)

First consideration:
since nRead represents the readed bytes (and not readed TCHARs),
buff[nRead] does not terminate the string. The code that terminates
the string should look like this:

buff[nRead / sizeof(TCHAR)] = _T('\0')

Second consideration:
What happens when nRead is odd? An "half-TCHAR" is readed?

In msdn, I found several other incongruences...

Is this a "bug", or am I wrong? How is it possible to write safe
_UNICODE code in association with CAsyncSocket?

Thanks in advance
Andrea



Sat, 10 Sep 2005 12:13:42 GMT  
 CAsyncSocket and _UNICODE

Quote:

> Hi all,
> I have a remark about CAsyncSocket and _UNICODE I can't handle on my
> own! Please consider the following example for the 0nReceive method I
> found on msdn

> http://msdn.microsoft.com/library/en-us/vcmfc98/html/_mfc_casyncsocke...

>    // example from msdn
>    // ...

>    TCHAR buff[4096];
>    int nRead;
>    nRead = Receive(buff, 4096);

>    // ...

>    buff[nRead] = 0; //terminate the string
>    CString szTemp(buff);

>    // ...

> Assume that the _UNICODE flag is set (sizeof(TCHAR)=sizeof(wchar_t)=2
> bytes)

> First consideration:
> since nRead represents the readed bytes (and not readed TCHARs),
> buff[nRead] does not terminate the string. The code that terminates
> the string should look like this:

> buff[nRead / sizeof(TCHAR)] = _T('\0')

> Second consideration:
> What happens when nRead is odd? An "half-TCHAR" is readed?

> In msdn, I found several other incongruences...

> Is this a "bug", or am I wrong? How is it possible to write safe
> _UNICODE code in association with CAsyncSocket?

> Thanks in advance
> Andrea

Sockets communicate only a stream of bytes, so it is possible for nRead
to be an odd number from time to time.  It will be necessary to buffer
the received data and process only the amount that is valid.  I.e. an
even number of bytes in this case.  Any remaining byte(s) must be held
in a buffer.  Then the data received during the next OnReceive must be
appended to that buffer.  A buffer manager something like the following
is needed in any socket that receives data that must be "reassembled"
into anything that is not a pure stream of bytes.

0. Initialize int startindex = 0;
   char inbuff[4096];
1. nRead = Receive(&inbuff[startindex], 4096 - startindex);
2. Copy the valid data (an even count) to an output (TCHAR) buffer.
3. Terminate the output buffer and process it.
4. Move any remaining byte(s) down to the start of the input buffer.
5. Set startindex to number of bytes remaining in input buffer
   so next Receive will append to it.

--
Scott McPhillips [VC++ MVP]



Sun, 11 Sep 2005 00:59:59 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. UNICODE, programs using unicode

2. how to get unicode string from database in an none-unicode project

3. Why define _UNICODE and UNICODE

4. UNICODE and _UNICODE: ??

5. #define UNICODE or _UNICODE ?

6. Are CStrings UNICODE if _UNICODE is defined?

7. Unicode Edit box in non-unicode application?

8. Using Unicode DLL in Non-Unicode Exe

9. UNICODE or _UNICODE?

10. Question regarding (old?) CAsyncSocket bug

11. CAsyncSocket::AsyncSelect

12. Help CAsyncSocket In Thread

 

 
Powered by phpBB® Forum Software