Skip to Main Content

Java APIs

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Java NIO - closing socket/connection

843790Apr 9 2008 — edited Apr 9 2008
Dear All,

I am starting to practice on java NIO examples and learn a little bit. I have not gone very far yet. I managed to put together simple server side program which I am using to learn on various examples. Currently it is able to accept connections and read the input from connections. But after that, there is a bit for which I cannot find any information in my guides I have.

How about proper closing socket? I have two scenarios:

1. My networking part is placed in a separate thread from the main one. Main thread can take user's commands and so maintanin networking thread start/stop etc. To stop networking thread I use thread interruption -> net.interrupt();

2. Second scenario is, when client program is connected to running server program, and disconnects from it by any means. My networking thread on server side does stops immediately too. Why? I have no clue :) It is terminated on disconnection from any client side. I do not remember writing any code which should cause this. I mean, my networking thread is able to open socket, channel, selector, and use selector to differ between acceptable and readable keys and then either accept them or read them with a little of processing. that is it. So why does it close itself.

And the worst part of it. Whenever it terminates - i mean in both scenarios listed above. Port or the socket remains still opened. So if I attempt to build my networking thread again on a same port, obviously it would not work, because it is already engaged.

So in general I have two questions:
Why is client disconnection killing my server networking thread as well?
How to terminate thread with proper closure of socket/port upon its end? That thread has infinite blocking loop inside. (that is why i used thread interruption)

Is there anyone who could direct me please? At least to some web resources where I could read for my answers and possibly solutions.

Thank you
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 7 2008
Added on Apr 9 2008
2 comments
363 views