Http file download, howto

Discuss the development of new homebrew software, tools and libraries.

Moderators: cheriff, TyRaNiD

Post Reply
aaa801
Posts: 2
Joined: Wed Apr 29, 2009 12:27 am

Http file download, howto

Post by aaa801 »

Hea, does anyone have a sample code for connecting to a ap and downloading a file, i have the latest toolchain running on windows (dont ask lol), need urgently for a project
jojojoris
Posts: 255
Joined: Sun Mar 30, 2008 4:06 am

Post by jojojoris »

Do you want a easy one with my libcurl+EasyConnect?

http://jojosoft.1free.ws/viewtopic.php?f=7&t=10

Code: Select all

#include <pspkernel.h>
#include <pspdebug.h>

#include <stdio.h>

#include <curl/curl.h>

#include "easyconnect.h"

PSP_MODULE_INFO&#40;"Inet Sample", 0, 1, 1&#41;;
PSP_MAIN_THREAD_ATTR&#40;THREAD_ATTR_USER&#41;;

/* Exit callback */
int exit_callback&#40;int arg1, int arg2, void *common&#41; &#123;
    sceKernelExitGame&#40;&#41;;
    return 0;
&#125;

/* Callback thread */
int CallbackThread&#40;SceSize args, void *argp&#41; &#123;
    int cbid;

    cbid = sceKernelCreateCallback&#40;"Exit Callback", exit_callback, NULL&#41;;
    sceKernelRegisterExitCallback&#40;cbid&#41;;

    sceKernelSleepThreadCB&#40;&#41;;

    return 0;
&#125;

/* Sets up the callback thread and returns its thread id */
int SetupCallbacks&#40;void&#41; &#123;
    int thid = 0;

    thid = sceKernelCreateThread&#40;"update_thread", CallbackThread, 0x11, 0xFA0, 0, 0&#41;;
    if &#40;thid >= 0&#41; &#123;
        sceKernelStartThread&#40;thid, 0, 0&#41;;
    &#125;

    return thid;
&#125;

size_t my_fwrite&#40;void *buffer, size_t size, size_t nmemb, void *stream&#41; &#123;
    FILE * pFile = fopen&#40;"tmp.tmp","w"&#41;;
    size_t sizecontent = fwrite&#40;buffer, size, nmemb, pFile&#41;;
    fclose&#40;pFile&#41;;
    return sizecontent;
&#125;

int main&#40;&#41; &#123;
    SetupCallbacks&#40;&#41;;

    EasyConnect connection;
    connection.InitGU&#40;&#41;;
    connection.Connect&#40;&#41;;
    connection.TermGU&#40;&#41;;

    pspDebugScreenInit&#40;&#41;;

    pspDebugScreenPrintf&#40;"Trying to download&#58;\nhttp&#58;//www.google.com\n\n"&#41;;

    if &#40;connection.IsConnected&#40;&#41;&#41; &#123;
        CURL *curl;
        CURLcode res;
        curl = curl_easy_init&#40;&#41;;
        if &#40;curl&#41; &#123;
            curl_easy_setopt&#40;curl, CURLOPT_URL, "http&#58;//www.google.com"&#41;;
            curl_easy_setopt&#40;curl, CURLOPT_WRITEFUNCTION, my_fwrite&#41;;

            res = curl_easy_perform&#40;curl&#41;;
            curl_easy_cleanup&#40;curl&#41;;

            if &#40;res == CURLE_OK&#41; &#123;
                pspDebugScreenPrintf&#40;"File downloaded.\n\n\n"&#41;;
                FILE * pFile = fopen&#40;"tmp.tmp","r+"&#41;;
                long int size;
                fseek&#40;pFile, 0, SEEK_END&#41;;
                size = ftell&#40;pFile&#41;;
                rewind&#40;pFile&#41;;
                char content&#91;size + 1&#93;;
                memset&#40;content,0,size+1&#41;;
                fread&#40;content, size, 1, pFile&#41;;
                fclose&#40;pFile&#41;;
                pspDebugScreenPrintf&#40;content&#41;;
                
            &#125; else &#123;
                pspDebugScreenPrintf&#40;"Something went wrong with libcurl&#58; error %i\n", res&#41;;
            &#125;

            

        &#125; else &#123;
            pspDebugScreenPrintf&#40;"Something went wrong with libcurl\n"&#41;;
        &#125;

    &#125; else &#123;
        pspDebugScreenPrintf&#40;"You must connect to an access point to use this sample\n"&#41;;
    &#125;

    while &#40;1&#41; &#123;
        sceKernelDelayThread&#40;10000&#41;;
    &#125;

    sceKernelExitGame&#40;&#41;;
    return 0;
&#125;

Code: Select all

int main&#40;&#41;&#123;
     SetupCallbacks&#40;&#41;;
     makeNiceGame&#40;&#41;;
     sceKernelExitGame&#40;&#41;;
