/** * 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); }
public static int getAvailablePort() { try (ServerSocket ss = new ServerSocket()) { ss.bind(null); return ss.getLocalPort(); } catch (IOException e) { return getRandomPort(); } }
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; }
int start(final RunNiFi runner) throws IOException { serverSocket = new ServerSocket(); serverSocket.bind(new InetSocketAddress("localhost", 0)); final int localPort = serverSocket.getLocalPort(); listener = new Listener(serverSocket, runner); final Thread listenThread = new Thread(listener); listenThread.setName("Listen to NiFi"); listenThread.setDaemon(true); listenThread.start(); return localPort; }
public NetJavaServerSocketImpl (Protocol protocol, String hostname, int port, ServerSocketHints hints) { this.protocol = protocol; // create the server socket try { // initialize server = new java.net.ServerSocket(); if (hints != null) { server.setPerformancePreferences(hints.performancePrefConnectionTime, hints.performancePrefLatency, hints.performancePrefBandwidth); server.setReuseAddress(hints.reuseAddress); server.setSoTimeout(hints.acceptTimeout); server.setReceiveBufferSize(hints.receiveBufferSize); } // and bind the server... InetSocketAddress address; if( hostname != null ) { address = new InetSocketAddress(hostname, port); } else { address = new InetSocketAddress(port); } if (hints != null) { server.bind(address, hints.backlog); } else { server.bind(address); } } catch (Exception e) { throw new GdxRuntimeException("Cannot create a server socket at port " + port + ".", e); } }
public SocketReceiverTester(int port) throws IOException { ServerSocketFactory socketFactory = ServerSocketFactory.getDefault(); acceptor = socketFactory.createServerSocket(); acceptor.setReuseAddress(true); acceptor.bind(new InetSocketAddress(port)); }
public static void bind(ServerSocket srv_sock, InetAddress bind_addr, int start_port, int end_port, int backlog) throws Exception { int original_start_port=start_port; while(true) { try { InetSocketAddress sock_addr=new InetSocketAddress(bind_addr, start_port); srv_sock.bind(sock_addr, backlog); } catch(SocketException bind_ex) { if(start_port == end_port) throw new BindException("No available port to bind to in range [" + original_start_port + " .. " + end_port + "]"); if(bind_addr != null && !bind_addr.isLoopbackAddress()) { NetworkInterface nic=NetworkInterface.getByInetAddress(bind_addr); if(nic == null) throw new BindException("bind_addr " + bind_addr + " is not a valid interface: " + bind_ex); } start_port++; continue; } break; } }
public ServerSocket createServerSocket(String type, InetSocketAddress inetSocketAddress) throws IOException { ServerSocketChannel serverSocketChannel = null; ServerSocket serverSocket = null; if (orb.getORBData().acceptorSocketType().equals(ORBConstants.SOCKETCHANNEL)) { serverSocketChannel = ServerSocketChannel.open(); serverSocket = serverSocketChannel.socket(); } else { serverSocket = new ServerSocket(); } serverSocket.bind(inetSocketAddress); return serverSocket; }
Leader(QuorumPeer self,LeaderZooKeeperServer zk) throws IOException { this.self = self; this.proposalStats = new ProposalStats(); try { if (self.getQuorumListenOnAllIPs()) { ss = new ServerSocket(self.getQuorumAddress().getPort()); } else { ss = new ServerSocket(); } ss.setReuseAddress(true); if (!self.getQuorumListenOnAllIPs()) { ss.bind(self.getQuorumAddress()); } } catch (BindException e) { if (self.getQuorumListenOnAllIPs()) { LOG.error("Couldn't bind to port " + self.getQuorumAddress().getPort(), e); } else { LOG.error("Couldn't bind to " + self.getQuorumAddress(), e); } throw e; } this.zk=zk; }
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 ); }
@Override protected void startInner() { if(logger.isEnabledFor(Level.INFO)) logger.info("Starting Voldemort NIO socket server (" + serviceName + ") on port " + port); try { for(int i = 0; i < selectorManagers.length; i++) { selectorManagers[i] = new NioSelectorManager(endpoint, requestHandlerFactory, socketBufferSize, socketKeepAlive, selectorMaxHeartBeatTimeMs); selectorManagerThreadPool.execute(selectorManagers[i]); } serverSocketChannel.socket().bind(endpoint, acceptorBacklog); serverSocketChannel.socket().setReceiveBufferSize(socketBufferSize); serverSocketChannel.socket().setReuseAddress(true); acceptorThread.start(); } catch(Exception e) { throw new VoldemortException(e); } enableJmx(this); }
serverSocket = serverSocketFactory.createServerSocket(); serverSocket.setReuseAddress(inetSocketAddress.getPort() != 0); serverSocket.bind(inetSocketAddress, 50);
@Override public ServerSocket createServerSocket(int port) throws IOException { ServerSocketChannel serverSocketChannel = ServerSocketChannel.open(); serverSocketChannel.socket().bind(new InetSocketAddress(port)); return serverSocketChannel.socket(); }
public void start() throws IOException { logger.debug("Starting Bootstrap Listener to communicate with Bootstrap Port {}", bootstrapPort); serverSocket = new ServerSocket(); serverSocket.bind(new InetSocketAddress("localhost", 0)); serverSocket.setSoTimeout(2000); final int localPort = serverSocket.getLocalPort(); logger.info("Started Bootstrap Listener, Listening for incoming requests on port {}", localPort); listener = new Listener(serverSocket); final Thread listenThread = new Thread(listener); listenThread.setDaemon(true); listenThread.setName("Listen to Bootstrap"); listenThread.start(); logger.debug("Notifying Bootstrap that local port is {}", localPort); sendCommand("PORT", new String[] { String.valueOf(localPort), secretKey}); }
public NetJavaServerSocketImpl (Protocol protocol, String hostname, int port, ServerSocketHints hints) { this.protocol = protocol; // create the server socket try { // initialize server = new java.net.ServerSocket(); if (hints != null) { server.setPerformancePreferences(hints.performancePrefConnectionTime, hints.performancePrefLatency, hints.performancePrefBandwidth); server.setReuseAddress(hints.reuseAddress); server.setSoTimeout(hints.acceptTimeout); server.setReceiveBufferSize(hints.receiveBufferSize); } // and bind the server... InetSocketAddress address; if( hostname != null ) { address = new InetSocketAddress(hostname, port); } else { address = new InetSocketAddress(port); } if (hints != null) { server.bind(address, hints.backlog); } else { server.bind(address); } } catch (Exception e) { throw new GdxRuntimeException("Cannot create a server socket at port " + port + ".", e); } }
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 { 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(String.format("Failed to create server socket on %s[%s]", bindAddr, Integer.valueOf(nport))); throwMe.initCause(e); throw throwMe; } return result; } }
@Override public void run() { try { httpd.getMyServerSocket().bind(httpd.hostname != null ? new InetSocketAddress(httpd.hostname, httpd.myPort) : new InetSocketAddress(httpd.myPort)); hasBinded = true; } catch (IOException e) { this.bindException = e; return; } do { try { final Socket finalAccept = httpd.getMyServerSocket().accept(); if (this.timeout > 0) { finalAccept.setSoTimeout(this.timeout); } final InputStream inputStream = finalAccept.getInputStream(); httpd.asyncRunner.exec(httpd.createClientHandler(finalAccept, inputStream)); } catch (IOException e) { NanoHTTPD.LOG.log(Level.FINE, "Communication with the client broken", e); } } while (!httpd.getMyServerSocket().isClosed()); }
@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; } }
public static int getAvailablePort() { try (ServerSocket ss = new ServerSocket()) { ss.bind(null); return ss.getLocalPort(); } catch (IOException e) { return getRandomPort(); } }