CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    May 2012
    Posts
    14

    Retrieving Bit stream from the server through the Socket

    Hi,

    I am trying to retrieve a bitstream from a server using the following command:

    int RecvBytes=recv(Socket,buf,100,0);

    Now the RecvBytes has a result of 20 which means 20 bytes a returned but in buf I have just 2 strange char.

    I need the binary of all the received packet. As, the RecvBytes has 20 Bytes value, I should get 20*8=160 bits but recvbuf has 2 strange variables, casting it to int and converting ASCII value to bits give me just 2*8=16 bits.

    How can I get all 160 bits instead of 16 bits?

    Thank you.

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Retrieving Bit stream from the server through the Socket

    Quote Originally Posted by test user View Post
    int RecvBytes=recv(Socket,buf,100,0);

    Now the RecvBytes has a result of 20 which means 20 bytes a returned but in buf I have just 2 strange char.
    Please, define "strange char".

    BTW, did you debug your code?
    Victor Nijegorodov

  3. #3
    Join Date
    May 2012
    Posts
    14

    Re: Retrieving Bit stream from the server through the Socket

    Yes, the buf is a character array. E.g. My RecvResult is 20 and the data in. buf is just 2 characters i.e. a space and Ò

    From 2 characters, I can only obtain 16 bits while the packet has 20 bytes or 160 bits.

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Retrieving Bit stream from the server through the Socket

    Quote Originally Posted by test user View Post
    Yes, the buf is a character array. E.g. My RecvResult is 20 and the data in. buf is just 2 characters i.e. a space and Ò
    Why are you assuming that there are only two characters? Why not inspect all 20 bytes?

    If recv() returns a 20, believe what it is telling you -- it isn't telling you a lie. You received 20 bytes, and in all likelihood, some of those bytes are NULL characters. So possibly you are assuming that the NULL terminates the data, when that isn't the case at all.

    If you're using functions that work on NULL-terminated strings to view or manipulate the data, then do not use these functions. You're receiving 20 bytes of data, so you have to process all 20 bytes, even if some of those bytes are 0. Using functions such as strlen(), strcpy(), or output statements like printf using "%s" format specifier, or if you use the debugger to display null terminated strings -- these are all wrong approaches to handle your data.

    Regards,

    Paul McKenzie

  5. #5
    Join Date
    May 2012
    Posts
    14

    Re: Retrieving Bit stream from the server through the Socket

    So, if I use the instruction

    int RecvBytes=recv(Socket,buf,100,0);

    And now I have 20 bytes received and stored in buf register, what should I do next to retrieve bitstream from buf register? How can I inspect all the 20 bytes, I have no clue where the 20 bytes are stored?

    Quote Originally Posted by Paul McKenzie View Post
    Why are you assuming that there are only two characters? Why not inspect all 20 bytes?

    If recv() returns a 20, believe what it is telling you -- it isn't telling you a lie. You received 20 bytes, and in all likelihood, some of those bytes are NULL characters. So possibly you are assuming that the NULL terminates the data, when that isn't the case at all.

    If you're using functions that work on NULL-terminated strings to view or manipulate the data, then do not use these functions. You're receiving 20 bytes of data, so you have to process all 20 bytes, even if some of those bytes are 0. Using functions such as strlen(), strcpy(), or output statements like printf using "%s" format specifier, or if you use the debugger to display null terminated strings -- these are all wrong approaches to handle your data.

    Regards,

    Paul McKenzie

  6. #6
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Retrieving Bit stream from the server through the Socket

    [QUOTE=test user;2072641]So, if I use the instruction

    int RecvBytes=recv(Socket,buf,100,0);

    And now I have 20 bytes received and stored in buf register, what should I do next to retrieve bitstream from buf register?
    So you're saying you don't know how to use arrays in C++?
    Code:
    for (int i = 0; i < 20; ++i)
    {
        // buf[i] is the i'th character in the buf array
    }
    I have no clue where the 20 bytes are stored?


    You called recv() and you don't know what the second parameter means?

    The 20 bytes are:

    buf[0], buf[1], buf[2], buf[3], buf[4], ... buf[19].

    Also, what do you mean by "buf register"? The buf is an array of bytes, isn't it?

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; June 23rd, 2012 at 09:47 PM.

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

    Re: Retrieving Bit stream from the server through the Socket

    Quote Originally Posted by test user View Post
    ... but in buf I have just 2 strange char [which in a later post are revealed to be] just 2 characters i.e. a space and Ò

    ... but recvbuf has 2 strange variables, casting it to int and converting ASCII value to bits give me just 2*8=16 bits.
    It's not possible to get the characters codes for "space and Ò" in just 16 bits. The "Ò", for example, is a Unicode character, and for that character alone, at least 16 bits are required.

    As Paul mentions, you are probably not interpreting your received results correctly. You might, for example (and as Paul notes), be using string-based functions to interpret the results. No string-based functions should ever be used directly on the buffer received in a TCP connection.

    Please let us see your code for interpreting the results of the received buffer.

    And please give us more information on the server that is sending the buffer. If you have the code for it, please let us see the sending code too. If you don't have the code, please give us information about the server (e.g., is it an HTTP server on the web, sending data in accordance with a specific protocol?)

    Mike

  8. #8
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: Retrieving Bit stream from the server through the Socket

    Quote Originally Posted by MikeAThon View Post
    [...] The "Ò", for example, is a Unicode character, and for that character alone, at least 16 bits are required.
    Just to note: Actually, it is contained in codepage 1252 at position 0xD2, so it's well possible to represent it in 8 bits in an MBCS build.

    However, admittedly, that's of rather peripheral relevance to the point of the thread...
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

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