0

I am getting a lot of connection reset errors, trough debugging i found out that the code is found within these lines:

//set up output stream for objects
output = new ObjectOutputStream(socket.getOutputStream());
output.flush(); //flush the output buffer to send header information
// Read a message sent by client application
ois = new ObjectInputStream(socket.getInputStream());
String message = (String) ois.readObject();

The error occurs at this line:

String message = (String) ois.readObject();

The error message:

java.net.SocketException: Connection reset
        at java.net.SocketInputStream.read(Unknown Source)
        at java.net.SocketInputStream.read(Unknown Source)
        at java.net.SocketInputStream.read(Unknown Source)
        at java.io.ObjectInputStream$PeekInputStream.peek(Unknown Source)
        at java.io.ObjectInputStream$BlockDataInputStream.peek(Unknown Source)
        at java.io.ObjectInputStream$BlockDataInputStream.peekByte(Unknown Source)
        at java.io.ObjectInputStream.readObject0(Unknown Source)
        at java.io.ObjectInputStream.readObject(Unknown Source)
        at org.bitbucket.c4d3r.ConnectionListener.listen(ConnectionListener.java:47)
        at org.bitbucket.c4d3r.ConnectionListener.<init>(ConnectionListener.java:27)
        at org.bitbucket.c4d3r.ConnectionHandler.connectionServer(ConnectionHandler.java:46)
        at org.bitbucket.c4d3r.ConnectionHandler.run(ConnectionHandler.java:24)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
        at java.lang.Thread.run(Unknown Source)

I know that the connection reset is caused by a socket that's closed unexpectedly, however i am quite new to sockets and i am unable to find what i am doing wrong. Therefore i was hoping that someone could help me out with this. For a better vision of everything i have copied a bigger block of code beneath

private DomainController dc;
    private Socket socket;
    private boolean changed;
    private ObjectOutputStream output;
    private ObjectInputStream ois;
    private Map json;

    public ConnectionListener(DomainController dc, Socket socket) {
        this.dc = dc;
        this.socket = socket;
        listen();
    }

    @Override
    public void run() {

        listen();
        return;
    }

    @SuppressWarnings("finally")
    public void listen() {
        try
        {
            //set up output stream for objects
            output = new ObjectOutputStream(socket.getOutputStream());
            output.flush(); //flush the output buffer to send header information
            // Read a message sent by client application
            ois = new ObjectInputStream(socket.getInputStream());

            String message = (String) ois.readObject();
            boolean inProgress;
            //CHECK IF THERE ARE GIVEN COMMANDS
            if(message.substring(0, 8).equalsIgnoreCase("COMMAND="))
            {
                switch(message.substring(8))
                {
                case "askMinigames":
                    //System.out.println("Sending minigames list");
                    output.writeObject(new String(dc.getProp().getProperty("minigames")));
                    output.flush(); //flush output to client
                    break;
                }
            }
        } catch (SocketException ex) {
            System.out.printf("Connection reset\n");
            ex.printStackTrace();
        } catch (IOException e) {
            System.out.println("Input exception");
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        finally {
            try
            {
                if(ois != null) {
                    ois.close();
                }
                if(output != null) {
                    output.close();
                }
                if(socket != null) {
                    socket.close();
                }
            }
            catch (IOException ioException) {
                ioException.printStackTrace();
            }
        }
    }
}
6
  • In your finally block you may end up with closed resources; you should catch every close() attempt. Commented Jun 15, 2013 at 16:30
  • I did 3 close() attempts in 1 try catch, isn't that sufficient? or should i have 3 seperate try catches? Commented Jun 15, 2013 at 16:41
  • Well, if ois fails to close for instance, you'll exit the (inner) try block immediately Commented Jun 15, 2013 at 16:44
  • I am still having the connection reset error, i already applied your possible fix but it didn't help Commented Jun 15, 2013 at 17:09
  • Who's your client? They can reset the connection too, on the other side. Commented Jun 15, 2013 at 18:07

1 Answer 1

1

You need to use the same object input and output streams for the life of the socket, at both ends. As you are just sending strings, I don't really see why you're using object streams at all: you could use e.g. DataOutputStream.writeUTF() and DataInputStream.readUTF().

Sign up to request clarification or add additional context in comments.

Comments

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.