List of usage examples for java.nio.channels ServerSocketChannel open
public static ServerSocketChannel open() throws IOException
From source file:org.cloudata.core.commitlog.CommitLogServer.java
private int startFileTransferChannel(String dirName, FileChannel[] fileChannelList) { int port = -1; try {//from w w w . j a v a 2 s .c o m ServerSocketChannel ssc = ServerSocketChannel.open(); ssc.configureBlocking(false); ServerSocket serverSocket = ssc.socket(); serverSocket.bind(null); port = serverSocket.getLocalPort(); new FileTransferThread(dirName, ssc, fileChannelList).start(); //LOG.info("File transfer thread is started and read method is done"); } catch (IOException e) { LOG.warn("starting file transfer is fail", e); } return port; }
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 va 2s . co 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; }
From source file:org.quickserver.net.server.QuickServer.java
/** * Returns a ServerSocket object to be used for listening. * @since 1.4.0//from w w w .java 2s . co 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()); } }