ROM Hack [Homebrew Help] Socket code always fails.

SirEnder125

Well-Known Member
Newcomer
Joined
Dec 8, 2020
Messages
86
Reaction score
12
Trophies
0
Age
18
XP
230
Country
United States
Hello. This is the code I am having trouble with:
Code:
#include <iostream>
#include <3ds.h>
#include <string>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/unistd.h>

#define PORT 6868

void Sleep(int milliseconds) {
    svcSleepThread(1000000 * milliseconds);
}

int main(int argc, char** argv) {
    // Initialize gfx and console.
    gfxInitDefault();
    consoleInit(GFX_TOP, NULL);

    int sock = 0;
    struct sockaddr_in server_addr;
    char buffer[1024] = { 0 };

    if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
        std::cout << CONSOLE_RED << "Error occurred on socket creation.\n" << CONSOLE_RESET;
        return -1;
    }

    // Main loop
    while (aptMainLoop()) {
        // Scan all inputs.
        hidScanInput();

        u32 kDown = hidKeysDown();

        if (kDown & KEY_START)
            break; // Exit the application.

        gfxFlushBuffers();
        gfxSwapBuffers();
        gspWaitForVBlank();
    }

    // Exit
    gfxExit();
    return 0;
}

When ran, this code fails to create a socket every time.
Can someone please help me?
 
Did you initialize socket service? This should be at the beginning of your main function:
Code:
// allocate buffer for SOC service
    SOC_buffer = (u32*)memalign(SOC_ALIGN, SOC_BUFFERSIZE);

    if(SOC_buffer == NULL) {
        failExit("memalign: failed to allocate\n");
    }

    // Now intialise soc:u service
    if ((ret = socInit(SOC_buffer, SOC_BUFFERSIZE)) != 0) {
        failExit("socInit: 0x%08X\n", (unsigned int)ret);
    }

and "socExit();" somewhere near the end where you deinitialize everything.
 
  • Like
Reactions: Julie_Pilgrim
Oh, I was unaware that was necessary. Thanks! :D

--------------------- MERGED ---------------------------

Actually, I get some error saying that function returning function is not allowed... (With extra code from 3ds examples (devkitPro))
 
Oh, I was unaware that was necessary. Thanks! :D

--------------------- MERGED ---------------------------

Actually, I get some error saying that function returning function is not allowed... (With extra code from 3ds examples (devkitPro))
Could you show the full error?
 
upload_2021-3-17_17-58-30.png

It says that when I hover over "format".

Also it told me to place braces.
 
Right, sorry, the error is probably not even caused by that, I just copied that chunk of code from the example and forgot to mention anything else. Try this chunk of code that actually declares the return value variable and the datatype for SOC_buffer, sorry...
Code:
// allocate buffer for SOC service
    int ret = 0;
    u32* SOC_buffer = (u32*)memalign(SOC_ALIGN, SOC_BUFFERSIZE);

    if(SOC_buffer == NULL) {
        failExit("memalign: failed to allocate\n");
    }

    // Now intialise soc:u service
    if ((ret = socInit(SOC_buffer, SOC_BUFFERSIZE)) != 0) {
        failExit("socInit: 0x%08X\n", (unsigned int)ret);
    }
If you still get the error about that function, you can just remove the failExit lines from the code and replace them with your own error messages or just with "exit(1);", that's what I did in one of my programs and it worked just fine.
 

Site & Scene News

Popular threads in this forum