I’m stuck on Stage #wy1 - responding to multiple ping
This is my solution:
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:
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
include relevant logs here (please make sure to keep the backticks around this!)
And here’s a snippet of my code:
include relevant code here (please make sure to keep the backticks around this!)