Originally posted by Hammer
I'm interested in helping you, can you post something that'll compile?
BTW, please don't bump your threads.
Sorry about the bump, actions of a desperate man I guess
Code:
#include "stdafx.h"
#include <afx.h>
#include "winsock2.h"
#include <iostream>
#include <fstream>
using namespace std;
int main(int argc, char* argv[])
{
char buf[2000];
int socketfd;
WSADATA wsaData;
if (WSAStartup(MAKEWORD( 2, 2 ), &wsaData ) != 0 )
{
cout << "Wsastartup failed" << endl;
exit(0);
}
if ( LOBYTE( wsaData.wVersion ) != 2 || HIBYTE( wsaData.wVersion )!= 2)
{
WSACleanup( );
exit(0);
}
if ((socketfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1)
{
cout << "socket failed" << endl;
exit(0);
}
string domain = "www.google.com";
string path = "/images/logo.gif";
LPHOSTENT server;
server = gethostbyname(domain.c_str());
if (server == 0)
{
cout << "server failed" << endl;
exit(0);
}
SOCKADDR_IN dest_addr;
dest_addr.sin_family = AF_INET;
dest_addr.sin_addr.s_addr = *(unsigned long int*)server->h_addr_list[0];
dest_addr.sin_port = htons(80);
if (connect(socketfd, (struct sockaddr*)&dest_addr, sizeof(dest_addr)) == -1)
{
cout << "connect failed" << endl;
exit(0);
}
string command = "GET ";
command.append(path);
command.append(" HTTP/1.1\nhost:");
command.append(domain);
command.append("\r\n\r\n");
if (send(socketfd, command.c_str(), command.length(), 0) == -1)
{
cout << "send failed" << endl;
exit(0);
}
fstream ofile("logo.gif", ios::out | ios::binary);
string buffer;
int recv_bytes;
while((recv_bytes = recv(socketfd, buf, sizeof(buf),0)) > 0)
{
ofile.write(buf, recv_bytes);
cout << "#";
}
cout << endl;
ofile.close();
closesocket(socketfd);
WSACleanup();
cout << "Finished" << endl;
return 0;
}
This code should create a file logo.gif and save the data to it. For the moment I have to removed the http header info that also is writen to the file manually via notepad. Will try and implement a removeHeader() method as soon as I know this will actually work.
Appreciate any response.