0

In this function, passing string as an argument (which is having the huge amount of data as a string)...

SendBytes method is defined like this

bool NetOutputBuffer_c::SendBytes ( const void * pBuf, int iLen )
{
        BYTE * pMy = (BYTE*)pBuf;

        while ( iLen>0 && !m_bError )
        {
                int iLeft = m_iBufferSize - ( m_pBufferPtr-m_pBuffer );
                if ( iLen<=iLeft )
                {
                        printf("iLen is %d\n",iLen);
                        memcpy ( m_pBufferPtr, pMy, iLen );
                        m_pBufferPtr += iLen;
                        break;
                }

                ResizeIf ( iLen );
        }
        return !m_bError;
}

bool NetOutputBuffer_c::SendCompressedString ( std::string sStr )
{
    std::cout << sStr << endl;
    const char *cStr = sStr.c_str();
    std::cout << cStr <<endl;

    int iLen = cStr ? strlen(cStr) : 0;
    SendInt ( iLen );
    return SendBytes ( cStr, iLen );
}

Tried printing the value of sStr to check whether it has proper data or not.

Then converting the std::string to const char *

After converting it,tried printing the value of cStr. But it(cStr) actually contains 5% of the actual data(sStr)...

what shall I need to do in order to get the whole data?

Can someone guide me in this regard?

  • 2
    Does your string have embedded null (`'\0'`) characters? – Benjamin Lindley Aug 21 '14 at 06:34
  • The string has the compressed data like this xÚì½ëRãȶ.ú¿"ö;(Ö<8f>¹ºã0½ò®Ì½^?^Y0à^B^[^ZCÝNìP^H[`<81>mQ¾@¹Vì<88>ó^ZçõÎ<93><9c><91>)ù<86>.¶<85>^L6ݽÖìÆ^^<99><92>,¥ò^[×o|úïÿ¸õ½Nkð^_ÿóÿþ<8f>®;ìû~ë?öþc0ºiº<83>¡^G^?Þ^EÃvß<85>?^^ûÁ­ßñZÞ Ù÷^_<87>~Ð<9b>^[è<98>iáXý÷ÿÞû^Ow8ì<9b>ÃF^Sý^Ëû^E<83>:nïnäÞéc^O<86>îp4<80>?<9e>Ü<8e>ßr<87><9e>><88>;^Z¶<83>¾ÿÛ|ðzÃþx8~4£^_½¦ïv^^ûþÓìrÚþ]»^Cÿ^[N<8f>õ^X<{ýG·ù0ý¦çvÍ/ñà úú^LæämOO<83>?ü^³«<87>uáê½þ0<80>Ë<9b>;^]ü<ý©ïÁYÂß<¹3± – user3532122 Aug 21 '14 at 06:38
  • Compressed data is binary format and you probably should use std::cout.write() rather than formatted << output. @Benjamin is right you probably have null characters that indicate the end of a c-string. PS. you can't see null characters from a printout or in an editor. – Galik Aug 21 '14 at 06:41
  • Are you sure? What do you get if you do this? `int x = std::find(sStr.begin(), sStr.end(), '\0') - sStr.begin(); std::cout << x;` – Benjamin Lindley Aug 21 '14 at 06:44
  • @Galik:can u please edit that code and tell me how to get the whole data.? – user3532122 Aug 21 '14 at 06:44
  • Try this: bool NetOutputBuffer_c::SendCompressedString ( std::string sStr ) { SendInt ( sStr.size() ); return SendBytes ( sStr.c_str(), sStr.size() ); } **[see Benjamin's answer]** – Galik Aug 21 '14 at 06:48
  • @BenjaminLindley:yes it contain null values in between – user3532122 Aug 21 '14 at 06:53
  • Your problem is that you use a string for storing something else than a string. For arbitrary data, use `std::vector`. – Quentin Aug 21 '14 at 07:44

1 Answers1

4

strlen works for c-strings. That is, null terminated strings. It searches for a '\0' character, and returns that character's position as the length of the string. You have compressed binary data, not a c-string. It almost certainly contains a 0 before the end of the string. Use sStr.size() instead.

SendInt ( sStr.size() );
return SendBytes ( sStr.c_str(), sStr.size() );
Benjamin Lindley
  • 101,917
  • 9
  • 204
  • 274
  • Benjamin Lindley:ok.Still I'm getting the same issue...can u please check and tell me whether i've implemented the correct code which u said. bool NetOutputBuffer_c::SendCompressedString ( std::string sStr ) { std::cout << sStr << endl; // const char *cStr = sStr.c_str(); // std::cout << cStr < – user3532122 Aug 21 '14 at 07:28
  • @user3532122: Is the call to `SendBytes` working? Printing out the c-string still won't work for the same reason that strlen doesn't work. Do you really need that? I assumed it was just your attempt at debugging the failed call to `SendBytes`. – Benjamin Lindley Aug 21 '14 at 07:41
  • But if you need it to print the whole string from the c-string (I can't imagine why), then use `std::cout.write(sStr.c_str(), sStr.size());` – Benjamin Lindley Aug 21 '14 at 07:43
  • i can able to print the data by following the method what u said....But Now i need to send the whole data which is available in write(sStr.c_str(),sStr.size()) as Byte * .What shall i need to do inorder to make it working? – user3532122 Sep 16 '14 at 06:38