简体   繁体   English

套接字重置异常-Java

[英]Socket Reset Exception - Java

I am trying to send a ByteBuffer array over a socket on the port 25565 on the address "localhost". 我正在尝试通过地址“本地主机”上的端口25565上的套接字发送ByteBuffer数组。 But for some reason, Java is throwing a connection reset exception when doing input.read() . 但是由于某种原因,Java在执行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. 注意:是的,我确实知道仅使用input.read()不会获得我尝试发送的整个ByteBuffer数组。 But right now I just want to read the first byte and print it out to the console. 但是现在,我只想读取第一个字节并将其打印到控制台。

  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 . 不要使用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(); } } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM