c++ - Weird behavior in an HTTP request with Winsock2 -
i'm trying implement http request in c++ using winsock 2.
after connecting http://stackoverflow.com, here's im sending:
send(_socket, "get / http/1.1\r\nhost: www.stackoverflow.com\r\n\r\n", (the length ), 0);
and reading response:
std::string s; char buffer[buffer_length]; int bytes = 0; { zeromemory(buffer, buffer_length); bytes = recv(_socket, buffer, buffer_length, 0); s.append(buffer); } while (bytes == buffer_length); return s;
by way: #define buffer_length 512
here's come weird part, when run program response's headers part:
http/1.1 200 ok cache-control: public, max-age=34 content-type: text/html; charset=utf-8 expires: wed, 04 sep 2013 08:14:00 gmt last-modified: wed, 04 sep 2013 08:13:00 gmt vary: * x-frame-options: sameorigin date: wed, 04 sep 2013 08:13:26 gmt content-length: 193288
but when debug read function step step response including content. if put breakpoint on zeromemory() function , continue execute code pressing f10 key (step over) advances 1 "command" @ time, loop goes on few times exists , returns full response, both headers , content. if put breakpoint after recv() function (s.append(buffer)) loop exists (after 1 execution of loop performed) , retruns headers.
someone can explain me behavior?
what assume there kind of timed delay between sending headers , content, when stop before recv() debugger server have enough time send , recv() can read all, when put after, server had enough time send headers , recv() function reads what's arrived , return less 512 bytes read, , loop exists. right?
thanks in advance!
the assumption right, mentioned in comment can less buffer size if data has not arrived yet. should in loop use knowledge of http , example \r\n\r\n
know when headers have arrived, lookup content length , try read amount of data content. or alternatively, since on winsock2
use methods httpsendrequest
(http://msdn.microsoft.com/en-us/library/windows/desktop/aa384247(v=vs.85).aspx) , friends - less issues if want send requests.
Comments
Post a Comment