Thank you all for the replies.
Salem I understand what you mean about cutting the connection maybe having a negative impact on the server, especially if I did it multiple consecutive times, and certainly don’t want to do that. However as Anonytmouse says all web browsers have the ability to stop connections built in, and the ability to click on a different link before all the data has been recieved from the server. In any case is it not just possible to let the server send the data, but just not actually receive it all at my end however tell the server I did, or tell it to stop sending the data part way through the transfer, thus avoiding a time out.
Anonytmouse thank you for the information on the Libcurl CURLOPT_WRITEFUNCTION function however I could not get it to work. In the end I decided I would just try to download the file using sockets to allow me to be more flexible, and so wrote the following code, however I cannot get it to work.
Code:
//Projected linked with libwsock32.a
#include <winsock2.h>
#include <string>
#include <iostream>
////////////////////////////////////////////////////////////
int main()
{
char buff[512];
WSADATA wsaData;
struct hostent *hp;
unsigned int addr;
struct sockaddr_in server;
const char servername[] = "www.google.co.uk";
// const char filepath[] = "/index.html";
const char filepath_send[] = "GET /index.html \n";
int wsaret=WSAStartup(0x101,&wsaData);
if(wsaret)
return 0;
SOCKET conn;
conn=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if(conn==INVALID_SOCKET)
{
std::cout << "socket() error: " << WSAGetLastError() << std::endl;
system ("PAUSE");
return 0;
}
if(inet_addr(servername)==INADDR_NONE)
{
hp=gethostbyname(servername);
}
else
{
addr=inet_addr(servername);
hp=gethostbyaddr((char*)&addr,sizeof(addr),AF_INET);
}
if(hp==NULL)
{
std::cout << "gethostbyname() error: " << WSAGetLastError() << std::endl;
system ("PAUSE");
closesocket(conn);
return 0;
}
server.sin_addr.s_addr=*((unsigned long*)hp->h_addr);
server.sin_family=AF_INET;
server.sin_port=htons(80);
int y;
y = connect(conn,(struct sockaddr*)&server,sizeof(server));
if( y == SOCKET_ERROR)
{
closesocket(conn);
std::cout << "connect() error: " << WSAGetLastError() << std::endl;
system ("PAUSE");
return 0;
}
// std::cout << filepath_send << std::endl;
y = send(conn,filepath_send,strlen(filepath_send),0);
if( y == SOCKET_ERROR)
{
closesocket(conn);
std::cout << "send() error: " << WSAGetLastError() << std::endl;
system ("PAUSE");
return 0;
}
while(1)
{
std::cout << "GOT HERE" << std::endl;
y = recv(conn,buff,512,0);
if (y == SOCKET_ERROR)
{
std::cout << "recv() error: " << WSAGetLastError() << std::endl;
break;
}
std::cout << "Bytes recieved: " << y << std::endl;
if (y == 0)
break;
std::cout << buff << std::endl;
}
closesocket(conn);
WSACleanup();
system ("PAUSE");
return 0;
}
If I try www.cprogramming.com/begin.html I get 404 Not Found
If I try www.google.co.uk/index.html I get an error from the recv() part of the code: 10053 which is “Software caused connection abort. A connection was aborted by the software in your machine, possibly due to a TCP/IP configuration error, data transmission time-out or protocol error.”
Any help would be much appreciated.