0

I am trying to send a ByteBuffer array over a socket on the port 25565 on the address "localhost". But for some reason, Java is throwing a connection reset exception when doing input.read(). Could someone please tell me whats going on?

Sender:

private static Socket socket;

public static void main(String[] args) throws IOException {
    socket = new Socket("localhost", 25565);
    String Password = "1234";
    ByteBuffer Buffer = ByteBuffer.allocate(1 + Password.getBytes().length);
    Buffer.put((byte) 0x00);
    Buffer.putShort((short) Password.getBytes().length);
    Buffer.put(Password.getBytes());
    DataOutputStream output = new DataOutputStream(socket.getOutputStream());
    output.write(Buffer.array());
}

public static void sendBytes(byte[] myByteArray) throws IOException {
    DataOutputStream output = new DataOutputStream(socket.getOutputStream());
    output.write("LOL".getBytes());
    output.flush();
}

Receiver:

public static void main(String[] args) {
    try {
        ServerSocket ServerSocket = new ServerSocket(25565);
        System.out.println("Waiting for connection...");
        Socket socket = ServerSocket.accept();
        DataInputStream Input = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
        System.out.println(Input.read());
        ServerSocket.close();
        socket.close();
    } catch (Exception e) {
        if(e instanceof SocketTimeoutException) {
            System.out.println("THE SOCKET TIMED OUT!");
        }
        else {
            e.printStackTrace();
        }
    }
}

Stack trace:

java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(SocketInputStream.java:189)
at java.net.SocketInputStream.read(SocketInputStream.java:121)
at java.io.BufferedInputStream.fill(BufferedInputStream.java:246)
at java.io.BufferedInputStream.read(BufferedInputStream.java:265)
at java.io.FilterInputStream.read(FilterInputStream.java:83)
at net.networking.Receiver.main(Receiver.java:17)

NOTE: Yes, I do know that just using input.read() will not get the whole ByteBuffer array I'm trying to send. But right now I just want to read the first byte and print it out to the console.

1 Answer 1

1
  1. You're not closing the connection in the sender, so it gets reset when the process exits.

    private static Socket socket;
    
    public static void main(String[] args) throws IOException {
        socket = new Socket("localhost", 25565);
        String Password = "1234";
        sendBytes(Password.getBytes());
        output.close();
    }
    
    public static void sendBytes(byte[] myByteArray) throws IOException {
        ByteBuffer Buffer = ByteBuffer.allocate(3 + myByteArray.length);
        Buffer.put((byte) 0x00);
        Buffer.putShort((short) myByteArray.length);
        Buffer.put(myByteArray);
        DataOutputStream output = new DataOutputStream(socket.getOutputStream());
        output.write(Buffer.array());
        output.flush();
    }
    
  2. You're only reading one byte and then closing the connection. You need to read the entire transmission. If you close a socket with unread data still pending, the connection is reset. Also, if you want to handle exceptions separately, catch them separately. Don't use instanceof.

    public static void main(String[] args) {
        try {
            ServerSocket ServerSocket = new ServerSocket(25565);
            System.out.println("Waiting for connection...");
            Socket socket = ServerSocket.accept();
            DataInputStream Input = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
            byte b = Input.readByte();
            short dataLen = Input.readShort();
            byte[] data = new byte[dataLen];
            Input.readFully(data);
            // use data as needed...
            System.out.println("Data received");
            Input.close();
            ServerSocket.close();
        } catch (SocketTimeoutException e) {
            System.out.println("THE SOCKET TIMED OUT!");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
Sign up to request clarification or add additional context in comments.

1 Comment

Still doesnt work, tried what you said. Could you send me the exact code fix you were proposing? I just want to make sure I put what you mean.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.