Example usage for java.net ServerSocket isBound

List of usage examples for java.net ServerSocket isBound

Introduction

In this page you can find the example usage for java.net ServerSocket isBound.

Prototype

public boolean isBound() 

Source Link

Document

Returns the binding state of the ServerSocket.

Usage

From source file:io.tilt.minka.domain.NetworkShardIDImpl.java

private void testPort() {
    ServerSocket socket = null;
    try {//from   w  w w.j a  v  a  2s  .  co  m
        socket = new ServerSocket(port);
        logger.info("{}: Testing host {} port {} OK", getClass().getSimpleName(), sourceHost, port);
    } catch (IOException e) {
        throw new IllegalArgumentException("Testing port cannot be opened: " + port, e);
    } finally {
        if (socket != null && !socket.isBound()) {
            throw new IllegalArgumentException("Testing port cannot be opened: " + port);
        } else if (socket != null) {
            try {
                socket.close();
            } catch (IOException e) {
                throw new IllegalArgumentException("Testing port cannot be tested: " + port);
            }
        }
    }
}

From source file:com.github.hrpc.rpc.Server.java

public static void bind(ServerSocket socket, InetSocketAddress address, int backlog, Option conf,
        String rangeConf) throws IOException {
    try {//ww w.j  av  a  2 s. c  o  m
        IntegerRanges range = null;
        if (rangeConf != null) {
            range = conf.getRange(rangeConf, "");
        }
        if (range == null || range.isEmpty() || (address.getPort() != 0)) {
            socket.bind(address, backlog);
        } else {
            for (Integer port : range) {
                if (socket.isBound())
                    break;
                try {
                    InetSocketAddress temp = new InetSocketAddress(address.getAddress(), port);
                    socket.bind(temp, backlog);
                } catch (BindException e) {
                    //Ignored
                }
            }
            if (!socket.isBound()) {
                throw new BindException("Could not find a free port in " + range);
            }
        }
    } catch (SocketException e) {
        throw NetUtils.wrapException(null, 0, address.getHostName(), address.getPort(), e);
    }
}

From source file:org.apache.ftpserver.socketfactory.FtpSocketFactoryTest.java

private void testCreateServerSocket(final FtpSocketFactory ftpSocketFactory, final int port)
        throws Exception, IOException {
    boolean freePort = false;
    try {/*ww  w. j  av  a  2s  .c  o  m*/
        final ServerSocket testSocket = new ServerSocket(port, 100);
        freePort = testSocket.isBound();
        testSocket.close();
    } catch (Exception exc) {
        // ok
        freePort = true;
    }
    if (freePort) {
        new Thread() {
            public void run() {
                synchronized (this) {
                    try {
                        this.wait(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                        fail(e.toString());
                    }
                }
                try {
                    Socket socket = new Socket();
                    socket.connect(new InetSocketAddress("localhost", port));
                    socket.getInputStream();
                    socket.getOutputStream();
                    socket.close();
                } catch (UnknownHostException e) {
                    e.printStackTrace();
                    fail(e.toString());
                } catch (IOException e) {
                    e.printStackTrace();
                    fail(e.toString());
                }
            }
        }.start();
        ServerSocket serverSocket = ftpSocketFactory.createServerSocket();
        assertNotNull(serverSocket);
        serverSocket.accept();
    }
}

From source file:org.apache.hadoop.ipc.TestServer.java

@Test
public void testBind() throws Exception {
    Configuration conf = new Configuration();
    ServerSocket socket = new ServerSocket();
    InetSocketAddress address = new InetSocketAddress("0.0.0.0", 0);
    socket.bind(address);/*from w w  w. j a  v a2 s  .  c  o  m*/
    try {
        int min = socket.getLocalPort();
        int max = min + 100;
        conf.set("TestRange", min + "-" + max);

        ServerSocket socket2 = new ServerSocket();
        InetSocketAddress address2 = new InetSocketAddress("0.0.0.0", 0);
        Server.bind(socket2, address2, 10, conf, "TestRange");
        try {
            assertTrue(socket2.isBound());
            assertTrue(socket2.getLocalPort() > min);
            assertTrue(socket2.getLocalPort() <= max);
        } finally {
            socket2.close();
        }
    } finally {
        socket.close();
    }
}

From source file:org.apache.hadoop.ipc.TestServer.java

@Test
public void testBindSimple() throws Exception {
    ServerSocket socket = new ServerSocket();
    InetSocketAddress address = new InetSocketAddress("0.0.0.0", 0);
    Server.bind(socket, address, 10);/*from w ww . j  a v a 2 s  .c om*/
    try {
        assertTrue(socket.isBound());
    } finally {
        socket.close();
    }
}

From source file:org.apache.hadoop.ipc.TestServer.java

@Test
public void testEmptyConfig() throws Exception {
    Configuration conf = new Configuration();
    conf.set("TestRange", "");

    ServerSocket socket = new ServerSocket();
    InetSocketAddress address = new InetSocketAddress("0.0.0.0", 0);
    try {/*from   w  ww . j av  a2s  .  c  om*/
        Server.bind(socket, address, 10, conf, "TestRange");
        assertTrue(socket.isBound());
    } finally {
        socket.close();
    }
}

From source file:org.kercoin.magrit.Magrit.java

void tryBind(int port) throws IOException {
    ServerSocket ss = null;
    try {/*  ww w  .  ja va 2s . co m*/
        ss = new ServerSocket(port);
    } finally {
        if (ss != null && ss.isBound()) {
            ss.close();
        }
    }
}