&#125;
aaa801
Posts: 2
Joined: Wed Apr 29, 2009 12:27 am

Post by aaa801 »

Thanks, now i can start my project =D
m0skit0
Posts: 191
Joined: Tue Jun 02, 2009 8:58 pm

Post by m0skit0 »

One thing is CSMA/CA (Wifi) connection, and another thing is TCP/IP and plus HTTP. Dont confuse terms.
The Incredible Bill Gates wrote:The obvious mathematical breakthrough would be development of an easy way to factor large prime numbers.
foxss
Posts: 6
Joined: Sun Dec 07, 2008 2:00 pm

zlib error

Post by foxss »

Looking for help, I got problem when try to do make.

Code: Select all

/usr/local/pspdev/lib/gcc/psp/4.3.2/../../../../psp/lib/libcurl.a&#40;content_encodi
ng.o&#41;&#58; In function `exit_zlib'&#58;
content_encoding.c&#58;&#40;.text+0xa0&#41;&#58; undefined reference to `inflateEnd'
/usr/local/pspdev/lib/gcc/psp/4.3.2/../../../../psp/lib/libcurl.a&#40;content_encodi
ng.o&#41;&#58; In function `inflate_stream'&#58;
content_encoding.c&#58;&#40;.text+0x164&#41;&#58; undefined reference to `inflate'
content_encoding.c&#58;&#40;.text+0x23c&#41;&#58; undefined reference to `inflateEnd'
/usr/local/pspdev/lib/gcc/psp/4.3.2/../../../../psp/lib/libcurl.a&#40;content_encodi
ng.o&#41;&#58; In function `Curl_unencode_deflate_write'&#58;
content_encoding.c&#58;&#40;.text+0x3c0&#41;&#58; undefined reference to `inflateInit_'
/usr/local/pspdev/lib/gcc/psp/4.3.2/../../../../psp/lib/libcurl.a&#40;content_encodi
ng.o&#41;&#58; In function `Curl_unencode_gzip_write'&#58;
content_encoding.c&#58;&#40;.text+0x7cc&#41;&#58; undefined reference to `zlibVersion'
content_encoding.c&#58;&#40;.text+0x804&#41;&#58; undefined reference to `inflateInit2_'
content_encoding.c&#58;&#40;.text+0x858&#41;&#58; undefined reference to `inflateInit2_'
collect2&#58; ld returned 1 exit status
make&#58; *** &#91;myproject.elf&#93; Error 1
And Makefile

Code: Select all

TARGET = myproject
OBJS = main.o easyconnect.o

INCDIR = 
CFLAGS = -G0 -Wall -O2
CXXFLAGS = $&#40;CFLAGS&#41; -fno-exceptions -fno-rtti
ASFLAGS = $&#40;CFLAGS&#41;

LIBDIR =
LIBS= -lstdc++ -lpspgu -lz -lcurl
BUILD_PRX=1

EXTRA_TARGETS = EBOOT.PBP
PSP_EBOOT_TITLE = My PSP Project
BUILD_PRX = 1
PSP_FW_VERSION = 371
PSP_LARGE_MEMORY = 1


PSPSDK=$&#40;shell psp-config --pspsdk-path&#41;
include $&#40;PSPSDK&#41;/lib/build.mak
Thanks!
m0skit0
Posts: 191
Joined: Tue Jun 02, 2009 8:58 pm

Post by m0skit0 »

Seems you dont have zlib installed or you're not linking it correctly.
The Incredible Bill Gates wrote:The obvious mathematical breakthrough would be development of an easy way to factor large prime numbers.
foxss
Posts: 6
Joined: Sun Dec 07, 2008 2:00 pm

Post by foxss »

Thanks m0skit0 for replying.
My problem still there after I download make and install latest zlib from psp2dev trunk. It give me same error when do make.
m0skit0 wrote:Seems you dont have zlib installed or you're not linking it correctly.
m0skit0
Posts: 191
Joined: Tue Jun 02, 2009 8:58 pm

Post by m0skit0 »

Have you linked it correctly? I see -lz, r u sure this is correct?
The Incredible Bill Gates wrote:The obvious mathematical breakthrough would be development of an easy way to factor large prime numbers.
Mon Ouïe
Posts: 36
Joined: Sun Jul 05, 2009 10:22 pm

Post by Mon Ouïe »

I guess the order is wrong (but -lz is correct): A library which depends on other has to be linked before the libraries it depends on. So, it should be :

Code: Select all

-lcurl -lz
foxss
Posts: 6
Joined: Sun Dec 07, 2008 2:00 pm

Post by foxss »

YES, IT WORKS!
Thanks Mon Ouïe!
Mon Ouïe wrote:I guess the order is wrong (but -lz is correct): A library which depends on other has to be linked before the libraries it depends on. So, it should be :

Code: Select all

-lcurl -lz
Post Reply