Failed to create connection: dial tcp 127.0.0.1:4221: connect: connection refused

I’m stuck on Stage #cn2.

What is weird is that when running

codecrafters test --previous

it also runs stage CN2 successfully. But when running

codecrafters test

I get this error:

[tester::#CN2] Failed to create connection: dial tcp 127.0.0.1:4221: connect: connection refused
[tester::#CN2] Test failed

Output:

bubuntu@W11CKLPSV3:~/codecrafters-http-server-c$ codecrafters test --previous
Initiating test run…

:high_voltage: This is a turbo test run.

Running tests. Logs should appear shortly…

[compile] Compilation successful.

Debug = true

[tester::#AT4] Running tests for Stage #AT4 (Bind to a port)
[tester::#AT4] Running program
[tester::#AT4] $ ./your_program.sh
[tester::#AT4] Connecting to localhost:4221 using TCP
[your_program] – Running vcpkg install
[your_program] All requested packages are currently installed.
[your_program] All requested installations completed successfully in: 41 us
[your_program] – Running vcpkg install - done
[your_program] – The C compiler identification is GNU 14.2.0
[tester::#AT4] Failed to connect to port 4221, retrying in 1s
[tester::#AT4] Failed to connect to port 4221, retrying in 1s
[tester::#AT4] Failed to connect to port 4221, retrying in 1s
[your_program] – The CXX compiler identification is GNU 14.2.0
[your_program] – Detecting C compiler ABI info
[your_program] – Detecting C compiler ABI info - done
[your_program] – Check for working C compiler: /usr/bin/cc - skipped
[your_program] – Detecting C compile features
[your_program] – Detecting C compile features - done
[your_program] – Detecting CXX compiler ABI info
[your_program] – Detecting CXX compiler ABI info - done
[your_program] – Check for working CXX compiler: /usr/local/bin/c++ - skipped
[your_program] – Detecting CXX compile features
[your_program] – Detecting CXX compile features - done
[your_program] – Configuring done (5.6s)
[your_program] – Generating done (0.0s)
[your_program] – Build files have been written to: /app/build
[your_program] [ 16%] Building C object CMakeFiles/http-server.dir/src/http_response.c.o
[your_program] [ 33%] Building C object CMakeFiles/http-server.dir/src/http_status.c.o
[your_program] [ 50%] Building C object CMakeFiles/http-server.dir/src/log.c.o
[your_program] [ 66%] Building C object CMakeFiles/http-server.dir/src/main.c.o
[your_program] [ 83%] Building C object CMakeFiles/http-server.dir/src/utils.c.o
[your_program] [100%] Linking C executable http-server
[your_program] [100%] Built target http-server
[your_program] Logs from your program will appear here!
[your_program] Waiting for a client to connect…
[tester::#AT4] Success! Closing connection
[tester::#AT4] Test passed.
[tester::#AT4] Terminating program
[your_program] Client connected
[tester::#AT4] Program terminated successfully

[tester::#IA4] Running tests for Stage #IA4 (Respond with 200)
[tester::#IA4] Running program
[tester::#IA4] $ ./your_program.sh
[your_program] – Running vcpkg install
[your_program] All requested packages are currently installed.
[your_program] All requested installations completed successfully in: 39.5 us
[your_program] – Running vcpkg install - done
[your_program] – Configuring done (0.0s)
[your_program] – Generating done (0.0s)
[your_program] – Build files have been written to: /app/build
[your_program] [100%] Built target http-server
[your_program] Logs from your program will appear here!
[your_program] Waiting for a client to connect…
[tester::#IA4] Connected to localhost port 4221
[tester::#IA4] $ curl -v http:/ /localhost:4221/
[tester::#IA4] > GET / HTTP/1.1
[tester::#IA4] > Host: localhost:4221
[tester::#IA4] >
[tester::#IA4] Sent bytes: “GET / HTTP/1.1\r\nHost: localhost:4221\r\n\r\n”
[tester::#IA4] Received bytes: “HTTP/1.1 200 OK\r\n\r\n”
[tester::#IA4] < HTTP/1.1 200 OK
[tester::#IA4] <
[tester::#IA4] Received response with 200 status code
[tester::#IA4] Test passed.
[tester::#IA4] Terminating program
[your_program] Client connected
[tester::#IA4] Program terminated successfully

[tester::#IH0] Running tests for Stage #IH0 (Extract URL path)
[tester::#IH0] Running program
[tester::#IH0] $ ./your_program.sh
[your_program] – Running vcpkg install
[your_program] All requested packages are currently installed.
[your_program] All requested installations completed successfully in: 47.4 us
[your_program] – Running vcpkg install - done
[your_program] – Configuring done (0.0s)
[your_program] – Generating done (0.0s)
[your_program] – Build files have been written to: /app/build
[your_program] [100%] Built target http-server
[your_program] Logs from your program will appear here!
[your_program] Waiting for a client to connect…
[tester::#IH0] Connected to localhost port 4221
[tester::#IH0] $ curl -v http:/ /localhost:4221/grape
[tester::#IH0] > GET /grape HTTP/1.1
[tester::#IH0] > Host: localhost:4221
[tester::#IH0] >
[tester::#IH0] Sent bytes: “GET /grape HTTP/1.1\r\nHost: localhost:4221\r\n\r\n”
[tester::#IH0] Received bytes: “HTTP/1.1 404 Not Found\r\n\r\n”
[tester::#IH0] < HTTP/1.1 404 Not Found
[tester::#IH0] <
[tester::#IH0] Received response with 404 status code
[your_program] Client connected
[tester::#IH0] Test passed.
[tester::#IH0] Terminating program
[tester::#IH0] Program terminated successfully

[tester::#CN2] Running tests for Stage #CN2 (Respond with body)
[tester::#CN2] Running program
[tester::#CN2] $ ./your_program.sh
[your_program] – Running vcpkg install
[your_program] All requested packages are currently installed.
[your_program] All requested installations completed successfully in: 38.6 us
[your_program] – Running vcpkg install - done
[your_program] – Configuring done (0.0s)
[your_program] – Generating done (0.0s)
[your_program] – Build files have been written to: /app/build
[your_program] [100%] Built target http-server
[your_program] Logs from your program will appear here!
[your_program] Waiting for a client to connect…
[your_program] Client connected
[tester::#CN2] Connected to localhost port 4221
[tester::#CN2] $ curl -v http:/ /localhost:4221/echo/orange
[tester::#CN2] > GET /echo/orange HTTP/1.1
[tester::#CN2] > Host: localhost:4221
[tester::#CN2] >
[tester::#CN2] Sent bytes: “GET /echo/orange HTTP/1.1\r\nHost: localhost:4221\r\n\r\n”
[tester::#CN2] Received bytes: “HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\nContent-Length: 6\r\n\r\norange”
[tester::#CN2] < HTTP/1.1 200 OK
[tester::#CN2] < Content-Type: text/plain
[tester::#CN2] < Content-Length: 6
[tester::#CN2] <
[tester::#CN2] < orange
[tester::#CN2] <
[tester::#CN2] Received response with 200 status code
[tester::#CN2] ✓ Content-Type header is present
[tester::#CN2] ✓ Content-Length header is present
[tester::#CN2] ✓ Body is correct
[tester::#CN2] Test passed.
[tester::#CN2] Terminating program
[your_program] buf: 6
[tester::#CN2] Program terminated successfully

Test passed. Congrats!

bubuntu@W11CKLPSV3:~/codecrafters-http-server-c$ codecrafters test
Initiating test run…

:high_voltage: This is a turbo test run.

Running tests. Logs should appear shortly…

[compile] Compilation successful.

Debug = true

[tester::#CN2] Running tests for Stage #CN2 (Respond with body)
[tester::#CN2] Running program
[tester::#CN2] $ ./your_program.sh
[your_program] – Running vcpkg install
[your_program] All requested packages are currently installed.
[your_program] All requested installations completed successfully in: 46.1 us
[your_program] – Running vcpkg install - done
[your_program] – The C compiler identification is GNU 14.2.0
[tester::#CN2] Failed to create connection: dial tcp 127.0.0.1:4221: connect: connection refused
[tester::#CN2] Test failed
[tester::#CN2] Terminating program
[tester::#CN2] Program terminated successfully

View our article on debugging test failures:

Hey @mole-480, looks like your repo is missing the required .codecrafters folder.

I’ve pushed a fix to your repo. You can run git pull to sync the changes.

Let me know if you’re still running into issues!

1 Like

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