套接字重置异常 - Java

时间:2015-02-18 01:29:11

标签: java sockets tcp stack-trace packet

我正在尝试在地址“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数组。但是现在我只想读取第一个字节并将其打印到控制台。

1 个答案:

答案 0 :(得分: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. 您只需读取一个字节然后关闭连接。您需要阅读整个传输。如果关闭仍未处理的未读数据的套接字,则重置连接。此外,如果您想单独处理异常,请分别捕获它们。请勿使用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();
        }
    }