CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jun 2006
    Posts
    61

    Winsock recv() problem !

    Code:
    char buff[5];
    CString htmlSource;
    int rb=recv(conn,buff,sizeof(buff),MSG_PEEK);
    while(rb>0)
    {
    rb=recv(conn,buff,sizeof(buff),MSG_PEEK);
    htmlSource+=(CString)buff;
    }
    return htmlSource;
    A very usuall recv() !
    I use it to get a http request's response from a server
    the problem is that i always recieve the response followed by request i sent !!!
    which looks like that...
    Code:
    Host: www.site.com
    Connection: Close
    ...ect
    ny one got a clue whats wrong ?!

  2. #2
    Join Date
    Apr 2005
    Location
    Norway
    Posts
    3,934

    Re: Winsock recv() problem !

    Why are you just peeking at the data?
    Code:
    rb=recv(conn,buff,sizeof(buff),MSG_PEEK);
    You append the data whitout checking if any (or how much) data was received. If you want to treat data received using recv as a c-string (zero ended), you need to add the zero yourself. Something like:
    Code:
    while(rb>0)
    {
        rb=recv(conn, buff, sizeof(buff) - 1, 0); // make room for ending zero
        buff[rb] = '\0'; // add ending zero
        htmlSource += (CString)buff;
    }
    And maybe you're better off with a do-while statement:
    Code:
    char buff[1024];
    CString htmlSource;
    do
    {
        rb=recv(conn, buff, sizeof(buff) - 1, 0);
        buff[rb] = '\0'; // add ending zero
        htmlSource += (CString)buff;
    } while (rb > 0);
    
    return htmlSource;
    - petter

  3. #3
    Join Date
    Jun 2006
    Posts
    61

    Re: Winsock recv() problem !

    i peeked data coz i always got the recv function to return the same # of bytes recieved with no new data !...and so i got into an endless loop

    ny ur code snippet helped alot...thnx mate

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