title: Made a socket in C++ in winsock and connected to google and am getting some garbage at the end of the response
body: <p>I made a simple web browser and I'm able to connect to Google on the page <code>www.google.com/?gws_rd=ssl</code> and I get all the http information like <code>Cache-Control:</code>, <code>Content-Type:</code> etc but the last part after that stuff comes out as garbage, here's what it gives me: </p>
  
  <pre><code>HTTP/1.1 200 OK Date: Wed, 19 Aug 2015 03:38:22 GMT
  Expires: -1
  Cache-Control: private, max-age=0
  Content-Type: text/html; charset=UTF-8
  P3P: CP="This is not a P3P policy! See http://www.google.com/support/accounts/bi
  n/answer.py?hl=en&amp;answer=151657 for more info."
  Content-Encoding: gzip
  Server: gws
  Content-Length: 14534
  X-XSS-Protection: 1; mode=block
  X-Frame-Options: SAMEORIGIN
  Set-Cookie: PREF=ID=1111111111111111:FF=0:TM=1439955502:LM=1439955502:V=1:S=juTG
  4tnW5tH5E6BF; expires=Fri, 18-Aug-2017 03:38:22 GMT; path=/; domain=.google.com
  Set-Cookie: NID=70=glka-fZspN0pbH8M2m0Oay-dXvaHNwyMqKB-O4dn7ZgObHTbnn7dyc4cfn_uX
  XKOobdvfpksNRCV5uM1QWPGXV_MjfxdvW2aQQ4zrEtfdxnZ4IxuPQFM61B_ZI0wDTsJ; expires=Thu
  , 18-Feb-2016 03:38:22 GMT; path=/; domain=.google.com; HttpOnly
  
  ▼ï
  Bytes received: 3316
  From server: ⁿh╖6Ü╗ú»╠╟²ú}╪φ▼²h▌&lt;¢Füg┴½ó┬k■Θµ¢←┤┼
  ƒz~ßD╓$╕lw[²▬⌡G¢╕*r|!╩╓⌂╫ñäk┬ÿ.─E▬ÿ≈╨▓ W@D╠y√┐☺àY{J╙Z"╞ëf±4▄aeP↨⌐"╔S¡²C╟╜,?⌠╢|║┤
  ÿ√╢φûΓå-╔╖n¢&lt;╞3|(₧&amp;-╖ë&gt;aJVz╛ÖF┌§Θº`á╙⌠≤mlk·ñu
  ╞?╧m▄û¢ú┘♣âúΘéö&gt;◄╣Θ╝░që└â₧ƒ┼▓┐l·Äu╛▀û½^Ä│⌠kΩò²╔╗ú▼m`Ä ⁿh▼=nπ±@╣╝²²?Γå▒æ~╒ⁿ╟Å]ΦF‼
  ╚╒R╒Öêß^╧╪O7ΣB?ºß∩φl█.âq)≈«(R→τå┌&amp;«DZ¡(╘╬α◄╗ív:Fñk!}8Ü ┐╬♂E.♥k╘Q$ƒ§%
  </code></pre>
  
  <p>Here's my code:</p>
  
  <pre><code>#include &lt;windows.h&gt;
  #include &lt;winsock2.h&gt;
  #include &lt;conio.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;iostream&gt;
  using namespace std;
  #define SCK_VERSION2 0x0202
  #define DEFAULT_BUFLEN 12000
  #define DEFAULT_PORT 27015
  
  namespace Globals{
      extern string input = "";
  }
  using namespace Globals;
  
  int sck() {
      //----------------------
      // Declare and initialize variables.
      WSADATA wsaData;
      int iResult;
  
      SOCKET ConnectSocket = INVALID_SOCKET;
      struct sockaddr_in clientService;
  
      char name[500] = "";
      char ipADDRESS[500] = "";
      char sPORT[500] = "";
  
      sockaddr_in sName;
      int sNameSize =  sizeof(sName);
  
      char const* sendbuf = "GET /?gws_rd=ssl  HTTP/1.1\r\n"
      "Host: www.google.com:80\r\n"
      "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5 (.NET CLR 3.5.30729)\r\n"
      "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n"
      "Accept-Language: en-us,en;q=0.5\r\n"
      "Accept-Encoding: gzip,deflate\r\n"
      "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\r\n"
      "Keep-Alive: 300\r\n"
      "Connection: keep-alive\r\n"
      "Pragma: no-cache\r\n"
      "DNT: 1"
      "Cache-Control: no-cache\r\n\r\n";
      char recvbuf[DEFAULT_BUFLEN];
      int recvbuflen = DEFAULT_BUFLEN;                                    //23.214.132.132 GoDaddy.com
      int WSAERROR = WSAGetLastError();                                   
      //system("color 04");                                               
      //----------------------
      // Initialize Winsock
      iResult = WSAStartup(MAKEWORD(2,2), &amp;wsaData);
      if (iResult != NO_ERROR) {
        printf("WSAStartup failed: %d\n", iResult);
        return 1;
      }
  
      //----------------------
      // Create a SOCKET for connecting to server
      ConnectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
      if (ConnectSocket == INVALID_SOCKET) {
          printf("Error at socket(): %i\n", WSAGetLastError() );
          WSACleanup();
          return 1;
      }
  
      //----------------------
      // The sockaddr_in structure specifies the address family,
      // IP address, and port of the server to be connected to.
  
      printf("IP ADDRESS: 74.125.196.191 is Google \n");
      cin &gt;&gt; ipADDRESS;
      printf("PORT: \n");
      cin &gt;&gt; sPORT;
      u_short PORT = strtoul(sPORT, NULL, 0);
      clientService.sin_family = AF_INET;
      clientService.sin_addr.s_addr = inet_addr(ipADDRESS);                            
      clientService.sin_port = htons(PORT); 
      //----------------------
      // Connect to server.
      iResult = connect( ConnectSocket, (SOCKADDR*) &amp;clientService, sizeof(clientService) );
      if ( iResult == SOCKET_ERROR) {
          closesocket (ConnectSocket);
          printf("Unable to connect to server: %i\n", WSAGetLastError());
          WSACleanup();
          return 1;
      }
  
      //----------------------
      //Get local host name
      iResult = gethostname(name, sizeof(name));
      if (iResult == NO_ERROR) {
          printf("Host Name: %s\n", name);
      }
      else if (iResult == SOCKET_ERROR) {
          printf("Could not resolve host name: %i", WSAGetLastError());
      }
  
      //------------------------
      //Get peer name
      iResult = getpeername(ConnectSocket, (struct sockaddr*)&amp;sName, &amp;sNameSize);
      if (iResult == NO_ERROR)
          printf("Peer Name: %s\n", inet_ntoa(sName.sin_addr));
      else if (iResult == SOCKET_ERROR)
          printf("Could not get peer name: %i\n", WSAGetLastError());
  
      //-------------------------
      // Send an initial buffer
      iResult = send( ConnectSocket, sendbuf, (int)strlen(sendbuf), 0 );
      if (iResult == SOCKET_ERROR) {
          printf("send failed: %d\n", WSAGetLastError());
          closesocket(ConnectSocket);
          WSACleanup();
          return 1;
      }
      else
          printf("Bytes Sent: %i\n", iResult);
  
      //-----------------------------
      // shutdown the connection since no more data will be sent
      iResult = shutdown(ConnectSocket, SD_SEND);
      if (iResult == SOCKET_ERROR) {
          printf("shutdown failed: %d\n", WSAGetLastError());
          closesocket(ConnectSocket);
          WSACleanup();
          return 1;
      }
  
      // Receive until the peer closes the connection
      do {
  
          iResult = recv(ConnectSocket, recvbuf, recvbuflen, 0);
          if ( iResult &gt; 0 ) {
              printf("Bytes received: %d\n", iResult); //printf("Bytes received: %d\n", iResult);
              printf("From server: %s\n", recvbuf);
          }
          else if ( iResult == 0 )
              printf("Connection closed\n");
          else if (WSAERROR == WSAETIMEDOUT)
              printf("recv failed: WSAETIMEDOUT\n");
      } while( iResult &gt; 0 );
  
      // cleanup
      closesocket(ConnectSocket);
      WSACleanup();
      system("PAUSE");
      return 0;
  }
  
  int main() {
          sck();
  }
  </code></pre>