I have passed this stage earlier, but then after refactoring my code, I am getting this error
remote: [tester::#ZU2] client-2: $ redis-cli PING
remote: [tester::#ZU2] client-2: Sent bytes: “*1\r\n$4\r\nPING\r\n”
remote: [tester::#ZU2] Received: “” (no content received)
remote: [tester::#ZU2] ^ error
remote: [tester::#ZU2] Error: Expected start of a new RESP2 value (either +, -, :, $ or *)
My code is this:
public static void main(String args){
// You can use print statements as follows for debugging, they’ll be visible when running tests.
System.out.println(“Logs from your program will appear here!”);
// Uncomment this block to pass the first stage
ServerSocket serverSocket = null;
Socket clientSocket = null;
int port = 6379;
try {
serverSocket = new ServerSocket(port);
// Since the tester restarts your program quite often, setting SO_REUSEADDR
// ensures that we don't run into 'Address already in use' errors
serverSocket.setReuseAddress(true);
// Wait for connection from client.
clientSocket = serverSocket.accept();
// clientSocket.getOutputStream().write(“+PONG\r\n”.getBytes());
// System.out.println(“Received PONG from client!”);
System.out.printf(“Connected with Client : " + clientSocket.getPort() +”\n");
readMultiplePingsFromSameConnection(clientSocket);
}
catch (IOException e) {
System.out.println("IOException: " + e.getMessage());
} finally {
try {
if (clientSocket != null) {
clientSocket.close();
}
} catch (IOException e) {
System.out.println("IOException: " + e.getMessage());
}
}
}
private static void readMultiplePingsFromSameConnection(Socket clientSocket) throws IOException{
BufferedReader br = new BufferedReader(
new InputStreamReader(clientSocket.getInputStream()));
OutputStream outputStream = clientSocket.getOutputStream();
String line;
while ((line = br.readLine()) != null) {
System.out.printf("send response of ping : " + line + "\n");
if (line.equalsIgnoreCase("PING")) {
outputStream.write("+PONG\r\n".getBytes());
System.out.println("Received PONG from client!");
}
}
}
I have refactored it by referring to one of the code examples but still getting this error.
I tried with code example directly, still getting same error, can someone help me with this?