# Connection Reset Error

**URL:** <https://forum.codecrafters.io/t/connection-reset-error/1946>\
**Category:** Challenges\
**Tags:** challenge:kafka\
**Created:** [September 23, 2024, 10:56am UTC](https://forum.codecrafters.io/t/connection-reset-error/1946 "2024-09-23T10:56:22Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Vedant-Gandhi](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/vedant-gandhi/32/1882_2.png) [@Vedant-Gandhi](https://forum.codecrafters.io/u/Vedant-Gandhi)\
**Post date:** [September 23, 2024, 10:56am UTC](https://forum.codecrafters.io/t/connection-reset-error/1946/1 "2024-09-23T10:56:22Z")

</div>

Here is the function that handles the connection -

```go
func handleConnection(conn net.Conn) {
	defer conn.Close()

	var messageLen, correlationID uint32

	if err := binary.Read(conn, binary.BigEndian, &messageLen); err != nil {
		fmt.Println("Error reading message length:", err)
		return
	}

	// Parse fields
	var apiKey, apiVersion int16
	if err := binary.Read(conn, binary.BigEndian, &apiKey); err != nil {
		fmt.Println("Error reading API key:", err)
		return
	}
	if err := binary.Read(conn, binary.BigEndian, &apiVersion); err != nil {
		fmt.Println("Error reading API version:", err)
		return
	}
	if err := binary.Read(conn, binary.BigEndian, &correlationID); err != nil {
		fmt.Println("Error reading correlation ID:", err)
		return
	}

	fmt.Printf("API Key: %d\n", apiKey)
	fmt.Printf("API Version: %d\n", apiVersion)
	fmt.Printf("Correlation ID: %d\n", correlationID)

	// Check API version
	if apiVersion < 0 || apiVersion > 4 {
		fmt.Println("In err response")

		sendErrorResponse(conn, correlationID, 35) // UNSUPPORTED_VERSION
		os.Exit(1)
	}

	sendSuccessResponse(conn, correlationID)
}

```

I’m stuck on Stage #NC5(change to your stage, ex. #JM3),  
I am getting the error when the API Version is outside the whitelisted range i.e. 0 - 4

Here are my logs:

```auto
remote: [tester::#NC5] error reading from connection: read tcp [::1]:51552->[::1]:9092: read: connection reset by peer
remote: [tester::#NC5] Test failed

```

---

<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:** [October 4, 2024, 1:21am UTC](https://forum.codecrafters.io/t/connection-reset-error/1946/3 "2024-10-04T01:21:18Z")

</div>

Closing this thread due to inactivity. If you still need assistance, feel free to reopen or start a new discussion!

---

<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:** [October 4, 2024, 1:21am UTC](https://forum.codecrafters.io/t/connection-reset-error/1946/4 "2024-10-04T01:21:22Z")

</div>


