0

我在网上找到了这段代码,它应该将文件下载到插入我的 wii 的 USB 中,但是,我发现它只适用于 http 或 www 域,这意味着我无法从我的 github.io 网站下载文件。我真的找不到任何有用的示例或文档。有什么好的文档或示例可以让我受益吗?我的代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <gccore.h>
#include <wiiuse/wpad.h>

#include <fat.h>
#include <network.h>

#define textPos(x, y) printf("\x1b[%d;%dH", y, x)

static void *xfb = NULL;
static GXRModeObj *rmode = NULL;

void initText();

void loop();         
bool init_network(); 

int main(int argc, char **argv)
{
    struct sockaddr_in sin; 
    struct hostent *dns;    

    VIDEO_Init();
    WPAD_Init();

    initText();

    bool init = init_network(); 
    char host[64] = "jawa2401.github.io";
    textPos(1, 1);
    printf("Network: %d\n", init);
    s32 sock = net_socket(AF_INET, SOCK_STREAM, 0); 
    printf("Socket: %d\n", sock);
    dns = net_gethostbyname(host); 
    printf("Dedicated Nameservers: %d\n", sizeof(dns->h_addr_list));
    int f = fatInitDefault();
    memcpy(&sin.sin_addr.s_addr, dns->h_addr, dns->h_length); 
    sin.sin_family = AF_INET;
    sin.sin_port = htons(80); 
    printf("Setting up sockaddr_in...\n");
    s32 c = net_connect(sock, (struct sockaddr *)&sin, sizeof(sin)); 
    printf("Connecting: %d\n", c);
    int size = 0x800;

    char reponse[size];
    char requete[128];
    sprintf(requete, "GET /robots.txt HTTP/1.1\r\nHost: %s\r\nAccept: */*\r\n", host);
    printf("Sending GET Request: %s\n", requete);
    s32 r = net_send(sock, requete, sizeof(requete), 0); 
    printf("GET Request Returned: %d\n", r);
    printf("Preparing for FAT write: %d\n", f);
    printf("memset");
    memset(reponse, 0, size);
    s32 rec = net_recv(sock, reponse, size, 8); 
    printf("Receiving: %d\n", rec);
    FILE *za = fopen("/resp.dat", "wb");
    printf("Writing file.... (fat -> %d)\n", f);
    int fw = fwrite(reponse, sizeof(u8), sizeof(reponse), za);
    fclose(za);
    printf("File written: %d", fw);
    loop();

    return 0;
}

void initText()
{
    rmode = VIDEO_GetPreferredMode(NULL);
    xfb = MEM_K0_TO_K1(SYS_AllocateFramebuffer(rmode));
    console_init(xfb, 20, 20, rmode->fbWidth, rmode->xfbHeight, rmode->fbWidth * VI_DISPLAY_PIX_SZ);

    VIDEO_Configure(rmode);
    VIDEO_SetNextFramebuffer(xfb);
    VIDEO_SetBlack(FALSE);
    VIDEO_Flush();
    VIDEO_WaitVSync();
    if (rmode->viTVMode & VI_NON_INTERLACE)
        VIDEO_WaitVSync();
}

void loop()
{
    while (1)
    {
        WPAD_ScanPads();
        u32 pressed = WPAD_ButtonsDown(0);
        if (pressed & WPAD_BUTTON_HOME)
            break;
        VIDEO_WaitVSync();
    }
}

bool init_network()
{
    bool ok = false;
    for (int i = 0; i < 50 && !ok; i++) 
        if (net_init() >= 0)
            ok = true;
    return ok;
}
4

0 回答 0