Failed on step "Respond to multiple PINGs #wy1"

I wrote my own c++ imp, but it doesn’t work properly.
Then i copied one of the solutions from “Code examples”, but is doesn’t work too.
Can anyone help me? I really stuck on it (

Here is my code impl:

#include <arpa/inet.h>   //function for translating IP address from string to binary format
#include <netdb.h>       //to get information about host (gethostbyname)
#include <sys/socket.h>  //declare  data for socket
#include <sys/types.h>   //declare types of system data calls
#include <unistd.h>      //to system functions, close file descript (handle)

#include <cstdlib>   //lib for common functions, e.g EXIT
#include <cstring>   //to work with string in C-style, strcpy
#include <iostream>  //lib for input streams output streams
#include <string>

int main(int argc, char **argv) {
  // Flush after every std::cout / std::cerr
  std::cout << std::unitbuf;
  std::cerr << std::unitbuf;

  // Create new socket, AF_INET - use IPV4 address, SOCK_STREAM - use TCP
  // socket, 0 - default val for TCP socket
  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";

  std::cout << "Logs from your program will appear here!\n";
  int client_fd;

  client_fd = accept(server_fd, (struct sockaddr *)&client_addr, (socklen_t *)&client_addr_len);

  if (client_fd < 0) {
    std::cerr << "Failed to accept connection\n";
    return 1;
  }

  std::cout << "Client connected\n";

  // Simulate handling client requests and responding to commands
  char buff[128] = "";
  bzero(&buff, sizeof(buff));
  while (1) {
    memset(&buff, '\0', sizeof(buff));
    recv(client_fd, buff, 15, 0);
    if (strcasecmp(buff, "*1\r\n$4\r\nping\r\n") != 0) {
      bzero(&buff, sizeof(buff));
      continue;
    }
    send(client_fd, "+PONG\r\n", 7, 0);
  }

  close(server_fd);

  return 0;
}

Here is the git push remote logs:

artm1904@DESKTOP-CIVJLLL:~/c++_projects/codecrafters-redis-cpp$ git push origin master
Enumerating objects: 1, done.
Counting objects: 100% (1/1), done.
Writing objects: 100% (1/1), 185 bytes | 185.00 KiB/s, done.
Total 1 (delta 0), reused 0 (delta 0), pack-reused 0
remote: ------------------------------------------------------------------------
remote: 
remote: 
remote:      ___            _          ___              __  _                   
remote:     / __\ ___    __| |  ___   / __\_ __  __ _  / _|| |_  ___  _ __  ___ 
remote:    / /   / _ \  / _` | / _ \ / /  | '__|/ _` || |_ | __|/ _ \| '__|/ __|
remote:   / /___| (_) || (_| ||  __// /___| |  | (_| ||  _|| |_|  __/| |   \__ 
remote:   \____/ \___/  \__,_| \___|\____/|_|   \__,_||_|   \__|\___||_|   |___/
remote: 
remote: 
remote:    Welcome to CodeCrafters! Your commit was received successfully.
remote: 
remote: ------------------------------------------------------------------------
remote: 
remote: ⏳ Turbo test runners busy. You are in queue.
remote: 

remote: 
remote: Running tests on your code. Logs should appear shortly...
remote: 
remote: [compile] -- Running vcpkg install
remote: [compile] Detecting compiler hash for triplet x64-linux...
remote: [compile] Compiler found: /usr/local/bin/c++
remote: [compile] All requested packages are currently installed.
remote: [compile] Total install time: 89.2 us
remote: [compile] -- Running vcpkg install - done
remote: [compile] -- The C compiler identification is GNU 13.2.0
remote: [compile] -- The CXX compiler identification is GNU 13.2.0
remote: [compile] -- Detecting C compiler ABI info
remote: [compile] -- Detecting C compiler ABI info - done
remote: [compile] -- Check for working C compiler: /usr/bin/cc - skipped
remote: [compile] -- Detecting C compile features
remote: [compile] -- Detecting C compile features - done
remote: [compile] -- Detecting CXX compiler ABI info
remote: [compile] -- Detecting CXX compiler ABI info - done
remote: [compile] -- Check for working CXX compiler: /usr/local/bin/c++ - skipped
remote: [compile] -- Detecting CXX compile features
remote: [compile] -- Detecting CXX compile features - done
remote: [compile] -- Performing Test CMAKE_HAVE_LIBC_PTHREAD
remote: [compile] -- Performing Test CMAKE_HAVE_LIBC_PTHREAD - Success
remote: [compile] -- Found Threads: TRUE
remote: [compile] -- Configuring done (1.7s)
remote: [compile] -- Generating done (0.0s)
remote: [compile] -- Build files have been written to: /app/build
remote: [compile] [ 50%] Building CXX object CMakeFiles/server.dir/src/Server.cpp.o
remote: [compile] [100%] Linking CXX executable server
remote: [compile] [100%] Built target server
remote: [compile] Moved ./.codecrafters/run.sh → ./your_program.sh
remote: [compile] Compilation successful.
remote: 
remote: [tester::#WY1] Running tests for Stage #WY1 (Respond to multiple PINGs)
remote: [tester::#WY1] $ ./your_program.sh
remote: [your_program] Waiting for a client to connect...
remote: [your_program] Logs from your program will appear here!
remote: [your_program] Client connected
remote: [tester::#WY1] client-1: $ redis-cli PING
remote: [tester::#WY1] Received "PONG"
remote: [tester::#WY1] client-1: > PING
remote: [tester::#WY1] write tcp 127.0.0.1:50332->127.0.0.1:6379: write: broken pipe
remote: [tester::#WY1] Test failed (try setting 'debug: true' in your codecrafters.yml to see more details)
remote: 

Hey @artm1904, could you upload your code to GitHub and share the link? It will be much easier to debug if I can run it directly.

Hello, this git-repo contains solutions. I tried to pass the test with this solution and it failed

Looks like you’ve made it past this stage. Great job!

Let me know if you still need any help with it.

Sorry… i found my fool mistake
I do changes in my code and the always use below commands:

  1. git commit --allow-empty -m “[any message]”
  2. git push origin master

I do not really push my changes to remote server)

1 Like

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