I’m stuck on Stage #NH4 in Build your own Kafka
I followed the code example to run the handle request function recursively. However, after successfully processed the first request, I am stuck at an empty stream and am unable to read the second request.
Here are my logs:
And here’s a snippet of my code:
public static void main(String[] args){
System.err.println("Logs from your program will appear here!");
ServerSocket serverSocket;
Socket clientSocket = null;
final int port = 9092;
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();
int counter = 0;
while (true) {
DataInputStream input = new DataInputStream(clientSocket.getInputStream());
OutputStream output = clientSocket.getOutputStream();
if (input.available() == 0) {
System.out.println("No data available");
Thread.sleep(50);
continue;
}
System.out.println("counter = " + counter);
handleSequentialRequests(input, output);
counter++;
}
} catch (IOException e) {
System.out.println("IOException: " + e.getMessage());
} catch (InterruptedException ie) {
System.out.println("InterruptedException: " + ie.getMessage());
} finally {
try {
if (clientSocket != null) {
clientSocket.close();
}
} catch (IOException e) {
System.out.println("IOException: " + e.getMessage());
}
}
}