Example usage for javax.net ServerSocketFactory createServerSocket

List of usage examples for javax.net ServerSocketFactory createServerSocket

Introduction

In this page you can find the example usage for javax.net ServerSocketFactory createServerSocket.

Prototype

public abstract ServerSocket createServerSocket(int port, int backlog, InetAddress ifAddress)
        throws IOException;

Source Link

Document

Returns a server socket bound to the specified port, with a specified listen backlog and local IP.

Usage

From source file:io.github.kitarek.elasthttpd.server.networking.HttpConfiguredServerSocket.java

private static ServerSocket createServerSocketUnchecked(final SocketConfiguration socketConfiguration)
        throws IOException {
    ServerSocketFactory serverSocketFactory = ServerSocketFactory.getDefault();
    final ServerSocket serverSocket = serverSocketFactory.createServerSocket(
            socketConfiguration.getListeningPort(), socketConfiguration.getConnectionsToAcceptQueueSize(),
            socketConfiguration.getListeningAddress());
    setSocketReceiveBufferSizeWhenProvided(socketConfiguration, serverSocket);
    setSocketReuseAddress(socketConfiguration, serverSocket);
    return serverSocket;
}

From source file:it.jnrpe.server.CBindingThread.java

private void init() throws IOException, KeyManagementException, KeyStoreException, CertificateException,
        UnrecoverableKeyException {
    InetAddress addr = InetAddress.getByName(m_Binding.getIP());
    ServerSocketFactory sf = null;

    if (m_Binding.useSSL())
        sf = getSSLSocketFactory(m_Binding.getKeyStoreFile(), m_Binding.getKeyStorePassword(), "JKS");
    else//from w w  w  . jav a 2  s . c  om
        sf = ServerSocketFactory.getDefault();

    m_serverSocket = sf.createServerSocket(m_Binding.getPort(), 0, addr);
    //m_serverSocket.setSoTimeout(10000); //Ten seconds timeout added by oriol.lopez
    if (m_serverSocket instanceof SSLServerSocket)
        ((SSLServerSocket) m_serverSocket)
                .setEnabledCipherSuites(((SSLServerSocket) m_serverSocket).getSupportedCipherSuites());

    // Init the thread factory
    m_threadFactory = new CThreadFactory(m_Binding.getThreadFactoryConfig());
}

From source file:org.quickserver.net.server.QuickServer.java

/**
 * Returns a ServerSocket object to be used for listening.
 * @since 1.4.0//  ww w  .  jav a 2 s.  c o m
 */
protected void makeServerSocket() throws BindException, IOException {
    server = null;
    logger.log(Level.FINEST, "Binding {0} to IP: {1}", new Object[] { getName(), getBindAddr() });
    InetSocketAddress bindAddress = new InetSocketAddress(getBindAddr(), getPort());

    try {
        NetworkInterface ni = NetworkInterface.getByInetAddress(getBindAddr());
        if (ni != null) {
            logger.fine("NetworkInterface: " + ni);
        }
    } catch (Exception igrnore) {
        /*ignore*/} catch (Error igrnore) {
        /*ignore*/}

    if (getSecure().isEnable() == false) {
        logger.log(Level.FINE, "Making a normal ServerSocket for {0}", getName());
        setRunningSecure(false);

        if (getBlockingMode() == false) {
            //for non-blocking
            serverSocketChannel = ServerSocketChannel.open();
            server = serverSocketChannel.socket();
            server.bind(bindAddress, getBasicConfig().getAdvancedSettings().getBacklog());
        } else {
            //for blocking
            server = new ServerSocket(getPort(), getBasicConfig().getAdvancedSettings().getBacklog(),
                    getBindAddr());
        }
    } else {
        try {
            logger.log(Level.FINE, "Making a secure ServerSocket for {0}", getName());
            getSSLContext();
            setRunningSecure(true);

            if (getBlockingMode() == false) {

                logger.log(Level.FINE, "Making a secure ServerSocketChannel for {0}", getName());
                //for non-blocking
                serverSocketChannel = ServerSocketChannel.open();
                server = serverSocketChannel.socket();
                server.bind(bindAddress, getBasicConfig().getAdvancedSettings().getBacklog());
            } else {

                ServerSocketFactory ssf = getSSLContext().getServerSocketFactory();
                SSLServerSocket serversocket = (SSLServerSocket) ssf.createServerSocket(getPort(),
                        getBasicConfig().getAdvancedSettings().getBacklog(), getBindAddr());
                serversocket.setNeedClientAuth(secure.isClientAuthEnable());
                setRunningSecure(true);

                secureStoreManager.logSSLServerSocketInfo(serversocket);

                server = serversocket;
                serverSocketChannel = server.getChannel();

                if (serverSocketChannel == null && getBlockingMode() == false) {
                    logger.warning("Secure Server does not support Channel! So will run in blocking mode.");
                    blockingMode = false;
                }

            } //blocking
        } catch (NoSuchAlgorithmException e) {
            logger.log(Level.WARNING, "NoSuchAlgorithmException : {0}", e);
            throw new IOException("Error creating secure socket : " + e.getMessage());
        } catch (KeyManagementException e) {
            logger.log(Level.WARNING, "KeyManagementException : {0}", e);
            throw new IOException("Error creating secure socket : " + e.getMessage());
        }
    }

    server.setReuseAddress(true);

    int connectionTime = 0;
    int latency = 0;
    int bandwidth = 0;

    connectionTime = getBasicConfig().getAdvancedSettings().getPerformancePreferencesConnectionTime();
    latency = getBasicConfig().getAdvancedSettings().getPerformancePreferencesLatency();
    bandwidth = getBasicConfig().getAdvancedSettings().getPerformancePreferencesBandwidth();

    logger.log(Level.FINE, "getPerformancePreferencesConnectionTime : {0}", connectionTime);
    logger.log(Level.FINE, "getPerformancePreferencesLatency : {0}", latency);
    logger.log(Level.FINE, "getPerformancePreferencesBandwidth : {0}", bandwidth);

    server.setPerformancePreferences(connectionTime, latency, bandwidth);

    int clientSocketReceiveBufferSize = getBasicConfig().getAdvancedSettings()
            .getClientSocketReceiveBufferSize();
    if (clientSocketReceiveBufferSize > 0) {
        logger.log(Level.FINE, "clientSocketReceiveBufferSize: {0}", clientSocketReceiveBufferSize);
        server.setReceiveBufferSize(clientSocketReceiveBufferSize);
    }

    if (getBlockingMode() == false) {
        logger.log(Level.FINE, "Server Mode {0} - Non Blocking", getName());
        if (selector == null || selector.isOpen() == false) {
            logger.finest("Opening new selector");
            selector = Selector.open();
        } else {
            logger.log(Level.FINEST, "Reusing selector: {0}", selector);
        }
        serverSocketChannel.configureBlocking(false);
        serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);
        selector.wakeup();
    } else {
        logger.log(Level.FINE, "Server Mode {0} - Blocking", getName());
    }
}