Help me with the bind to port - Error before even starting

I’m stuck on Stage Bind to a port #jm1

I’ve tried to do everything that the tutorial said but it still doesnt work idk why i am using manjaro maybe because of that or some firewall? but i turned it off tho

Here are my logs:

[compile] -- Running vcpkg install
[compile] Detecting compiler hash for triplet x64-linux...
[compile] Compiler found: /usr/local/bin/c++
[compile] All requested packages are currently installed.
[compile] Total install time: 520 ns
[compile] -- Running vcpkg install - done
[compile] -- The C compiler identification is GNU 13.2.0
[compile] -- The CXX compiler identification is GNU 13.2.0
[compile] -- Detecting C compiler ABI info
[compile] -- Detecting C compiler ABI info - done
[compile] -- Check for working C compiler: /usr/bin/cc - skipped
[compile] -- Detecting C compile features
[compile] -- Detecting C compile features - done
[compile] -- Detecting CXX compiler ABI info
[compile] -- Detecting CXX compiler ABI info - done
[compile] -- Check for working CXX compiler: /usr/local/bin/c++ - skipped
[compile] -- Detecting CXX compile features
[compile] -- Detecting CXX compile features - done
[compile] -- Performing Test CMAKE_HAVE_LIBC_PTHREAD
[compile] -- Performing Test CMAKE_HAVE_LIBC_PTHREAD - Success
[compile] -- Found Threads: TRUE
[compile] -- Configuring done (4.8s)
[compile] -- Generating done (0.0s)
[compile] -- Build files have been written to: /app/build
[compile] [ 50%] Building CXX object CMakeFiles/server.dir/src/Server.cpp.o
[compile] [100%] Linking CXX executable server
[compile] [100%] Built target server
[compile] Moved ./.codecrafters/run.sh → ./your_program.sh
[compile] Compilation successful.
[tester::#JM1] Running tests for Stage #JM1 (Bind to a port)
[tester::#JM1] $ ./your_program.sh
[tester::#JM1] Connecting to port 6379...
[your_program] Waiting for a client to connect...
[your_program] Logs from your program will appear here!
[tester::#JM1] Failed to connect to port 6379.
[tester::#JM1] Test failed (try setting 'debug: true' in your codecrafters.yml to see more details)

And here’s a snippet of my code:

#include <iostream>
#include <cstdlib>
#include <string>
#include <cstring>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netdb.h>

int main(int argc, char **argv) {
  // Flush after every std::cout / std::cerr
  std::cout << std::unitbuf;
  std::cerr << std::unitbuf;
  
  int server_fd = socket(AF_INET, SOCK_STREAM, 0);
  if (server_fd < 0) {
   std::cerr << "Failed to create server socket\n";
   return 1;
  }
  
  // Since the tester restarts your program quite often, setting SO_REUSEADDR
  // ensures that we don't run into 'Address already in use' errors
  int reuse = 1;
  if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0) {
    std::cerr << "setsockopt failed\n";
    return 1;
  }
  
  struct sockaddr_in server_addr;
  server_addr.sin_family = AF_INET;
  server_addr.sin_addr.s_addr = INADDR_ANY;
  server_addr.sin_port = htons(6379);
  
  if (bind(server_fd, (struct sockaddr *) &server_addr, sizeof(server_addr)) != 0) {
    std::cerr << "Failed to bind to port 6379\n";
    return 1;
  }
  
  int connection_backlog = 5;
  if (listen(server_fd, connection_backlog) != 0) {
    std::cerr << "listen failed\n";
    return 1;
  }
  
  struct sockaddr_in client_addr;
  int client_addr_len = sizeof(client_addr);
  std::cout << "Waiting for a client to connect...\n";

  // You can use print statements as follows for debugging, they'll be visible when running tests.
  std::cout << "Logs from your program will appear here!\n";

  // Uncomment this block to pass the first stage
  
  accept(server_fd, (struct sockaddr *) &client_addr, (socklen_t *) &client_addr_len);
  std::cout << "Client connected\n";
  
  close(server_fd);

  return 0;
}

Hmm, it looks like your changes weren’t committed and pushed to our git server and GitHub.

Could you double-check that your changes have been saved, and then try running the following commands?

i did it though, maybe some commit problem?

[saikoukami@saikoukami-vmwarevirtualplatform codecrafters-redis-cpp]$ git remote -v
origin https://git.codecrafters.io/[censored] (fetch)
origin https://git.codecrafters.io/[censored] (push)
[saikoukami@saikoukami-vmwarevirtualplatform codecrafters-redis-cpp]$ git status
On branch master
Your branch is up to date with ‘origin/master’.

nothing to commit, working tree clean
[saikoukami@saikoukami-vmwarevirtualplatform codecrafters-redis-cpp]$ git add .
[saikoukami@saikoukami-vmwarevirtualplatform codecrafters-redis-cpp]$ git commit -am “[any message]”
git push origin master
On branch master
Your branch is up to date with ‘origin/master’.

nothing to commit, working tree clean
Everything up-to-date
[saikoukami@saikoukami-vmwarevirtualplatform codecrafters-redis-cpp]$


some github problem? im sorry im new to this

This might be a silly question, but could you triple-check that you’ve saved the file before committing and pushing?

Another thing you can try is editing the comment in any way, saving it, and then creating a new commit.

OMFG i really cantr believe i’m so dumb, really sorry to waste your time… i am so used to vs code autosave it didnt even cross my mind and this was my first time switching to a linux system, i forgot i’m sorry and thank you <3 really

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.