This discussion is archived
2 Replies Latest reply: May 29, 2012 2:13 AM by sabre150 RSS

SO timeout relevance in non-blocking NIO ServerSocketChannel

sabre150 Expert
Currently Being Moderated
I converted a threaded blocking server from using the standard blocking IO with a thread for each connection to using non-blocking NIO channels. Works great but I'm trying to understand the SO timeout relevance in the context of non-blocking NIO channels. Is there any relevance and should I set an SO timeout on the ServerSocket associated with a ServerSocketChannel and if so how should I handle it?
  • 1. Re: SO timeout relevance in non-blocking NIO ServerSocketChannel
    EJP Guru
    Currently Being Moderated
    No. Socket timeouts are for blocking mode. If you need timeouts in non-blocking mode you have to do them yourself, taking advantage of the Selector.select(long timeout) method and keeping track of activity times per channel yourself. You should use that select() method anyway, rather than just blocking indefinitely, as it gives you a chance to catch up on housekeeping, dead channels, etc. For example as a very naive approach if nothing happens in say a select timeout of 10 minutes you might want to close all accepted SocketChannels.
  • 2. Re: SO timeout relevance in non-blocking NIO ServerSocketChannel
    sabre150 Expert
    Currently Being Moderated
    Thanks EJP. I was concerned that I could not see the relevance of SO Timeout in non-blocking mode. I do use the select(timeout) method to provide connection timeout for certain types of connections so by accident I have done it correctly though I have used the naive approach.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points