Hello everyone, I would like your help to decide which is the best way
to download a file in C++, if any of the three forms below, or if you
know any better.

1) using *fork* and *execl* to _execute a program_ (working)

-----
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>

// $ g++ download.cpp
// $ ./a.out

int main(int argc, char **argv) {
    pid_t child = 0;
    int pipes[2];

    if(pipe( pipes ) != 0) {
        fprintf( stderr, "cannot create pipe" );
        return 1;
    }

    child = fork();

    if (child < 0) {
        fprintf( stderr, "process failed to fork\n" );
        return 1;
    }
    if (child == 0) {
        close(pipes[1]);
        // read from pipes[0] here
//        wait(NULL);
    }
    else {
        dup2(pipes[1], 0);        // 0 is stdout
        close(pipes[0]);

                                system("echo -n 'Start Download'");
                                execl("/usr/bin/aria2c", "aria2c
","http://curl.haxx.se/download/curl-7.21.4.tar.bz2";, (char *)0); use
aria2c or axel or wget or curl
                                system("echo -n 'Download Finish'"); // this 
line will never execute
    }
    return 0;
}
-----


2) using a _library_:  *curl* (working)

-----
#define CURL_STATICLIB
#include <stdio.h>
#include <curl/curl.h>
#include <curl/types.h>
#include <curl/easy.h>
#include <string>

// sudo aptitude install libcurl3 libcurl3-dev  or
http://curl.haxx.se/download.html
// g++ -Wall -std=c++98 -pedantic -Werror -lcurl -I /usr/local/include
-L /usr/local/lib

size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *stream) {
    size_t written;
    written = fwrite(ptr, size, nmemb, stream);
    return written;
}

int main(void) {
    CURL *curl;
    FILE *fp;
    CURLcode res;
    const char *url = "http://curl.haxx.se/download/curl-7.21.4.tar.bz2";;
    const char outfilename[FILENAME_MAX] = "curl-7.21.4.tar.bz2";
    curl = curl_easy_init();
    if (curl) {
        fp = fopen(outfilename,"wb");
        curl_easy_setopt(curl, CURLOPT_URL, url);
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
        res = curl_easy_perform(curl);
        curl_easy_cleanup(curl);
        fclose(fp);
    }
    return 0;
}
-----


3) using *connect* ... *websock*  (this algorithm is not working)

-----
struct sockaddr_in webaddress;
int websock;

webaddress.sin_family = AF_INET;
webaddress.sin_addr.s_addr = inet_addr("http://www.google.com/index.html";);
webaddress.sin_port = htons(80);
if(connect(websock, (struct sockaddr *) &webaddress, sizeof(webaddress))
< 0) {
        perror("Failed to connect with the server \n");
        // TODO: printf or send msg back, or just create blank file
}
// Try to read from website, give error if not possible
//else if(( received = recv(websock,buffer,BUFFSIZE-1, 0)) < 0 )
else if(( received = read(websock,buffer,BUFFSIZE-1)) < 0 ) {
        perror("Failed to receive additional bytes from client \n");
}
// Read successful
else if(received > 0) {
        printf("Received from Client : %s", buffer);
        memset(buffer, 0, sizeof(buffer));
}
// Close the client if badness occurred
else if(received < 1) {
        printf("Closing this client (1)\n");
        close(websock);
        //pthread_exit(NULL);
}

// attempt download index.html
// send file back
// save file to local directory
-----


4) or if you know any better...


Thank you.

John


------------------------------------------------------------------------------
Enable your software for Intel(R) Active Management Technology to meet the
growing manageability and security demands of your customers. Businesses
are taking advantage of Intel(R) vPro (TM) technology - will your software 
be a part of the solution? Download the Intel(R) Manageability Checker 
today! http://p.sf.net/sfu/intel-dev2devmar
_______________________________________________
witty-interest mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/witty-interest

Reply via email to