download a file from a url in C++ - Ubuntu Forums
Re: download a file from a url in C++
Here's a C version.
PHP Code:
/*
* wget_sortof.c
*
* Copyright 2007 Vyacheslav Goltser <slavikg@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
/* get the main page from google.com */
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
int main(int argc, char** argv)
{
struct sockaddr_in servaddr;
struct hostent *hp;
int sock_id;
char message[1024*1024] = {0};
int msglen;
char request[] = "GET /index.html HTTP/1.0\n"
"From: slava!!!\nUser-Agent: wget_sortof by slava\n\n";
//Get a socket
if((sock_id = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
fprintf(stderr,"Couldn't get a socket.\n"); exit(EXIT_FAILURE);
}
else {
fprintf(stderr,"Got a socket.\n");
}
//book uses bzero which my man pages say is deprecated
//the man page said to use memset instead. :-)
memset(&servaddr,0,sizeof(servaddr));
//get address for google.com
if((hp = gethostbyname("google.com")) == NULL) {
fprintf(stderr,"Couldn't get an address.\n"); exit(EXIT_FAILURE);
}
else {
fprintf(stderr,"Got an address.\n");
}
//bcopy is deprecated also, using memcpy instead
memcpy((char *)&servaddr.sin_addr.s_addr, (char *)hp->h_addr, hp->h_length);
//fill int port number and type
servaddr.sin_port = htons(80);
servaddr.sin_family = AF_INET;
//make the connection
if(connect(sock_id, (struct sockaddr *)&servaddr, sizeof(servaddr)) != 0) {
fprintf(stderr, "Couldn't connect.\n");
}
else {
fprintf(stderr,"Got a connection!!!\n");
}
//NOW THE HTTP PART!!!
//send the request
write(sock_id,request,strlen(request));
//read the response
msglen = read(sock_id,message,1024*1024);
printf("response is %d bytes long\n", msglen);
//print the reasponse
printf("%s", message);
return 0;
}
__________________
I am infallible, you should know that by now.
"My favorite language is call STAR. It's extremely concise. It has exactly one verb '*', which does exactly what I want at the moment." --Larry Wall
(02:15:31 PM) ***TimToady and snake oil go way back...
42 lines of Perl - SHI - Home Site