Example usage for java.net ServerSocket bind

List of usage examples for java.net ServerSocket bind

Introduction

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

Prototype

public void bind(SocketAddress endpoint) throws IOException 

Source Link

Document

Binds the ServerSocket to a specific address (IP address and port number).

Usage

From source file:NewFingerServer.java

public static void main(String[] arguments) throws Exception {
    ServerSocketChannel sockChannel = ServerSocketChannel.open();
    sockChannel.configureBlocking(false);

    InetSocketAddress server = new InetSocketAddress("localhost", 79);
    ServerSocket socket = sockChannel.socket();
    socket.bind(server);

    Selector selector = Selector.open();
    sockChannel.register(selector, SelectionKey.OP_ACCEPT);

    while (true) {
        selector.select();/*from ww w  .  j av  a  2s. c  o  m*/
        Set keys = selector.selectedKeys();
        Iterator it = keys.iterator();
        while (it.hasNext()) {
            SelectionKey selKey = (SelectionKey) it.next();
            it.remove();
            if (selKey.isAcceptable()) {
                ServerSocketChannel selChannel = (ServerSocketChannel) selKey.channel();
                ServerSocket selSocket = selChannel.socket();
                Socket connection = selSocket.accept();

                InputStreamReader isr = new InputStreamReader(connection.getInputStream());
                BufferedReader is = new BufferedReader(isr);
                PrintWriter pw = new PrintWriter(new BufferedOutputStream(connection.getOutputStream()), false);
                pw.println("NIO Finger Server");
                pw.flush();
                String outLine = null;
                String inLine = is.readLine();
                if (inLine.length() > 0) {
                    outLine = inLine;
                }
                readPlan(outLine, pw);
                pw.flush();
                pw.close();
                is.close();

                connection.close();
            }
        }
    }
}

From source file:org.springframework.xd.gemfire.CacheServer.java

public static boolean portAvailable(int port) {
    InetSocketAddress addr = new InetSocketAddress(port);
    try {/*from  ww w . j a va2  s . c  o m*/
        ServerSocket socket = new ServerSocket();
        socket.bind(addr);
        socket.close();
    } catch (IOException e) {
        return false;
    }
    return true;

}

From source file:com.nesscomputing.service.discovery.client.TestServiceDiscovery.java

private static final int findUnusedPort() {
    int port;/*  w  w  w . j a  v  a  2s . co  m*/

    ServerSocket socket = null;
    try {
        socket = new ServerSocket();
        socket.bind(new InetSocketAddress(0));
        port = socket.getLocalPort();
    } catch (IOException ioe) {
        throw Throwables.propagate(ioe);
    } finally {
        try {
            if (socket != null) {
                socket.close();
            }
        } catch (IOException ioe) {
            // GNDN
        }
    }

    return port;
}

From source file:com.nesscomputing.httpserver.TestGuiceModule.java

private static int findUnusedPort() throws IOException {
    int port;/*w w  w  .j av  a2s . c  o  m*/

    ServerSocket socket = new ServerSocket();
    try {
        socket.bind(new InetSocketAddress(0));
        port = socket.getLocalPort();
    } finally {
        socket.close();
    }

    return port;
}

From source file:gridool.communication.transport.nio.GridNioServer.java

private static Selector createSelector(int port) throws IOException {
    final Selector selector = SelectorProvider.provider().openSelector();

    ServerSocketChannel serverChannel = ServerSocketChannel.open();
    serverChannel.configureBlocking(false);

    ServerSocket servSocket = serverChannel.socket();
    servSocket.setReuseAddress(true);//  ww w  .  ja v a 2  s.  c  om
    servSocket.bind(new InetSocketAddress(port));

    serverChannel.register(selector, SelectionKey.OP_ACCEPT);

    if (LOG.isInfoEnabled()) {
        LOG.info("GridNioServer is started at port: " + port);
    }
    return selector;
}

From source file:com.nokia.dempsy.mpcluster.zookeeper.ZookeeperTestServer.java

public static int findNextPort() throws IOException {
    // find an unused ehpemeral port
    InetSocketAddress inetSocketAddress = new InetSocketAddress(InetAddress.getLocalHost(), 0);
    ServerSocket serverSocket = new ServerSocket();
    serverSocket.setReuseAddress(true); // this allows the server port to be bound to even if it's in TIME_WAIT
    serverSocket.bind(inetSocketAddress);
    port = serverSocket.getLocalPort();/*ww  w  .j ava  2 s.com*/
    serverSocket.close();
    return port;
}

From source file:com.nesscomputing.service.discovery.server.TestStaticAnnounce.java

private static final int findUnusedPort() {
    int port;/* w w w. j a va2  s.co m*/

    ServerSocket socket = null;
    try {
        socket = new ServerSocket();
        socket.bind(new InetSocketAddress(0));
        port = socket.getLocalPort();
    } catch (final IOException ioe) {
        throw Throwables.propagate(ioe);
    } finally {
        try {
            socket.close();
        } catch (final IOException ioe) {
            // GNDN
        }
    }

    return port;
}

From source file:org.kde.kdeconnect.Backends.LanBackend.LanLink.java

static ServerSocket openTcpSocketOnFreePort() throws IOException {
    boolean success = false;
    int tcpPort = 1739;
    ServerSocket candidateServer = null;
    while (!success) {
        try {/*from  w  w w.j av a  2s.  c  o m*/
            candidateServer = new ServerSocket();
            candidateServer.bind(new InetSocketAddress(tcpPort));
            success = true;
            Log.i("KDE/LanLink", "Using port " + tcpPort);
        } catch (IOException e) {
            //Log.e("LanLink", "Exception openning serversocket: "+e);
            tcpPort++;
            if (tcpPort >= 1764) {
                Log.e("KDE/LanLink", "No more ports available");
                throw e;
            }
        }
    }
    return candidateServer;
}

From source file:org.apache.servicemix.http.HttpWsdlTest.java

/**
 * determines a free port number/*from   w w w  . jav  a2 s  . com*/
 * @return a free port number
 */
private static int getFreePortNumber() {

    int portNumber = usedPortNumber + 1;
    boolean inUse = true;

    while (inUse) {
        try {
            ServerSocket serverSocket = new ServerSocket();
            SocketAddress endpoint = new InetSocketAddress(portNumber);
            serverSocket.bind(endpoint);
            serverSocket.close();
            inUse = false;
            usedPortNumber = portNumber;
        } catch (IOException e) {
            inUse = true;
        }
    }

    return portNumber;

}

From source file:gridool.communication.transport.tcp.GridNioServer.java

private static void startListening(final Selector connectSelector, final int port) throws IOException {
    ServerSocketChannel serverChannel = ServerSocketChannel.open();
    serverChannel.configureBlocking(false);

    ServerSocket servSocket = serverChannel.socket();
    servSocket.setReuseAddress(true);//from   w w w.j  av a  2 s.  co m
    servSocket.bind(new InetSocketAddress(port));

    serverChannel.register(connectSelector, SelectionKey.OP_ACCEPT);
    if (LOG.isInfoEnabled()) {
        LOG.info("GridNioServer is started at port: " + port);
    }
}