Nicolas Hanout
Nicolas Hanout

Reputation: 19

if i set a timeout on a socket will its input stream still throw a timeout exception even if i wrap it in another type

I want to try to read from a socket's input stream for a certain time and then do something else if i don't receive any input i know i can set a timeout for the socket that will do that

mySocket.setSoTimeout(200);

but will it still work (will the InputStream still throw an exception to my catch block) even if i incapsulate it in ObjectInputStream

public void startClient(){
    try {
        connection = new Socket(InetAddress.getByName(hostName), port);
        output = new ObjectOutputStream( connection.getOutputStream());
        output.flush();
        input = new ObjectInputStream( connection.getInputStream());
    }
    catch (IOException ex) {
        Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
    }
    ExecutorService worker = Executors.newSingleThreadExecutor();
    worker.execute( this );
}

and is there any other way of doing it if this doesn't work. plus if the stream starts reading and it times out will it stop in the middle of it or will it continue until there are no more bytes in the stream as in: will the stream timeout after it starts reading the object if the 200ms pass?

i want to do something like this

    while(!connection.isClosed()){
        try{
            com = (String) input.readObject();
            if(com.equals("TERMINATE CONNECTION")){
                closeConnection();
            }else if(com.equals("SEND DATA")){
                sendData();
            }
        }catch(timeout exception){
             if( timedout and want to do something){ do something else ....}
        }
            com="";
  }

thanks everybody

Upvotes: 1

Views: 668

Answers (1)

user207421
user207421

Reputation: 311050

If I set a timeout on the socket's input stream

You don't 'set a timeout on the socket input stream'. You set the timeout on the socket itself.

will it still work even if I cast that stream to another type?

There is no casting to another type here. You are wrapping the socket input stream in another type. There is no way the socket read timeout can possibly be affected by that, and no way for the socket to even know that you've done it.

In short the question doesn't make sense.

plus if the stream starts reading and it times out will it stop in the middle of it or will it continue until there are no more bytes in the stream

I can't make head or tail of this either, but if you get a timeout it means no data arrived within the timeout periods. It doesn't break the connection. It might however break the ObjectInputStream, if you somehow get a timeout in the middle of reading an object.

NB:

  1. The timeout manifests itself as a SocketTimeoutException, not as something you detect in an if-else chain.

  2. It isn't correct to loop while connection.isClosed() returns false. It doesn't magically become true when the peer closes the connection. In this case the correct technique is to loop until you get an EOFException.

Upvotes: 1

Related Questions