[Switch] libcurl can't connect to server

Post Reply
_POG_
Posts: 1
Joined: Sat Feb 11, 2023 5:02 pm

[Switch] libcurl can't connect to server

Post by _POG_ » Tue Feb 14, 2023 8:47 am

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!

WinterMute
Site Admin
Posts: 1859
Joined: Tue Aug 09, 2005 3:21 am
Location: UK
Contact:

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

Post by WinterMute » Mon May 01, 2023 6:13 pm

You need to initialise socket support before you can use sockets. See https://github.com/switchbrew/switch-ex ... main.c#L58
Help keep devkitPro toolchains free, Donate today

Personal Blog

Post Reply

Who is online

Users browsing this forum: No registered users and 27 guests