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, int backlog) throws IOException 

Source Link

Document

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

Usage

From source file:org.apache.geode.internal.net.SocketCreator.java

public ServerSocket createServerSocket(int nport, int backlog, InetAddress bindAddr,
        List<GatewayTransportFilter> transportFilters, int socketBufferSize) throws IOException {
    if (transportFilters.isEmpty()) {
        return createServerSocket(nport, backlog, bindAddr, socketBufferSize);
    } else {/*from www. j a va  2  s .c  om*/
        printConfig();
        ServerSocket result = new TransportFilterServerSocket(transportFilters);
        result.setReuseAddress(true);
        // Set the receive buffer size before binding the socket so
        // that large buffers will be allocated on accepted sockets (see
        // java.net.ServerSocket.setReceiverBufferSize javadocs)
        result.setReceiveBufferSize(socketBufferSize);
        try {
            result.bind(new InetSocketAddress(bindAddr, nport), backlog);
        } catch (BindException e) {
            BindException throwMe = new BindException(
                    LocalizedStrings.SocketCreator_FAILED_TO_CREATE_SERVER_SOCKET_ON_0_1
                            .toLocalizedString(new Object[] { bindAddr, Integer.valueOf(nport) }));
            throwMe.initCause(e);
            throw throwMe;
        }
        return result;
    }
}

From source file:org.apache.geode.internal.net.SocketCreator.java

private ServerSocket createServerSocket(int nport, int backlog, InetAddress bindAddr, int socketBufferSize,
        boolean sslConnection) throws IOException {
    printConfig();//from   w w w  . java 2 s. c  o m
    if (sslConnection) {
        if (this.sslContext == null) {
            throw new GemFireConfigException("SSL not configured correctly, Please look at previous error");
        }
        ServerSocketFactory ssf = this.sslContext.getServerSocketFactory();
        SSLServerSocket serverSocket = (SSLServerSocket) ssf.createServerSocket();
        serverSocket.setReuseAddress(true);
        // If necessary, set the receive buffer size before binding the socket so
        // that large buffers will be allocated on accepted sockets (see
        // java.net.ServerSocket.setReceiverBufferSize javadocs)
        if (socketBufferSize != -1) {
            serverSocket.setReceiveBufferSize(socketBufferSize);
        }
        serverSocket.bind(new InetSocketAddress(bindAddr, nport), backlog);
        finishServerSocket(serverSocket);
        return serverSocket;
    } else {
        // log.info("Opening server socket on " + nport, new Exception("SocketCreation"));
        ServerSocket result = new ServerSocket();
        result.setReuseAddress(true);
        // If necessary, set the receive buffer size before binding the socket so
        // that large buffers will be allocated on accepted sockets (see
        // java.net.ServerSocket.setReceiverBufferSize javadocs)
        if (socketBufferSize != -1) {
            result.setReceiveBufferSize(socketBufferSize);
        }
        try {
            result.bind(new InetSocketAddress(bindAddr, nport), backlog);
        } catch (BindException e) {
            BindException throwMe = new BindException(
                    LocalizedStrings.SocketCreator_FAILED_TO_CREATE_SERVER_SOCKET_ON_0_1
                            .toLocalizedString(new Object[] { bindAddr, Integer.valueOf(nport) }));
            throwMe.initCause(e);
            throw throwMe;
        }
        return result;
    }
}

From source file:org.apache.geode.internal.net.SocketCreator.java

/**
 * Creates or bind server socket to a random port selected from tcp-port-range which is same as
 * membership-port-range.//from   w  w w. j a  v  a2s. c  o  m
 * 
 * @param ba
 * @param backlog
 * @param isBindAddress
 * @param tcpBufferSize
 * @param sslConnection whether to connect using SSL
 *
 * @return Returns the new server socket.
 *
 * @throws IOException
 */
public ServerSocket createServerSocketUsingPortRange(InetAddress ba, int backlog, boolean isBindAddress,
        boolean useNIO, int tcpBufferSize, int[] tcpPortRange, boolean sslConnection) throws IOException {
    ServerSocket socket = null;
    int localPort = 0;
    int startingPort = 0;

    // Get a random port from range.
    Random rand = new SecureRandom();
    int portLimit = tcpPortRange[1];
    int randPort = tcpPortRange[0] + rand.nextInt(tcpPortRange[1] - tcpPortRange[0] + 1);

    startingPort = randPort;
    localPort = startingPort;

    while (true) {
        if (localPort > portLimit) {
            if (startingPort != 0) {
                localPort = tcpPortRange[0];
                portLimit = startingPort - 1;
                startingPort = 0;
            } else {
                throw new SystemConnectException(
                        LocalizedStrings.TCPConduit_UNABLE_TO_FIND_FREE_PORT.toLocalizedString());
            }
        }
        try {
            if (useNIO) {
                ServerSocketChannel channl = ServerSocketChannel.open();
                socket = channl.socket();

                InetSocketAddress addr = new InetSocketAddress(isBindAddress ? ba : null, localPort);
                socket.bind(addr, backlog);
            } else {
                socket = this.createServerSocket(localPort, backlog, isBindAddress ? ba : null, tcpBufferSize,
                        sslConnection);
            }
            break;
        } catch (java.net.SocketException ex) {
            if (useNIO || SocketCreator.treatAsBindException(ex)) {
                localPort++;
            } else {
                throw ex;
            }
        }
    }
    return socket;
}