Brandon
Brandon

Reputation: 23510

Sockets downloading too little or too much of a webpage

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");
}

Upvotes: 0

Views: 226

Answers (1)

Ryan Guthrie
Ryan Guthrie

Reputation: 688

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.

Upvotes: 3

Related Questions