Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8842175
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T10:53:46+00:00 2026-06-14T10:53:46+00:00

Why does my code only download half a webpage?? Sometimes it downloads 4x the

  • 0

Why does my code only download half a webpage?? Sometimes it downloads 4x the webpage’s size :S

I cannot find what is wrong which is why I’m asking. Basically, I connect to the socket, send my Request and read the response to a buffer. I tried saving it to a file and printing it to the screen but it prints and saves incomplete data or too much data. I’m not sure if its a buffer-overflow or not or what I’m doing wrong.

Any ideas?

#define _WIN32_WINNT 0x501

#include <iostream>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdio.h>
#include <fstream>

using namespace std;

void Get(string WebPage)
{
    WSADATA wsaData;
    string Address;
    struct addrinfo *result;
    struct sockaddr_in  *sockaddr_ipv4;

    char Buffer[50000] = {0};

    string Header = "GET / HTTP/1.1\r\n";
    Header += "Host: " + WebPage + "\r\n";
    Header += "Connection: close\r\n";
    Header += "\r\n";

    if (WSAStartup(MAKEWORD(2,2), &wsaData) != 0) return;

    SOCKET Socket = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);

    getaddrinfo(WebPage.c_str(), NULL, NULL, &result);
    if (result->ai_family == AF_INET)
    {
        sockaddr_ipv4 = (struct sockaddr_in *) result->ai_addr;
        Address = inet_ntoa(sockaddr_ipv4->sin_addr);
    }
    freeaddrinfo(result);


    SOCKADDR_IN SockAddr;
    memset(&SockAddr, 0, sizeof(SockAddr));
    SockAddr.sin_port = htons(80);
    SockAddr.sin_family = AF_INET;
    SockAddr.sin_addr.s_addr = inet_addr(Address.c_str());

    if(connect(Socket,(SOCKADDR*)(&SockAddr),sizeof(SockAddr)) == SOCKET_ERROR) return;

    if (send(Socket, Header.c_str(), Header.size(), 0) == SOCKET_ERROR) return;
    shutdown(Socket, SD_SEND);

    std::string Response;

    while(true)
    {
        int Val = recv(Socket, Buffer, sizeof(Buffer), 0);
        if (Val == 0)
            break;
        else if (Val == SOCKET_ERROR)
        {
            cout<<"Error!";
        }
        else
        {
            Response += Buffer;
        }
    }

    closesocket(Socket);
    WSACleanup();

    ofstream File;
    File.open("C:/Saved.html");
    File<<Response;
    File.close();
}

int main()
{
    Get("villavu.com");
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T10:53:48+00:00Added an answer on June 14, 2026 at 10:53 am

    Edit: recv isn’t null terminating the data for you – you need to write how much data you received, not just += it.


    Is there any binary data in your response? If so, the

    Response += Buffer;
    

    will stop at the first null character. I would use a vector to store the data from the recv as such:

    vector<char> recvBuffer(50000);
    
    int bytesReceived = recv(socket, recvBuffer.data(), recvBuffer.size(), 0);
    //error checking
    recvBuffer.resize(bytesReceived);
    

    and again store your received data in another vector, copying it back in.

    vector<char> pageContents;
    
    pageContents.insert(pageContents.end(), recvBuffer.begin(), recvBuffer.end());
    

    That wouldn’t explain your 4x data though.

    Another issue I see is that you aren’t zeroing out your buffer after it is used.

    IOW: You need to write how much data you received, not just += the array.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Why does this code only draw a circle once? I cannot for the life
I have write this code but it does work only if I am already
The http://code.google.com/p/pywebkitgtk/ looks great but it seems to be running on linux only. Does
I am trying to generate an <object> tag only embed code and cannot get
I know the ESC key does code completion, but is there a way to
Does this code always evaluate to false? Both variables are two's complement signed ints.
Does this code cause a memory leak: int main(){ int * a = new
Does this code work across all standard compliant C++ compilers (it works with g++)?
This code does not throw an error but the query fails, that is, the
This code does a redirect, but uses a 302 status code: <cflocation url=http://stackoverflow.com addToken=no

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.