# Multiple PING failed on tester 2

**URL:** <https://forum.codecrafters.io/t/multiple-ping-failed-on-tester-2/3097>\
**Category:** Challenges\
**Tags:** challenge:redis\
**Created:** [November 24, 2024, 8:53am UTC](https://forum.codecrafters.io/t/multiple-ping-failed-on-tester-2/3097 "2024-11-24T08:53:41Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![wangshu24](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/wangshu24/32/3043_2.png) [@wangshu24](https://forum.codecrafters.io/u/wangshu24)\
**Post date:** [November 24, 2024, 8:53am UTC](https://forum.codecrafters.io/t/multiple-ping-failed-on-tester-2/3097/1 "2024-11-24T08:53:41Z")

</div>

I’m stuck on Stage #wy1 - responding to multiple ping

This is my solution:

```auto
func main() {
	// You can use print statements as follows for debugging, they'll be visible when running tests.
	fmt.Println("Logs from your program will appear here!")
	
	l, lisErr := net.Listen("tcp", "0.0.0.0:6379")
	if lisErr != nil {
		fmt.Println("Failed to bind to port 6379")
		os.Exit(1)
	}

	//Close listener on exit
	// defer l.Close()
	fmt.Println("Server is listening on port 6379")

	
	conn, acceptedErr := l.Accept()
	

	if acceptedErr != nil {
		fmt.Println("Error accepting connection: ", acceptedErr.Error())
		os.Exit(1)
	}
		
	buf := make([]byte,1024)

	for {
		dataLength, err := conn.Read(buf)
		if err != nil {
			if err.Error() == "EOF" {
				fmt.Println("Connection closed")
				break
			}
			fmt.Println("Error reading:", err.Error())
			break
		}
		if dataLength == 0 {
			fmt.Println("No data read")
			break
		}
		messages := strings.Split(string(buf), "\r\n")
		for _, message := range messages {
			switch message {
			case "PING":
				conn.Write([]byte("+PONG\r\n"))
			default:
			}
		}
	}
}

```

And here’s a solution I analysed and compared to:

```auto
func main() {
	// You can use print statements as follows for debugging, they'll be visible when running tests.
	fmt.Println("Logs from your program will appear here!")
	l, err := net.Listen("tcp", "0.0.0.0:6379")
	if err != nil {
		fmt.Println("Failed to bind to port 6379")
		os.Exit(1)
	}
	connection, err := l.Accept()
	if err != nil {
		fmt.Println("Error accepting connection: ", err.Error())
		os.Exit(1)
	}
	buf := make([]byte, 1024)
	for {
		dataLength, err := connection.Read(buf)
		if err != nil {
			if err.Error() == "EOF" {
				fmt.Println("Connection closed")
				break
			}
			fmt.Println("Error reading:", err.Error())
			break
		}
		if dataLength == 0 {
			fmt.Println("No data read")
			break
		}
		messages := strings.Split(string(buf), "\r\n")
		for _, message := range messages {
			switch message {
			case "PING":
				connection.Write([]byte("+PONG\r\n"))
			default:
			}
		}
	}
}

```

Both looks identical to me, yet my code doesn’t work on the second tester, with it receiving empty string. Am I just utterly crazy and blind or is there some hidden flaw to my logic that I don’t fully understand?

Here are my logs:  
[compile] Moved ./.codecrafters/run.sh → ./your\_program.sh

2

[compile] Compilation successful.

3

4

Debug = true

5

6

[tester::#ZU2] Running tests for Stage #ZU2 (Handle concurrent clients)

7

[tester::#ZU2] $ ./your\_program.sh

8

[your\_program] Logs from your program will appear here!

9

[your\_program] Server is listening on port 6379

10

[tester::#ZU2] client-1: $ redis-cli PING

11

[tester::#ZU2] client-1: Sent bytes: “\*1\r\n$4\r\nPING\r\n”

12

[tester::#ZU2] client-1: Received bytes: “+PONG\r\n”

13

[tester::#ZU2] client-1: Received RESP simple string: “PONG”

14

[tester::#ZU2] Received “PONG”

15

[tester::#ZU2] client-2: $ redis-cli PING

16

[tester::#ZU2] client-2: Sent bytes: “\*1\r\n$4\r\nPING\r\n”

17

[tester::#ZU2] Received: “” (no content received)

18

[tester::#ZU2] ^ error

19

[tester::#ZU2] Error: Expected start of a new RESP2 value (either +, -, :, $ or \*)

20

[tester::#ZU2] Test failed

21

[tester::#ZU2] Terminating program

22

[tester::#ZU2] Program terminated successfully

```auto
include relevant logs here (please make sure to keep the backticks around this!)

```

And here’s a snippet of my code:

```python
include relevant code here (please make sure to keep the backticks around this!)

```

---

<div class="post-metadata">

**Author:** ![mrmagicpotato007](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/mrmagicpotato007/32/3096_2.png) [@mrmagicpotato007](https://forum.codecrafters.io/u/mrmagicpotato007)\
**Post date:** [November 24, 2024, 9:55am UTC](https://forum.codecrafters.io/t/multiple-ping-failed-on-tester-2/3097/2 "2024-11-24T09:55:25Z")

</div>

+1 facing the same issue

update: got fixed in the test case we are creating another redis client at 16 to test another PING so you need to accept multiple connections using go routines.

---

<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:** [November 24, 2024, 11:56am UTC](https://forum.codecrafters.io/t/multiple-ping-failed-on-tester-2/3097/3 "2024-11-24T11:56:12Z")

</div>

Hi @wangshu24, looks like you’ve got past this stage — do you happen to remember what was wrong? Would love to see if we can improve the tester / instructions.

---

<div class="post-metadata">

**Author:** ![wangshu24](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/wangshu24/32/3043_2.png) [@wangshu24](https://forum.codecrafters.io/u/wangshu24)\
**Post date:** [November 24, 2024, 1:08pm UTC](https://forum.codecrafters.io/t/multiple-ping-failed-on-tester-2/3097/4 "2024-11-24T13:08:35Z")

</div>

Yes, the code was as I presented above. And when I tried running the test, the first test manage to submit a PING and my server received the message and replied with PONG. The second PING ended in error and the tester panic with the message that said Receive message: “” . So just an empty message received. Not sure if this help but a common issue I ran into when testing locally is everytime I pinged my server with a message, it shutdown the server immediately after instead of kept it listening.

---

<div class="post-metadata">

**Author:** ![wangshu24](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/wangshu24/32/3043_2.png) [@wangshu24](https://forum.codecrafters.io/u/wangshu24)\
**Post date:** [November 24, 2024, 1:11pm UTC](https://forum.codecrafters.io/t/multiple-ping-failed-on-tester-2/3097/5 "2024-11-24T13:11:06Z")

</div>

Sorry if that wasn’t anymore helpful but I was just as lost when I ran into that issue.

---

<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:** [November 24, 2024, 1:31pm UTC](https://forum.codecrafters.io/t/multiple-ping-failed-on-tester-2/3097/6 "2024-11-24T13:31:50Z")

</div>

Thank you so much for sharing the details! This is incredibly helpful. 👍

---

<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:** [December 1, 2024, 4:25pm UTC](https://forum.codecrafters.io/t/multiple-ping-failed-on-tester-2/3097/7 "2024-12-01T16:25:47Z")

</div>

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