ROM Hack [Homebrew Help] Socket code always fails.

SirEnder125

Well-Known Member
OP
Newcomer
Joined
Dec 8, 2020
Messages
86
Trophies
0
Age
16
XP
210
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?
 

IC_

GBAtemp's ???
Member
Joined
Aug 24, 2017
Messages
1,571
Trophies
1
Location
The Forest
XP
5,503
Country
Antarctica
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

SirEnder125

Well-Known Member
OP
Newcomer
Joined
Dec 8, 2020
Messages
86
Trophies
0
Age
16
XP
210
Country
United States
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))
 

IC_

GBAtemp's ???
Member
Joined
Aug 24, 2017
Messages
1,571
Trophies
1
Location
The Forest
XP
5,503
Country
Antarctica
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?
 

SirEnder125

Well-Known Member
OP
Newcomer
Joined
Dec 8, 2020
Messages
86
Trophies
0
Age
16
XP
210
Country
United States
upload_2021-3-17_17-58-30.png

It says that when I hover over "format".

Also it told me to place braces.
 

IC_

GBAtemp's ???
Member
Joined
Aug 24, 2017
Messages
1,571
Trophies
1
Location
The Forest
XP
5,503
Country
Antarctica
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

General chit-chat
Help Users
  • No one is chatting at the moment.
  • Sicklyboy @ Sicklyboy:
    @BigOnYa, I'll rip a bag off my Volcano to that, brother
    +2
  • a_username_that_is_cool @ a_username_that_is_cool:
    DDDS . Dual Developer Dedede System :)
    +1
  • BakerMan @ BakerMan:
    also happy birthday @Xdqwerty
    +1
  • BigOnYa @ BigOnYa:
    Is it me you're looking for?
  • MysticStarlight @ MysticStarlight:
    'Cause I wonder where you are
  • MysticStarlight @ MysticStarlight:
    wait I got the lyrics mixed up
    +1
  • BigOnYa @ BigOnYa:
    And I wonder what you do...Lol
  • BigOnYa @ BigOnYa:
    Its all good, I'm here to talk to you.
  • BigOnYa @ BigOnYa:
    Hello
  • BigOnYa @ BigOnYa:
    I've been playing fallout4 all day, I went into a elevator and it kept going, never stopped, waited 5 minutes, must of been a bug. How bout you, whatcha do today?
    +1
  • MysticStarlight @ MysticStarlight:
    I'm wondering why a stray tuxedo cat has a cloudy eye
  • MysticStarlight @ MysticStarlight:
    I've been playing TOTK all day also
    +1
  • MysticStarlight @ MysticStarlight:
    Oh you reminded me of those elevator nightmares
  • BigOnYa @ BigOnYa:
    Awesome game, fun how they force you to build, but you can build as you want
  • MysticStarlight @ MysticStarlight:
    It is super fun, I actually prefer it over BoTW
    +1
  • BigOnYa @ BigOnYa:
    The guy holding the sign always cracked me up, esp when you fail, and it falls
  • MysticStarlight @ MysticStarlight:
    Ah yes, that guy lol. I still need to find all his signs. The game has a lot to do though so I don't know what to finish first
  • MysticStarlight @ MysticStarlight:
    I'm 120 hours in and am still like 44.4% done
    +1
  • BigOnYa @ BigOnYa:
    Good times. Building is starting to get good around now for you. You making me want to make run thru again.
  • BigOnYa @ BigOnYa:
    Ok good chatting, enjoy your gaming, and avoid the lightning, or put your sword away. Good night.
  • K3Nv2 @ K3Nv2:
    All I gotta do is not spend $10 in the next 5 days and I'll break even this month lol
  • K3Nv2 @ K3Nv2:
    @BigOnYa, tell your wife not to forget her wallet
    +1
    K3Nv2 @ K3Nv2: @BigOnYa, tell your wife not to forget her wallet +1