This is my code so far to connect to a webpage and retrieve the html:

#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.cplusplus.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.cplusplus.com\r\nConnection: close\r\n\r\n", strlen("GET / HTTP/1.1\r\nHost: www.cplusplus.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] >= 32 || buffer[i] == '\n' || buffer[i] == '\r') {
			cout << buffer[i];
			i += 1;
		}
	}

	closesocket(Socket);
        WSACleanup();

	system("pause");
	return 0;
}

The first problem is that the html code it displays is only part of the code, it doesn't reach the final </html> at the bottom. Also, how would I change to code to connect to something like www.cplusplus.com/forums? Would I change the Host in the request? Or in the gethostbyname part? Or both?

Anyone have any ideas?

actually i just tried executiing your code and i got various errors in it.

undefined reference to `WSAStartup@8'
D:\socket programs\prog1.o(.text+0x124):prog1.cpp: undefined reference to `socket@12'
D:\socket programs\prog1.o(.text+0x13a):prog1.cpp: undefined reference to `gethostbyname@4'
D:\socket programs\prog1.o(.text+0x14d):prog1.cpp: undefined reference to `htons@4'
D:\socket programs\prog1.o(.text+0x19f):prog1.cpp: undefined reference to `connect@12'
D:\socket programs\prog1.o(.text+0x203):prog1.cpp: undefined reference to `send@16'
D:\socket programs\prog1.o(.text+0x21c):prog1.cpp: undefined reference to `recv@16'
D:\socket programs\prog1.o(.text+0x2ac):prog1.cpp: undefined reference to `closesocket@4'
D:\socket programs\prog1.o(.text+0x2b4):prog1.cpp: undefined reference to `WSACleanup@0'
Process terminated with status 1 (0 minutes, 0 seconds)

Well i am sorry , i was intending to help you , but seem like i myself have got several doubts , any idea on why is the error being displayed .?

That's strange... I don't get any of those errors... all of those calls are in winsock2.h but it didn't say anything about not finding the file... unless maybe you have an older version of winsock?

You don't need Winsock.
Just use Wininet (1 line of code with URLDLTF) or COM (6 lines)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.