阅读 89

download a file from a url in C++ Ubuntu Forums

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 argcchar** 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_INETSOCK_STREAM0)) == -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_addrhp->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 *)&servaddrsizeof(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


slavik is offline


文章分类
代码人生
版权声明:本站是系统测试站点,无实际运营。本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 XXXXXXo@163.com 举报,一经查实,本站将立刻删除。
相关推荐