套接字重置异常 - Java

问题描述 投票:0回答:1

我试图通过地址“localhost”上的端口25565上的套接字发送ByteBuffer数组。但由于某种原因,Java在执行input.read()时会抛出连接重置异常。有人可以告诉我发生了什么事吗?

发件人:

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

接收器:

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

堆栈跟踪:

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)

注意:是的,我知道只使用input.read()将无法获得我正在尝试发送的整个ByteBuffer阵列。但是现在我只想读取第一个字节并将其打印到控制台。

java sockets tcp stack-trace packet
1个回答
1
投票
  1. 您没有关闭发件人中的连接,因此在进程退出时会重置该连接。 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. 您只读取一个字节然后关闭连接。您需要阅读整个传输。如果关闭仍未处理的未读数据的套接字,则重置连接。此外,如果要单独处理异常,请单独捕获它们。不要使用instanceofpublic 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(); } }
© www.soinside.com 2019 - 2024. All rights reserved.