/** * Binds TCP socket on the provided <code>port</code>. * * @throws IOException if any I/O error occurs. */ @Override public void bind() throws IOException { getJavaChannel().socket().bind( new InetSocketAddress(InetAddress.getLocalHost(), port)); getJavaChannel().configureBlocking(false); LOGGER.info("Bound TCP socket at port: {}", port); }
/** * Starts listening to slave connections. * * @throws Exception If fails. */ public void beginAccept() throws Exception { this.serverSocketChannel = ServerSocketChannel.open(); this.selector = RemotingUtil.openSelector(); this.serverSocketChannel.socket().setReuseAddress(true); this.serverSocketChannel.socket().bind(this.socketAddressListen); this.serverSocketChannel.configureBlocking(false); this.serverSocketChannel.register(this.selector, SelectionKey.OP_ACCEPT); }
private static ServerSocketChannel newSocket() { try { ServerSocketChannel ch = ServerSocketChannel.open(); ch.configureBlocking(false); return ch; } catch (IOException e) { throw new NioException("Open a server socket error:" + e.getMessage(), e); } }
public Listener() throws IOException { address = new InetSocketAddress(bindAddress, port); // Create a new server socket and set to non blocking mode acceptChannel = ServerSocketChannel.open(); acceptChannel.configureBlocking(false); // Bind the server socket to the local host and port bind(acceptChannel.socket(), address, backlogLength, conf, portRangeConfig); port = acceptChannel.socket().getLocalPort(); //Could be an ephemeral port // create a selector; selector= Selector.open(); readers = new Reader[readThreads]; for (int i = 0; i < readThreads; i++) { Reader reader = new Reader( "Socket Reader #" + (i + 1) + " for port " + port); readers[i] = reader; reader.start(); } // Register accepts on the server socket with the selector. acceptChannel.register(selector, SelectionKey.OP_ACCEPT); this.setName("IPC Server listener on " + port); this.setDaemon(true); }
/** * This is used to configure the server socket for non-blocking * mode. It will also bind the server socket to the socket port * specified in the <code>SocketAddress</code> object. Once done * the acceptor is ready to accept newly arriving connections. * * @param address this is the server socket address to bind to */ private void bind(SocketAddress address) throws IOException { listener.configureBlocking(false); socket.setReuseAddress(true); socket.bind(address, 100); }
/** * Creates an instance of {@link NioServer} that opens a server channel and listens for connections. * Needs to be started next. * @param thread_factory The thread factory used to create new threads * @param socket_factory The socket factory used to create socket channels * @param bind_addr The local address to bind to. If null, the address will be picked by the OS * @param srv_port The local port to bind to If 0, the port will be picked by the OS. * @param end_port If srv_port is taken, the next port is tried, until end_port has been reached, in which case an * exception will be thrown. If srv_port == end_port, only 1 port will be tried. * @param external_addr The external address in case of NAT. Ignored if null. * @param external_port The external port on the NA. If 0, srv_port is used. * @throws Exception Thrown if the creation failed */ public NioServer(ThreadFactory thread_factory, SocketFactory socket_factory, InetAddress bind_addr, int srv_port, int end_port, InetAddress external_addr, int external_port) throws Exception { super(thread_factory, socket_factory); // channel=this.socket_factory.createServerSocketChannel("jgroups.nio.server"); // channel.setOption(StandardSocketOptions.SO_REUSEADDR, reuse_addr); // Util.bind(channel, bind_addr, srv_port, end_port); channel=Util.createServerSocketChannel(this.socket_factory, "jgroups.nio.server", bind_addr, srv_port, end_port); channel.configureBlocking(false); selector=Selector.open(); acceptor=factory.newThread(new Acceptor(), "NioServer.Selector [" + channel.getLocalAddress() + "]"); channel.register(selector, SelectionKey.OP_ACCEPT, null); local_addr=localAddress(bind_addr, channel.socket().getLocalPort(), external_addr, external_port); }
public NIOAcceptor(String name, int port, FrontendConnectionFactory factory) throws IOException { super.setName(name); this.port = port; this.selector = Selector.open(); this.serverChannel = ServerSocketChannel.open(); this.serverChannel.socket().bind(new InetSocketAddress(port)); this.serverChannel.configureBlocking(false); this.serverChannel.register(selector, SelectionKey.OP_ACCEPT); this.factory = factory; }
conf.getInt("hbase.ipc.server.read.connection-queue.size", 100); acceptChannel = ServerSocketChannel.open(); acceptChannel.configureBlocking(false); bind(acceptChannel.socket(), bindAddress, backlogLength); port = acceptChannel.socket().getLocalPort(); //Could be an ephemeral port address = (InetSocketAddress)acceptChannel.socket().getLocalSocketAddress();
@NotNull @Override public ServerSocketChannel channelFor(Selector selector) throws IOException { ServerSocketChannel myServerSocketChannel = selector.provider().openServerSocketChannel(); myServerSocketChannel.configureBlocking(false); int bindPort = -1; for (int port = firstTryPort; port <= lastTryPort; port++) { try { InetSocketAddress tryAddress = new InetSocketAddress(port); myServerSocketChannel.socket().bind(tryAddress); bindPort = tryAddress.getPort(); break; } catch (IOException e) { //try next port logger.debug("Could not bind to port {}, trying next port...", port); } } if (bindPort == -1) { logger.error(String.format( "No available ports in range [%d, %d] for the BitTorrent client!", firstTryPort, lastTryPort )); throw new IOException("No available port for the BitTorrent client!"); } return myServerSocketChannel; } }
private static ServerSocketChannel newSocket() { try { ServerSocketChannel ch = ServerSocketChannel.open(); ch.configureBlocking(false); return ch; } catch (IOException e) { throw new NioException("Open a server socket error:" + e.getMessage(), e); } }
Socket acceptedSocket = null; try { serverSocketChannel.configureBlocking(false); final ServerSocket serverSocket = serverSocketChannel.socket(); serverSocket.setSoTimeout(2000); while (!stopped.get() && acceptedSocket == null) {
ListenTask(int port) throws IOException { this.port = port; this.ssc = ServerSocketChannel.open(); ssc.socket().setReuseAddress(true); ssc.socket().bind(new InetSocketAddress(port), LISTEN_BACKLOG); ssc.configureBlocking(false); endpoint = new EndPoint(NioSelectorScheduler.this,ssc); // if port is automatically assigned then retrieve actual value if(port == 0) { this.port = ssc.socket().getLocalPort(); }; }
NioServerSocketChannel( ChannelFactory factory, ChannelPipeline pipeline, ChannelSink sink, Boss boss, WorkerPool<NioWorker> workerPool) { super(factory, pipeline, sink); this.boss = boss; this.workerPool = workerPool; try { socket = ServerSocketChannel.open(); } catch (IOException e) { throw new ChannelException( "Failed to open a server socket.", e); } try { socket.configureBlocking(false); } catch (IOException e) { try { socket.close(); } catch (IOException e2) { if (logger.isWarnEnabled()) { logger.warn( "Failed to close a partially initialized socket.", e2); } } throw new ChannelException("Failed to enter non-blocking mode.", e); } config = new DefaultServerSocketChannelConfig(socket.socket()); fireChannelOpen(this); }
/** @param udpPort May be null. */ public void bind (InetSocketAddress tcpPort, InetSocketAddress udpPort) throws IOException { close(); synchronized (updateLock) { selector.wakeup(); try { serverChannel = selector.provider().openServerSocketChannel(); serverChannel.socket().bind(tcpPort); serverChannel.configureBlocking(false); serverChannel.register(selector, SelectionKey.OP_ACCEPT); if (DEBUG) debug("kryonet", "Accepting connections on port: " + tcpPort + "/TCP"); if (udpPort != null) { udp = new UdpConnection(serialization, objectBufferSize); udp.bind(selector, udpPort); if (DEBUG) debug("kryonet", "Accepting connections on port: " + udpPort + "/UDP"); } } catch (IOException ex) { close(); throw ex; } } if (INFO) info("kryonet", "Server opened."); }
private Tunnel open() throws IOException { try { this.server = ServerSocketChannel.open().bind(null); this.server.configureBlocking(false); Selector selector = Selector.open(); startTunnelThread(selector); return this; } catch (IOException ioe) { LOG.error("Failed to open the tunnel", ioe); throw ioe; } }
public void connect() throws IOException { // Create a new selector this.selector = SelectorProvider.provider().openSelector(); // Create a new non-blocking server socket channel this.serverChannel = ServerSocketChannel.open(); serverChannel.configureBlocking(false); // Bind the server socket to the specified address and port serverChannel.socket().bind(address); // Register the server socket channel, indicating an interest in // accepting new connections serverChannel.register(selector, SelectionKey.OP_ACCEPT); log.log( Level.FINE, "Hosting TCP connection:{0}.", address ); }
try { boolean ok = false; final ServerSocketChannel serverChannel = ServerSocketChannel.open(); try { serverChannel.configureBlocking(false); if (optionMap.contains(Options.RECEIVE_BUFFER)) { serverChannel.socket().setReceiveBufferSize(optionMap.get(Options.RECEIVE_BUFFER, -1)); serverChannel.socket().setReuseAddress(optionMap.get(Options.REUSE_ADDRESSES, true)); serverChannel.bind(destination); if (bindListener != null) ChannelListeners.invokeChannelListener(new BoundChannel() {
protected void start(boolean direct) throws Exception { selector=Selector.open(); ch=ServerSocketChannel.open(); ch.bind(new InetSocketAddress("0.0.0.0", 7500)); ch.configureBlocking(false); ch.register(selector, SelectionKey.OP_ACCEPT, null); System.out.println("-- server ready");
@Override public void configure(InetSocketAddress addr, int maxcc) throws IOException { configureSaslLogin(); thread = new ZooKeeperThread(this, "NIOServerCxn.Factory:" + addr); thread.setDaemon(true); maxClientCnxns = maxcc; this.ss = ServerSocketChannel.open(); ss.socket().setReuseAddress(true); LOG.info("binding to port " + addr); ss.socket().bind(addr); ss.configureBlocking(false); ss.register(selector, SelectionKey.OP_ACCEPT); }
/** * 获取并配置ServerSocketChannel * * @return 返回配置好的ServerSocketChannel * @throws IOException */ private ServerSocketChannel getServerSocketChannel() throws IOException { final ServerSocketChannel serverSocketChannel = ServerSocketChannel.open(); serverSocketChannel.configureBlocking(false); serverSocketChannel.socket().setSoTimeout(options.getServerSocketTimeout()); return serverSocketChannel; }