title: Socket client, idle timeout
body: <p>Is there anyway to set a timeout for a client socket if the client don't get/send any message in a certain time?</p>
  
  <p>here is a sniplet of my code :</p>
  
  <pre><code>try {
    mainSocket = new ServerSocket(portNumber);
    clientSocket = mainSocket.accept();
    in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
    out = new PrintWriter(clientSocket.getOutputStream(), true);
  
  } catch (IOException ex) {
    Logger.error(this, "failed to accept connection. Error was " + ex.getMessage());
  }
  
  while (reading) {
    try {
  
      char[] arr = new char[2048];
      ch = 0;
      ch = in.read(arr);
      if (ch &lt; 0) {
        reading = false;
      }
      else {
      //process message
    } catch (IOException ex) {
      Logger.error(this, ex);
    }
  }
  </code></pre>
  
  <p>now what I want to do is if after x second the clientSocket didn't get any message, it will exit the loop, close the connection and restart accepting connection.</p>
  
  <p>I know how to close the connection and restart accepting connection, but I still can't figure the rest. Is there anyway to accomplish this?</p>
  
  <p>Thanks for the help</p>