Page 1 of 1

[Switch] libcurl can't connect to server

Posted: Tue Feb 14, 2023 8:47 am
by _POG_
Hi, lately I wanted to try to make a mod downloader. But every time I try to download files libcurl throws an error "Can't connect to server" (A friend tried my app on real hardware while being connected to the internet).
I tried to compile the downloader for both Windows and Linux and it worked every time. Has anyone an explanation for this? Here's my code (in C):

Code: Select all

#include <stdlib.h>
#include <errno.h>
#include <curl/curl.h>

#include <switch.h>

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

int downloadFile(char* url, char* output){
    CURL *curl;
    FILE *fp;
    CURLcode res;
    curl = curl_easy_init();
    if (curl) {
        fp = fopen(output,"wb");
        if (fp==NULL){
            printf("Failed opening file, abort...\n");
            return 1;
        }
        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);
        /* always cleanup */
        curl_easy_cleanup(curl);
        fclose(fp);
        if (res!= CURLE_OK){
            printf("Error: %s\n", curl_easy_strerror(res));
            return 1;
        }
    }
    return 0;
}
Thanks in advance!

Re: [Switch] libcurl can't connect to server

Posted: Mon May 01, 2023 6:13 pm
by WinterMute
You need to initialise socket support before you can use sockets. See https://github.com/switchbrew/switch-ex ... main.c#L58