CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Dec 2007
    Posts
    33

    sending unicode over winsock

    hi, does someone has any idea how can i send w_char array over winsock?
    i tried google, but haven't found nothing yet.
    thanks in advance

  2. #2
    Join Date
    Nov 2002
    Location
    California
    Posts
    4,556

    Re: sending unicode over winsock

    If by "winsock" you mean TCP, then you should understand that TCP simply sends a stream of bytes without trying to interpret what the bytes represent. To TCP, the the bytes are simply bytes. It's the responsibility of the sender and recipient to agree on what the stream of bytes might mean.

    So, in answer to your question, simply send the w_char array as bytes and make certain that the recipient knows that it will be receiving the w_char array and not something else:
    Code:
    // sender
    
    // I have omitted all needed calls to WSAStartup(), socket(), connect() etc, 
    // which are all needed to establish a connection to recipient
    
    w_char wideArray[100];
    int sentSoFar = 0;
    int needToSend = 2*100;
    char* addr = (char*) wideArray;
    while (sentSoFar<needToSend)
    {
      int iRet = send( s_Socket, addr+sentSoFar, needToSend-sentSoFar, 0 );
      if ( iRet == SOCKET_ERROR )
      {
         /* handle the error */
      }
      sentSoFar += iRet;
    }
    The code for the recipient is left up to you.

    Mike
    Last edited by MikeAThon; December 19th, 2007 at 03:33 PM.

  3. #3
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: sending unicode over winsock

    In addition to Mike's:

    you must understand a potential problem of sending WCHAR array - the opposit side may have different endianness. So sent items (w_chars) ideally have to be translated to net byte order at sending side and translated to host byte order at receiving side.
    Best regards,
    Igor

  4. #4
    Join Date
    Aug 2002
    Location
    India
    Posts
    97

    Arrow Re: sending unicode over winsock

    In addition to Mike's:

    you must understand a potential problem of sending WCHAR array - the opposit side may have different endianness. So sent items (w_chars) ideally have to be translated to net byte order at sending side and translated to host byte order at receiving side


    some more addition to the last post: you can use htons() ntohs() functiosn will help to convert "the network byte order, which is big-endian " "network byte order to host byte order, which is little-endian on Intel processors"

    Thanks,
    Abhilash

  5. #5
    Join Date
    Aug 2004
    Posts
    294

    Re: sending unicode over winsock

    Also UTF-8 is a good platform-indepenent encoding (fixed byte order). And it is supported by WideCharToMultiByte and MultiByteToWideChar - CP_UTF8.

    Note that UTF-8 is a variable-length encoding and if your characters are expected to be mostly ASCII7 (English, West- and Middle-European languages), you will gain almost 50% compression. For Cyrillic and Greek and few other it will be the same as raw WCHAR, and for Chinese, Japanese, Korean, etc. there will be 3/2 increase over WCHAR.
    Last edited by Boris K K; January 2nd, 2008 at 12:26 PM.
    Boris Karadjov
    Brainbench MVP for Visual C++
    http://www.brainbench.com/

  6. #6
    Join Date
    Aug 2002
    Location
    India
    Posts
    97

    Post Re: sending unicode over winsock

    wcstombs
    and mbstowcs also can be used

    The wcstombs function converts the wide-character string pointed to by wcstr to the corresponding multibyte characters and stores the results in the mbstr array. The count parameter indicates the maximum number of bytes that can be stored in the multibyte output string (that is, the size of mbstr).

    int i;
    char *pmbbuf = (char *)malloc( MB_CUR_MAX );
    wchar_t *pwchello = L"Hello, world.";

    printf( "Convert wide-character string:\n" );
    i = wcstombs( pmbbuf, pwchello, MB_CUR_MAX );
    printf( "\tCharacters converted: %u\n", i );
    printf( "\tMultibyte character: %s\n\n", pmbbuf );

    mbstowcs
    The mbstowcs function converts count or fewer multibyte characters pointed to by mbstr to a string of corresponding wide characters that are determined by the current locale.

    int i;
    char *pmbnull = NULL;
    char *pmbhello = (char *)malloc( MB_CUR_MAX );
    wchar_t *pwchello = L"Hi";
    wchar_t *pwc = (wchar_t *)malloc( sizeof( wchar_t ));

    printf( "Convert to multibyte string:\n" );
    i = wcstombs( pmbhello, pwchello, MB_CUR_MAX );
    printf( "\tCharacters converted: %u\n", i );
    printf( "\tHex value of first" );
    printf( " multibyte character: %#.4x\n\n", pmbhello );

    printf( "Convert back to wide-character string:\n" );
    i = mbstowcs( pwc, pmbhello, MB_CUR_MAX );
    printf( "\tCharacters converted: %u\n", i );
    printf( "\tHex value of first" );
    printf( " wide character: %#.4x\n\n", pwc );

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured