[SDL2_net]Error when trying to bind to 127.0.0.1

I’m on Manjaro using SDL2_net

When I run the server, it exits immediately and I get the output:

bind_address: 127.0.0.1
SDLNet_TCP_Open: Couldn’t connect to remote host

  IPaddress ip;
  fprintf(stderr, "bind_address: %s\n", config.bind_address);
  if (SDLNet_ResolveHost(&ip, config.bind_address, default_port) == -1) {
    fprintf(stderr, "SDLNet_ResolveHost: %s\n", SDLNet_GetError());
    SDLNet_Quit();
    SDL_Quit();
    return 1;
  }

  TCPsocket server = SDLNet_TCP_Open(&ip);
  if (!server) {
    fprintf(stderr, "SDLNet_TCP_Open: %s\n", SDLNet_GetError());
    SDLNet_Quit();
    SDL_Quit();
    return 1;
  }

It’s been working fine when using “NULL” or “0.0.0.0” as the second argument of SDLNet_ResolveHost

bind_address: 0.0.0.0
Server listening on 0.0.0.0:22777
Client 0 connected from 127.0.0.1:51488
Client 1 connected from 127.0.0.1:51490

I wanted to try binding to localhost host because my network tests are passing in Linux and MacOS GitHub runners, but failing in the Windows runner. I thought there’s a possibility that Windows was more restrictive about what addresses could be used in the runner.

I opened a ticket for this on my project repo.

What specifically is SDLNet_ResolveHost() returning as the IP? What happens if you pass “localhost” as the second argument instead of “127.0.0.1”?

And if you already have the IP address you could skip the call to SDLNet_ResolveHost(), since its job is to turn a host/domain name like “libsdl.org” to an IP address.

Thank you for your suggestions @sjr

The string that’s printed is 127.0.0.1:22777. I get the same result whether I use that IP or localhost.

  TCPsocket server = SDLNet_TCP_Open(&ip);
  if (!server) {
    print_ipaddress(&ip);
    fprintf(stderr, "SDLNet_TCP_Open: %s\n", SDLNet_GetError());
    SDLNet_Quit();
    SDL_Quit();
    return 1;
  }
static void print_ipaddress(const IPaddress *ip) {
  char ipaddr[INET6_ADDRSTRLEN];
  Uint32 host = SDL_SwapBE32(ip->host);

  if (host == 0) {
    snprintf(ipaddr, sizeof(ipaddr), "0.0.0.0");
  } else {
    snprintf(ipaddr, sizeof(ipaddr), "%u.%u.%u.%u", (host >> 24) & 0xFF, (host >> 16) & 0xFF,
             (host >> 8) & 0xFF, host & 0xFF);
  }

  printf("%s:%u\n", ipaddr, SDL_SwapBE16(ip->port));
}

More of my code for this is at PR #49