开发者

closing a socket channel asynchronously

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 appro开发者_运维百科priate?)

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


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 received or processed by the recipient application. If you need that you have to build it into your application protocol.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