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

**URL:** <https://forum.codecrafters.io/t/failed-to-create-connection-dial-tcp-127-0-0-1-connect-connection-refused/14232>\
**Category:** Challenges\
**Tags:** challenge:http-server\
**Created:** [August 1, 2025, 5:09pm UTC](https://forum.codecrafters.io/t/failed-to-create-connection-dial-tcp-127-0-0-1-connect-connection-refused/14232 "2025-08-01T17:09:16Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mole-480](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/mole-480/32/32_2.png) [@mole-480](https://forum.codecrafters.io/u/mole-480)\
**Post date:** [August 1, 2025, 5:09pm UTC](https://forum.codecrafters.io/t/failed-to-create-connection-dial-tcp-127-0-0-1-connect-connection-refused/14232/1 "2025-08-01T17:09:17Z")

</div>

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…
> 
> ⚡ 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…
> 
> ⚡ 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:

---

<div class="post-metadata">

**Author:** ![andy1li](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/andy1li/32/10_2.png) [@andy1li](https://forum.codecrafters.io/u/andy1li)\
**Post date:** [August 1, 2025, 5:58pm UTC](https://forum.codecrafters.io/t/failed-to-create-connection-dial-tcp-127-0-0-1-connect-connection-refused/14232/4 "2025-08-01T17:58:11Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex003/uploads/codecrafters/original/3X/7/0/700657133935c15703e22c7c8870394f6e6dc27d.svg) [@system](https://forum.codecrafters.io/u/system)\
**Post date:** [August 6, 2025, 5:59pm UTC](https://forum.codecrafters.io/t/failed-to-create-connection-dial-tcp-127-0-0-1-connect-connection-refused/14232/6 "2025-08-06T17:59:16Z")

</div>

Closed on August 6, 2025, 5:59pm UTC

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