1

I have the following code:

    private Socket mSenderSocket = null;

    private DataInputStream in = null;
    private DataOutputStream out = null;

    ...

        try 
        {
            mSenderSocket = new Socket(java.net.InetAddress.getByName(TCP_SERVER_IP), 12345);
            out = new DataOutputStream(mSenderSocket.getOutputStream());
            in = new DataInputStream(mSenderSocket.getInputStream());
        } 
        catch (UnknownHostException ex) 
        {
           System.err.println("Don't know about host.");
        }
        catch(IOException ex)
        {
            System.err.println("Couldn't get I/O");
        }

I am interested in the following scenario:

During the chat session if an I/O exception occurs then I have to do the following:

mSenderSocket = new Socket(java.net.InetAddress.getByName(TCP_SERVER_IP);

Now what happens with the DataInputStream and the DataOutputStream?

Should I initialize theese objects again or not?

7
  • I didn't get it, are you trying to assign something to an already initialized final variable? Commented Apr 19, 2013 at 12:27
  • obviously not! if mSenderSocket.getOutputStream() throws an exception what should i do with the out (DataOutputStream ) variable? Commented Apr 19, 2013 at 12:29
  • I don't get it as well, first of all, why are you trying to declare as final de Data Streams? Commented Apr 19, 2013 at 12:29
  • because i want to synchronize on it but it is another story... Commented Apr 19, 2013 at 12:30
  • Sorry for my ignorance, but I didn't get the question! Commented Apr 19, 2013 at 12:31

1 Answer 1

3

Almost certainly in an error condition you want to throw away the entire object and start again, or don't even construct the object in the first place.

(You also probably want a finally on that to close the Socket. As it happens, calling close on either stream or on the Socket will completely close all three objects.)

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.