I’ve finished the task #wy1 but unfortunately didn’t get what’s going on.
All the tests I do locally return me only a single PONG response regardless of how many times I put PING in the message.
However, I passed the test and completed the stage since I have to reuse the provided code example.
What do I miss?
Here are my logs:
user@user codecrafters-redis-rust % echo -ne '*1\r\n$4\r\nping\r\n' | nc localhost 6379
+PONG
Logs from your program will appear here!
accepted new connection
received 14 bytes
data: [42, 49, 13, 10, 36, 52, 13, 10, 112, 105, 110, 103, 13, 10]
received 0 bytes
data: []
-----------------------------------------------------------------------------------------
user@user codecrafters-redis-rust % echo -ne '*1\r\n$4\r\nping\nping\r\n' | nc localhost 6379
+PONG
accepted new connection
received 19 bytes
data: [42, 49, 13, 10, 36, 52, 13, 10, 112, 105, 110, 103, 10, 112, 105, 110, 103, 13, 10]
received 0 bytes
data: []
And here’s a snippet of my code:
fn handle_client(mut stream: TcpStream) {
let mut buf = [0; 512];
loop {
let bytes_read = stream.read(&mut buf).expect("Failed to read from client");
println!("received {} bytes", bytes_read);
println!("data: {:?}", &buf[..bytes_read]);
if bytes_read == 0 {
return;
}
let response = "+PONG\r\n";
stream.write_all(response.as_bytes()).expect("Failed to write to client");
}
}