Bwmat
Bwmat

Reputation: 4578

Checking if a port is bound & listening in Java; 'resetting' a SocketChannel?

class CheckServerStarted implements Closeable
    {
        private final SocketChannel channel;
        private final InetSocketAddress address;
        private boolean connStarted = false;

        public CheckServerStarted(InetSocketAddress address) throws IOException
        {
            channel = SocketChannel.open();
            channel.configureBlocking(false);

            this.address = address;
        }

        /**
         * @throws IOException 
         * 
         */
        private void startConn() throws IOException
        {
            channel.connect(address);
            connStarted = true;
        }

        private boolean check(int timeout, boolean retry)
        {
            for (int i = 0; i < timeout; i++)
            {
                try
                {
                    final boolean justStarted = !connStarted;
                    if (justStarted)
                    {
                        startConn();
                    }

                    if (justStarted || (i > 0))
                    {
                        // Wait a second between attempts/give the connection some time to get established.
                        try
                        {
                            Thread.sleep(1000);
                        }
                        catch (InterruptedException e)
                        {
                            throw new RuntimeException(e);
                        }
                    }

                    if (channel.finishConnect())
                    {
                        System.out.println("Server started - accepting connections on " + address.toString());
                        return true;
                    }
                }
                catch (IOException e)
                {
                    System.out.println("Connect attempt failed : " + e.getMessage());
                    if (!retry)
                    {
                        break;
                    }

                    // Try to start the connection again if it failed.
                    connStarted = false;
                }
            }

            System.out.println("Connect attempt to " + address.toString() + " failed, ran out of time/attempts");
            return false;
        }

        /* (non-Javadoc)
         * @see java.io.Closeable#close()
         */
        public void close()
        {
            try
            {
                channel.close();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
        }
    }

I have the following code, which is intended to be used to wait/check for a server to start & be listening on a given address/port.

The idea is that I can, for example, call

new CheckServerStarted(addr).check(60, true);

to try to connect to it for up to 60 seconds (retrying if the connection fails since the server hasn't bound the socket yet)

or

new CheckServerStarted(addr).check(1, false);

to check that the server is currently accepting connections.

When I run that first example now, I get output like

Connect attempt failed : Connection refused: no further information
14-Jun-2018 10:55:42    Connect attempt failed : null
14-Jun-2018 10:55:42    Connect attempt failed : null
14-Jun-2018 10:55:42    Connect attempt failed : null
14-Jun-2018 10:55:42    Connect attempt failed : null
14-Jun-2018 10:55:42    Connect attempt failed : null
14-Jun-2018 10:55:42    Connect attempt failed : null
14-Jun-2018 10:55:42    Connect attempt failed : null
14-Jun-2018 10:55:42    Connect attempt failed : null
14-Jun-2018 10:55:42    Connect attempt failed : null
14-Jun-2018 10:55:42    Connect attempt failed : null
14-Jun-2018 10:55:42    Connect attempt failed : null
14-Jun-2018 10:55:42    Connect attempt failed : null
14-Jun-2018 10:55:42    Connect attempt failed : null
14-Jun-2018 10:55:42    Connect attempt failed : null
14-Jun-2018 10:55:42    Connect attempt failed : null
14-Jun-2018 10:55:42    Connect attempt failed : null
14-Jun-2018 10:55:42    Connect attempt failed : null
14-Jun-2018 10:55:42    Connect attempt failed : null
14-Jun-2018 10:55:42    Connect attempt failed : null
14-Jun-2018 10:55:42    Connect attempt failed : null
14-Jun-2018 10:55:42    Connect attempt failed : null
14-Jun-2018 10:55:42    Connect attempt failed : null
14-Jun-2018 10:55:42    Connect attempt failed : null
14-Jun-2018 10:55:42    Connect attempt failed : null
14-Jun-2018 10:55:42    Connect attempt failed : null
14-Jun-2018 10:55:42    Connect attempt failed : null
14-Jun-2018 10:55:42    Connect attempt failed : null
14-Jun-2018 10:55:42    Connect attempt failed : null
14-Jun-2018 10:55:42    Connect attempt failed : null
14-Jun-2018 10:55:42    Connect attempt failed : null
14-Jun-2018 10:55:42    Connect attempt failed : null
14-Jun-2018 10:55:42    Connect attempt failed : null
14-Jun-2018 10:55:42    Connect attempt failed : null
14-Jun-2018 10:55:42    Connect attempt failed : null
14-Jun-2018 10:55:42    Connect attempt failed : null
14-Jun-2018 10:55:42    Connect attempt failed : null
14-Jun-2018 10:55:42    Connect attempt failed : null
14-Jun-2018 10:55:42    Connect attempt failed : null
14-Jun-2018 10:55:42    Connect attempt failed : null
14-Jun-2018 10:55:42    Connect attempt failed : null
14-Jun-2018 10:55:42    Connect attempt failed : null
14-Jun-2018 10:55:42    Connect attempt failed : null
14-Jun-2018 10:55:42    Connect attempt failed : null
14-Jun-2018 10:55:42    Connect attempt failed : null
14-Jun-2018 10:55:42    Connect attempt failed : null
14-Jun-2018 10:55:42    Connect attempt failed : null
14-Jun-2018 10:55:42    Connect attempt failed : null
14-Jun-2018 10:55:42    Connect attempt failed : null
14-Jun-2018 10:55:42    Connect attempt failed : null
14-Jun-2018 10:55:42    Connect attempt failed : null
14-Jun-2018 10:55:42    Connect attempt failed : null
14-Jun-2018 10:55:42    Connect attempt failed : null
14-Jun-2018 10:55:42    Connect attempt failed : null
14-Jun-2018 10:55:42    Connect attempt failed : null
14-Jun-2018 10:55:42    Connect attempt failed : null
14-Jun-2018 10:55:42    Connect attempt failed : null
14-Jun-2018 10:55:42    Connect attempt failed : null
14-Jun-2018 10:55:42    Connect attempt failed : null
14-Jun-2018 10:55:42    Connect attempt failed : null
14-Jun-2018 10:55:42    Connect attempt to /192.168.223.68:59695 failed, ran out of time/attempts

It's obviously not waiting 1 second between attempts.

I'm guessing I need to 'reset' channel in between attempts? Do I need to create a new one every time?

Upvotes: 0

Views: 124

Answers (1)

user207421
user207421

Reputation: 310964

You can't reconnect a socket that has been connected, even if the connect attempt failed. Have to close it and create a new one.

Other problems:

  • You are leaking the socket.
  • Get rid of the sleeps and use a Selector.
  • print e instead of e.getMessage(). If you had done this properly in the first place, instead of just staring at 'null' all day, you would probably have solved the problem yourself.
  • an IOException calling finishConnect() is fatal, not a signal for a retry.

Upvotes: 1

Related Questions