views:

43

answers:

2

I have a single-threaded non-blocking socket IO server written in Java using nio.

When I have finished writing to a connection, I want to close it.

Does the closing of the channel mean blocking until all buffered writes have been acknowledged by the recipient?

It would be useful to know if, when asynchronously closing, it succeeded or not, but I could live with any errors in the closing being ignored.

Is there any way to configure this, e.g. with setSoLinger() (and what settings would be appropriate?)

(A general discussion beyond Java about Linux and other OS in this respect would be useful to)

+1  A: 

I'm not sure what really happens but I know that close() includes flush() (except in PrintStream and PrintWriter...).

So my approach would be to add the connections to close to a queue and process that queue in a second thread (including error handling).

I understand that your server is single-threaded but a second thread doesn't cost that much, the complexity of the problem is low and the solution will be easy to understand any maintain.

Aaron Digulla
single threaded is single threaded is single threaded
Schildmeijer
@Schildmeijer: Reality always wins over rules.
Aaron Digulla
the flush in the close seems familiar from classic IO streams; but is that true of channels too?
Will
@Will: Either write a test to find out for sure or assume the worst.
Aaron Digulla
@Aaron Digulla: This is all irrelevant. He is talking about non-blocking I/O, which means java.nio, where there is no flush(), so there is nothing to test; and flush() has nothing to do with buffered data being acknowledged by the recipient in the first place.
EJP
+2  A: 

Closing in non-blocking mode is non-blocking.

You could put the channel into blocking mode, set a positive linger timeout, and close, and that would block for up to the linger timeout while the socket send buffer was being emptied, but alas Java doesn't throw an exception if the linger timeout expires, so you can't know whether all the data has gone. I reported this bug ten or more years ago and it came back 'will not fix' because of compatiblity concerns. If you can wait until Java 7 comes out I believe the nio2 stuff has this fixed, I certainly requested it, but who knows when that will be?

And even if you have all that, all you know is that the data was sent. You don't know anything about it being acknowledged by the recipient. If you need that you have to build it into your application protocol.

EJP
thank you, very much the confirmation of my understanding that I was looking for!
Will