-1

I Used java.nio for server programming and it works fine. When I try to close socket:

    serverChannel.socket().close();

        serverChannel.close();  

    boolean b1 = serverChannel.socket().isBound();   
  boolean b2 =
     serverChannel.socket().isClosed();

and check values, b1 is true and b2 is true. When I run netstat I see that state od port is "LISTENNING" While I was using "old" IO, closing socket did excatly what I expected (netstat did not list port as Listenning).

How can I "unbind" socket without shuting down JVM?

GauravRatnawat
  • 717
  • 1
  • 11
  • 25
  • 1
    `isBound()` just means that you called `bind()`, directly or indirectly. It never becomes false again. It doesn't mean the underlying port is still in use. There is no problem here to solve UNLESS you are using it with a `Selector`, in which case the close doesn't take effect until the next select operation. You can force it with `selectNow()`. – user207421 Dec 03 '19 at 11:22
  • NB You don't need both closes. Either is sufficient. – user207421 Dec 03 '19 at 11:30
  • @user207421 "There is no problem here to solve UNLESS you are using it with a Selector, in which case the close doesn't take effect until the next select operation. You can force it with selectNow()" I am using selector but i could not get it. Do you have any link or reference. – GauravRatnawat Dec 03 '19 at 11:58
  • Seems perfectly clear to me. See the Javadoc. – user207421 Dec 03 '19 at 20:31

1 Answers1

-1

I Got the solution. We have to keep in mind that keys contains serverSocketChannel as well.

if(this.serverChannel != null && this.serverChannel.isOpen()) {

    try {

        this.serverChannel.close();

    } catch (IOException e) {

        log.error("Exception while closing server socket");
    }
}

try {

    Iterator<SelectionKey> keys = this.selector.keys().iterator();

    while(keys.hasNext()) {

        SelectionKey key = keys.next();

        SelectableChannel channel = key.channel();

        if(channel instanceof SocketChannel) {

            SocketChannel socketChannel = (SocketChannel) channel;
            Socket socket = socketChannel.socket();
            String remoteHost = socket.getRemoteSocketAddress().toString();

            log.info("closing socket {}", remoteHost);

            try {

                socketChannel.close();

            } catch (IOException e) {

                log.warn("Exception while closing socket", e);
            }

            key.cancel();
        }
    }

    log.info("closing selector");
    selector.close();

} catch(Exception ex) {

    log.error("Exception while closing selector", ex);
}

Does Selector.close() closes all client sockets?

GauravRatnawat
  • 717
  • 1
  • 11
  • 25
  • There is nothing here that addresses the LISTENING problem you stated in your question, and closing all the client sockets when you close the server socket is needlessly abortive. What happened when you tried `shutdownNow()`? – user207421 Dec 04 '19 at 07:25