Click to See Complete Forum and Search --> : Winsock recv() problem !


FrozenEye
April 1st, 2007, 05:07 PM
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...
Host: www.site.com
Connection: Close
...ect

ny one got a clue whats wrong ?!

wildfrog
April 1st, 2007, 06:21 PM
Why are you just peeking at the data?

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:

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:

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

FrozenEye
April 6th, 2007, 02:23 AM
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