#NV3 Response is 0 bytes for tester, locally is correct

Logs:

[compile] [INFO] Scanning for projects...
[compile] [INFO] 
[compile] [INFO] -----------------< io.codecrafters:codecrafters-kafka >-----------------
[compile] [INFO] Building codecrafters-kafka 1.0
[compile] [INFO]   from pom.xml
[compile] [INFO] --------------------------------[ jar ]---------------------------------
[compile] [INFO] 
[compile] [INFO] --- resources:3.3.1:resources (default-resources) @ codecrafters-kafka ---
[compile] [INFO] skip non existing resourceDirectory /app/src/main/resources
[compile] [INFO] 
[compile] [INFO] --- compiler:3.13.0:compile (default-compile) @ codecrafters-kafka ---
[compile] [INFO] Recompiling the module because of changed source code.
[compile] [INFO] Compiling 1 source file with javac [debug target 23] to target/classes
[compile] [INFO] 
[compile] [INFO] --- resources:3.3.1:testResources (default-testResources) @ codecrafters-kafka ---
[compile] [INFO] skip non existing resourceDirectory /app/src/test/resources
[compile] [INFO] 
[compile] [INFO] --- compiler:3.13.0:testCompile (default-testCompile) @ codecrafters-kafka ---
[compile] [INFO] No sources to compile
[compile] [INFO] 
[compile] [INFO] --- surefire:3.2.5:test (default-test) @ codecrafters-kafka ---
[compile] [INFO] No tests to run.
[compile] [INFO] 
[compile] [INFO] --- jar:3.4.1:jar (default-jar) @ codecrafters-kafka ---
[compile] [INFO] Building jar: /app/target/codecrafters-kafka-1.0.jar
[compile] [INFO] 
[compile] [INFO] --- assembly:3.7.1:single (make-assembly) @ codecrafters-kafka ---
[compile] [INFO] Building jar: /tmp/codecrafters-build-kafka-java/codecrafters-kafka.jar
[compile] [WARNING] Configuration option 'appendAssemblyId' is set to false.
[compile] Instead of attaching the assembly file: /tmp/codecrafters-build-kafka-java/codecrafters-kafka.jar, it will become the file for main project artifact.
[compile] NOTE: If multiple descriptors or descriptor-formats are provided for this project, the value of this file will be non-deterministic!
[compile] [WARNING] Replacing pre-existing project main-artifact file: /app/target/codecrafters-kafka-1.0.jar
[compile] with assembly file: /tmp/codecrafters-build-kafka-java/codecrafters-kafka.jar
[compile] [INFO] ------------------------------------------------------------------------
[compile] [INFO] BUILD SUCCESS
[compile] [INFO] ------------------------------------------------------------------------
[compile] [INFO] Total time:  2.322 s
[compile] [INFO] Finished at: 2024-12-21T01:26:57Z
[compile] [INFO] ------------------------------------------------------------------------
[compile] Moved ./.codecrafters/run.sh → ./your_program.sh
[compile] Compilation successful.
Debug = true
[tester::#NV3] Running tests for Stage #NV3 (Send Correlation ID)
[tester::#NV3] $ ./your_program.sh /tmp/server.properties
[tester::#NV3] Connecting to broker at: localhost:9092
[tester::#NV3] Connection to broker at localhost:9092 successful
[tester::#NV3] Sending "ApiVersions" (version: 4) request (Correlation id: 7)
[tester::#NV3] Hexdump of sent "ApiVersions" request: 
[tester::#NV3] Idx  | Hex                                             | ASCII
[tester::#NV3] -----+-------------------------------------------------+-----------------
[tester::#NV3] 0000 | 00 00 00 23 00 12 00 04 00 00 00 07 00 09 6b 61 | ...#..........ka
[tester::#NV3] 0010 | 66 6b 61 2d 63 6c 69 00 0a 6b 61 66 6b 61 2d 63 | fka-cli..kafka-c
[tester::#NV3] 0020 | 6c 69 04 30 2e 31 00                            | li.0.1.
[tester::#NV3] 
[tester::#NV3] Hexdump of received "ApiVersions" response: 
[tester::#NV3] Idx  | Hex                                             | ASCII
[tester::#NV3] -----+-------------------------------------------------+-----------------
[tester::#NV3] | 
[tester::#NV3] 
[tester::#NV3] [Decoder] - .Response
[tester::#NV3] [Decoder] Received:
[tester::#NV3] [Decoder] Hex (bytes 0--1)                                | ASCII
[tester::#NV3] [Decoder] ------------------------------------------------+------------------
[tester::#NV3] [Decoder] | 
[tester::#NV3] [Decoder]  ^                                                ^
[tester::#NV3] [Decoder] Error: Expected int32 length to be 4 bytes, got 0 bytes
[tester::#NV3] [Decoder] Context:
[tester::#NV3] [Decoder] - response
[tester::#NV3] [Decoder]   - message length
[tester::#NV3] [Decoder]     - INT32
[tester::#NV3] [Decoder] 
[tester::#NV3] [Decoder] Test failed
[tester::#NV3] [Decoder] Terminating program
[your_program] #a	kafka-cli
[your_program] kafka-cli0.1
[your_program] IOException: Broken pipe
[tester::#NV3] [Decoder] Program terminated successfully

code:

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.StandardCharsets;

public class Main {
  public static void main(String[] args){

    ServerSocket serverSocket = null;
    Socket clientSocket = null;
    int port = 9092;
    //
    
    try {
      serverSocket = new ServerSocket(port);
      serverSocket.setReuseAddress(true);
      clientSocket = serverSocket.accept();

      DataInputStream in = new DataInputStream(clientSocket.getInputStream());
      DataOutputStream out = new DataOutputStream(clientSocket.getOutputStream());

      int messageSize = 5;
      int header = 7;

      String clientInput = new String(in.readAllBytes(), StandardCharsets.UTF_8);
      System.out.println(clientInput);

      out.writeInt(messageSize);
      out.writeInt(header);

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

local response:

echo -n "Placeholder request" | nc -v localhost 9092 | hexdump -C
00000000  00 00 00 05 00 00 00 07                           |........|
00000008

Hi @BenFletcher0, the issue lies with the readAllBytes() method, which reads until the end of the stream.

This can be problematic for TCP connections, as the method will block indefinitely if the connection remains open.

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