Debug help #NH4: unable to read second request

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());
            }
        }
    }

Hey @yuistar, could you upload your code to GitHub and share the link? It will be much easier to debug if I can run it directly.

@andy1li I uploaded my code to github
src/main/java/Main.java
Thank you in advance!

any update on this?

@yuistar, I tried running your code against the previous stages, but it’s actually no longer passing a previous stage #NV3 (Send Correlation ID).

Suggestions:

  1. Use our CLI to test against previous stages by running:
codecrafters test --previous
  1. Focus on fixing the early stages first, as later stages depend on them.
1 Like

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

Closing this thread due to inactivity. If you still need assistance, feel free to reopen or start a new discussion!