Unusual HTTP Response in Basic C++ Socket Programming -


I have found an original HTTP client installed in C ++, which still works fine. This is for school work, so there is a lot to do, but I have a problem.

Once I use the recv () function in the loop, buffer to add a piece of reaction to my response repeatedly, and then each time the buffer output problem is that every piece of the reaction At the end, the HTTP request is also being attacked. For example, the response will be a part of the source code of the page, then "GET / HTTP / 1.1 ...", after the next part, and then "GET ..." again, and so on.

Here is my relevant code:

  // REQUEST request request [] = "Head / INDEX HTTP / 1.1 \ r \ nHOST: www.google.com \ r \ NConnect: Close \ r \ n \ r \ n "; // send request len ​​= send (sockfd, request, sizeof (request), 0); // Write / output while responding (recv (sockfd, buf, sizeof (buf), 0)! = 0) {// Read & amp; Product feedback printf ("% s", buf); }  

buffer zero is not finished, which is necessary for wire in C ++ When you see "Extra GET", you are seeing the memory that you should not be stdlib tried to print your buffer, but never got the '0 0' character.

A quick fix is ​​to force buffer to end:

  int n = 1; While (n> gt; 0) {n = rqv (sockfd, buf, sizeof (buf), 0); If (n> gt; 0) {// buffer buffer is finished so that we can print it [n] = '\ 0'; // Output response printf ("% s", buf); }}  

Comments

Popular posts from this blog

MySql variables and php -

url rewriting - How to implement the returnurl like SO in PHP? -

Which Python client library should I use for CouchdB? -