List of usage examples for java.nio.channels ServerSocketChannel open
public static ServerSocketChannel open() throws IOException
From source file:gridool.util.xfer.TransferServer.java
public TransferServer(@Nonnull ExecutorService execPool, @CheckForNull TransferRequestListener handler) { if (handler == null) { throw new IllegalArgumentException(); }// w w w . j a v a 2 s .com this.execPool = execPool; this.handler = handler; try { this.serverChannel = ServerSocketChannel.open(); } catch (IOException e) { LOG.error(e); throw new IllegalStateException(e); } }
From source file:org.springframework.boot.devtools.tunnel.client.TunnelClient.java
/** * Start the client and accept incoming connections on the port. * @throws IOException in case of I/O errors *///from ww w . jav a 2 s . c om public synchronized void start() throws IOException { Assert.state(this.serverThread == null, "Server already started"); ServerSocketChannel serverSocketChannel = ServerSocketChannel.open(); serverSocketChannel.socket().bind(new InetSocketAddress(this.listenPort)); logger.trace("Listening for TCP traffic to tunnel on port " + this.listenPort); this.serverThread = new ServerThread(serverSocketChannel); this.serverThread.start(); }
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);/* www . j a v a 2 s .c o m*/ 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:net.timewalker.ffmq4.listeners.tcp.nio.NIOTcpListener.java
private void initServerSocket() throws JMSException { try {/*from w ww.j av a 2s . c o m*/ serverSocketChannel = ServerSocketChannel.open(); serverSocketChannel.configureBlocking(false); serverSocketChannel.socket().setReuseAddress(true); int tcpBackLog = settings.getIntProperty(FFMQServerSettings.LISTENER_TCP_BACK_LOG, DEFAULT_TCP_BACK_LOG); InetAddress bindAddress = getBindAddress(); InetSocketAddress isa = new InetSocketAddress(bindAddress, listenPort); serverSocketChannel.socket().bind(isa, tcpBackLog); } catch (Exception e) { throw new FFMQException("Could not configure server socket", "NETWORK_ERROR", e); } }
From source file:org.apache.james.mpt.DiscardProtocol.java
/** * Starts serving./*w w w. j a v a2s. c o m*/ * @throws IOException when connection fails * @throws IllegalStateException when already started */ public void start() throws IOException { synchronized (queue) { if (socket == null) { socket = ServerSocketChannel.open(); socket.socket().bind(new InetSocketAddress(port)); // only going to record a single conversation socket.configureBlocking(false); final Thread socketMonitorThread = new Thread(new SocketMonitor()); socketMonitorThread.start(); } else { throw new IllegalStateException("Already started"); } } }
From source file:oz.hadoop.yarn.api.net.AbstractSocketHandler.java
/** * // www . j av a 2 s. co m * @param address * @param server */ public AbstractSocketHandler(InetSocketAddress address, boolean server, Runnable onDisconnectTask) { Assert.notNull(address); this.onDisconnectTask = onDisconnectTask; this.address = address; this.executor = Executors.newCachedThreadPool(); this.listenerTask = new ListenerTask(); this.readingBuffer = ByteBuffer.allocate(16384); this.bufferPoll = new ByteBufferPool(); try { this.rootChannel = server ? ServerSocketChannel.open() : SocketChannel.open(); if (logger.isDebugEnabled()) { logger.debug("Created instance of " + this.getClass().getSimpleName()); } } catch (Exception e) { throw new IllegalStateException("Failed to create an instance of the ApplicationContainerClient", e); } this.thisClass = this.getClass(); this.lifeCycleLatch = new CountDownLatch(1); }
From source file:com.verisign.storm.metrics.GraphiteMetricsConsumerTest.java
@BeforeClass private void setupTestFixtures() { try {/*from w w w . j ava 2 s . c o m*/ testGraphiteServerSocketAddr = new InetSocketAddress(testGraphiteHost, testGraphitePort); testGraphiteServer = ServerSocketChannel.open(); testGraphiteServer.socket().bind(testGraphiteServerSocketAddr); testGraphiteServer.configureBlocking(false); } catch (IOException e) { LOG.error("Failed to open Graphite server at {}:{}", testGraphiteHost, testGraphitePort); } }
From source file:uk.ac.horizon.ubihelper.j2se.Server.java
public void init(InetAddress address, int port) { protocol = new MyProtocolManager(); peerConnectionListener = new ProtocolManager.ClientConnectionListener(protocol); // create channels // create server socket try {/*from ww w .j a v a2 s .c om*/ serverSocketChannel = ServerSocketChannel.open(); ServerSocket ss = serverSocketChannel.socket(); ss.bind(new InetSocketAddress(InetAddress.getByName("0.0.0.0"), port)); serverPort = ss.getLocalPort(); logger.info("Open server socket on port " + serverPort); serverSocketChannel.configureBlocking(false); } catch (IOException e) { logger.severe("Error opening ServerSocketChannel: " + e.getMessage()); failed = true; return; } try { selector = new PeerConnectionScheduler(serverSocketChannel); selector.setListener(selectorListener); selector.start(); } catch (IOException e) { logger.severe("Error starting Selector: " + e.getMessage()); failed = true; return; } // create and advertise with DnsServer dns = new DnsServer(); NetworkInterface ni = null; try { ni = NetworkInterface.getByInetAddress(address);//DnsClient.getFirstActiveInterface(); } catch (Exception e) { logger.severe("Could not get NetworkInterface for " + address + ": " + e); } dns.setNeworkInterface(ni); InetAddress ip = getInetAddress(ni); logger.info("Binding for multicast to " + ip.getHostAddress()); id = ip.getHostAddress() + ":" + serverPort; String servicename = DnsUtils.getServiceDiscoveryName(); String name = ip.getHostAddress(); SrvData srv = new SrvData(1, 1, serverPort, name); logger.info("Discoverable " + name + " as " + servicename); dns.add(new DnsProtocol.RR(servicename, DnsProtocol.TYPE_SRV, DnsProtocol.CLASS_IN, DEFAULT_TTL, DnsProtocol.srvToData(srv))); String instancename = "Server on " + ip; logger.info("Discoverable as " + instancename + " " + servicename); DnsProtocol.RR ptrRR = new DnsProtocol.RR(servicename, DnsProtocol.TYPE_PTR, DnsProtocol.CLASS_IN, DEFAULT_TTL, DnsProtocol.ptrToData(instancename, servicename)); dns.add(ptrRR); dns.start(); }
From source file:org.apache.hadoop.hdfs.server.datanode.SecureDataNodeStarter.java
@Override public void init(DaemonContext context) throws Exception { System.err.println("Initializing secure datanode resources"); // We should only start up a secure datanode in a Kerberos-secured cluster Configuration conf = new Configuration(); // Skip UGI method to not log in if (!conf.get(HADOOP_SECURITY_AUTHENTICATION).equals("kerberos")) throw new RuntimeException("Cannot start secure datanode in unsecure cluster"); // Stash command-line arguments for regular datanode args = context.getArguments();//from w w w .j av a2 s . c o m // Obtain secure port for data streaming to datanode InetSocketAddress socAddr = DataNode.getStreamingAddr(conf); int socketWriteTimeout = conf.getInt("dfs.datanode.socket.write.timeout", HdfsConstants.WRITE_TIMEOUT); ServerSocket ss = (socketWriteTimeout > 0) ? ServerSocketChannel.open().socket() : new ServerSocket(); ss.bind(socAddr, 0); // Check that we got the port we need if (ss.getLocalPort() != socAddr.getPort()) throw new RuntimeException("Unable to bind on specified streaming port in secure " + "context. Needed " + socAddr.getPort() + ", got " + ss.getLocalPort()); // Obtain secure listener for web server SelectChannelConnector listener = (SelectChannelConnector) HttpServer.createDefaultChannelConnector(); InetSocketAddress infoSocAddr = DataNode.getInfoAddr(conf); listener.setHost(infoSocAddr.getHostName()); listener.setPort(infoSocAddr.getPort()); // Open listener here in order to bind to port as root listener.open(); if (listener.getPort() != infoSocAddr.getPort()) throw new RuntimeException("Unable to bind on specified info port in secure " + "context. Needed " + socAddr.getPort() + ", got " + ss.getLocalPort()); if (ss.getLocalPort() >= 1023 || listener.getPort() >= 1023) throw new RuntimeException( "Cannot start secure datanode on non-privileged " + " ports. (streaming port = " + ss + " ) (http listener port = " + listener.getConnection() + "). Exiting."); System.err.println("Successfully obtained privileged resources (streaming port = " + ss + " ) (http listener port = " + listener.getConnection() + ")"); resources = new SecureResources(ss, listener); }
From source file:com.navjagpal.fileshare.WebServer.java
public WebServer(Context context, SharedPreferences sharedPreferences, SQLiteDatabase cookiesDatabase, int port) throws IOException { mPort = port;/*from ww w. ja v a2 s.co m*/ mServerSocketChannel = ServerSocketChannel.open(); mServerSocketChannel.socket().setReuseAddress(true); mServerSocketChannel.socket().bind(new InetSocketAddress(mPort)); mContext = context; mSharedPreferences = sharedPreferences; mCookiesDatabase = cookiesDatabase; deleteOldCookies(); }