pure841
pure841

Reputation: 43

Getting website source with C++ - content is repeated

So, I have this source:

#include <winsock2.h>
#include <windows.h>
#include <iostream>
#pragma comment(lib,"ws2_32.lib")
 
using namespace std;
 
int main (){
        WSADATA wsaData;
 
    if (WSAStartup(MAKEWORD(2,2), &wsaData) != 0) {
                cout << "WSAStartup failed.\n";
        system("pause");
                return 1;
    }
 
        SOCKET Socket=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
 
        struct hostent *host;
        host = gethostbyname("www.newegg.com");
 
        SOCKADDR_IN SockAddr;
        SockAddr.sin_port=htons(80);
        SockAddr.sin_family=AF_INET;
        SockAddr.sin_addr.s_addr = *((unsigned long*)host->h_addr);
 
        cout << "Connecting...\n";
        if(connect(Socket,(SOCKADDR*)(&SockAddr),sizeof(SockAddr)) != 0){
                cout << "Could not connect";
                system("pause");
                return 1;
        }
        cout << "Connected.\n";
 
        send(Socket,"GET / HTTP/1.1\r\nHost: www.newegg.com\r\nConnection: close\r\n\r\n", strlen("GET / HTTP/1.1\r\nHost: www.newegg.com\r\nConnection: close\r\n\r\n"),0);
        char buffer[10000];
 
        int nDataLength;
        while ((nDataLength = recv(Socket,buffer,10000,0)) > 0){               
                int i = 0;
                while (buffer[i] || buffer[i] == '\n' || buffer[i] == '\r') {
                        cout << buffer[i];
                        i += 1;
                }
        }
 
        closesocket(Socket);
        WSACleanup();
 
        system("pause");
        return 0;
}

Now, it works getting the source, however, it keeps repeating afterwards. For example:

Notice how it says , then continues? I was wondering how I can avoid that. I know I can limit the buffer, however is there a better solution? Thanks

Upvotes: 0

Views: 807

Answers (1)

Billy ONeal
Billy ONeal

Reputation: 106539

Try changing

    while (buffer[i] || buffer[i] == '\n' || buffer[i] == '\r') {
            cout << buffer[i];
            i += 1;
    }

to

    for(; i != nDataLength; ++i)
            cout << buffer[i];

It looks like you are reading past the end of the buffer into garbage data used by recv and friends. Nobody said the data returned had to be null terminated which is what the while loop assumes for correct behavior.

Upvotes: 1

Related Questions