客户端服务器套接字 java.net.SocketException:软件导致连接中止:recv 失败

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12466471/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 09:00:26  来源:igfitidea点击:

Client Server sockets java.net.SocketException: Software caused connection abort: recv failed

javasocketsssltcp

提问by Sarah

Possible Duplicate:
java.net.SocketException: Software caused connection abort: recv failed

可能重复:
java.net.SocketException:软件导致连接中止:recv 失败

I'm trying to write a client server pair where the connection is live all day and the client waits for the server to send a message. The steps are:

我正在尝试编写一个客户端服务器对,其中连接整天都处于活动状态,并且客户端等待服务器发送消息。步骤是:

  1. Server opens port and listens for connection
  2. Client connects in and waits for data
  3. Some time later (maybe hours) the server sends data to the client
  4. Client processes data and returns it to the server
  5. Repeat steps 3 and 4
  1. 服务器打开端口并监听连接
  2. 客户端连接并等待数据
  3. 一段时间后(可能是几个小时),服务器将数据发送给客户端
  4. 客户端处理数据并返回给服务器
  5. 重复步骤 3 和 4

I can get steps 1-4 working, however if I try to repeat step 3 I get the error in the title.

我可以使第 1-4 步工作,但是如果我尝试重复第 3 步,我会收到标题中的错误。

This is my method on the client side:

这是我在客户端的方法:

private static void waitForInput(SSLSocket sslsocket) throws IOException {
        do {
            try {
                ObjectInputStream ois = new ObjectInputStream(sslsocket.getInputStream());
                ObjectOutputStream oos = new ObjectOutputStream(sslsocket.getOutputStream());

                Object o = (Object) (ois.readObject());
                // excluded code to process data     

                oos.flush();
                oos.writeObject(o);
                oos.reset();
            }
            catch(ClassNotFoundException e){
                System.err.println(e.getMessage());
            }
        } while ( true );
    }

The code fails on the 4th line, The first time around it blocks and waits until I get the next bit of data, but it doesn't work twice. What am I doing wrong?

代码在第 4 行失败,第一次在它周围阻塞并等待我获得下一位数据,但它不会工作两次。我究竟做错了什么?

采纳答案by sharadendu sinha

Connection abort IOException is thrown when you are waiting to read from a socket that has been closed at the other end, check to see your server side code , if you are not accidentally closing the socket. Also the server side code could be uploaded for deeper analysis, also try posting the stack trace, it will help analyzing.

Connection abort IOException 在您等待从另一端已关闭的套接字读取时抛出,如果您没有意外关闭套接字,请检查您的服务器端代码。也可以上传服务器端代码进行更深入的分析,也可以尝试发布堆栈跟踪,这将有助于分析。

回答by Ralph Andreasen

You could move the declaration for oisand oosout of the do ... whileloop, since there is no need to redeclare them everytime, might need a try ... catcharound that.

你可以移动的声明oisoos出的do ... while循环,因为没有必要每次重复声明它们,可能需要一个try ... catch解决这一问题。

private static void waitForInput(SSLSocket sslsocket) throws IOException {
    ObjectInputStream ois = new ObjectInputStream(sslsocket.getInputStream());
    ObjectOutputStream oos = new ObjectOutputStream(sslsocket.getOutputStream());
    do {
        try {

            Object o = (Object) (ois.readObject());
            // excluded code to process data     

            oos.writeObject(o);
            oos.flush();
        }
        catch(ClassNotFoundException e){
            System.err.println(e.getMessage());
        }
    } while ( true );
}

And I have removed the oos.reset();and moved the oos.flush();

我已经删除oos.reset();并移动了oos.flush();

I believe that the problem is the oos.reset();and i would never reset a connection that is supposed to be persistent for hours, or, at least part of it.

我相信问题在于oos.reset();,我永远不会重置应该持续数小时或至少部分持续时间的连接。

Besides there is already a oisfor that connection and you don't need two of them.

此外ois,该连接已经有一个,您不需要其中的两个。