Refine search
private static boolean isLocalPortAvailable(int port) { try (ServerSocket socket = new ServerSocket(port)) { socket.setReuseAddress(true); return true; } catch (IOException e) { return false; } } }
/** * 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); }
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); } }
private static int findFreePort() { try(ServerSocket socket = new ServerSocket(0)) { socket.setReuseAddress(true); return socket.getLocalPort(); } catch (Exception ignore) { } throw new IllegalStateException("Could not find a free TCP/IP port to start dlv"); } }
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); } }
serverSocket = serverSocketFactory.createServerSocket(); serverSocket.setReuseAddress(inetSocketAddress.getPort() != 0); serverSocket.bind(inetSocketAddress, 50);
private static int findFreePort() { try (ServerSocket socket = new ServerSocket(0)) { socket.setReuseAddress(true); return socket.getLocalPort(); } catch (Exception ignore) { } throw new IllegalStateException("Could not find a free TCP/IP port to start debugging"); } }
try { server = new ServerSocket(); server.setReuseAddress(true); if (addr != null) { server.bind(new InetSocketAddress(addr, port)); } else { server.bind(new InetSocketAddress(port));
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(); }; }
/** * Get a server socket from an available port from a base port.<br> * Increasing on port number will occur when the port has already been used. * * @param basePort base port to start * @return new socket with available port */ private static ServerSocket getServerSocketFromBasePort(int basePort) { int tryCount = 0; while (true) { try { ServerSocket server = new ServerSocket(basePort + tryCount / 3, 100); server.setReuseAddress(true); return server; } catch (IOException e) { tryCount++; try { TimeUnit.MILLISECONDS.sleep(30); } catch (InterruptedException e1) { break; } } } return null; }
ss = new ServerSocket(); ss.setReuseAddress( true ); ss.bind(new InetSocketAddress((InetAddress) null, port), 0);
@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); }
protected static int getNextTCPPort(InetAddress bind_addr, int start_port) throws Exception { try(ServerSocket sock=new ServerSocket()) { sock.setReuseAddress(false); Util.bind(sock, bind_addr, start_port, start_port+100); return sock.getLocalPort(); } }
while((!shutdown) && (numRetries < 3)){ try { ss = new ServerSocket(); ss.setReuseAddress(true); if (listenOnAllIPs) { int port = view.get(QuorumCnxManager.this.mySid) setName(view.get(QuorumCnxManager.this.mySid) .electionAddr.toString()); ss.bind(addr); while (!shutdown) { Socket client = ss.accept();
public InProcessChronicleSource(@NotNull Chronicle chronicle, int port) throws IOException { this.chronicle = chronicle; server = ServerSocketChannel.open(); server.socket().setReuseAddress(true); server.socket().bind(new InetSocketAddress(port)); name = chronicle.name() + "@" + port; logger = Logger.getLogger(getClass().getName() + "." + name); service = Executors.newCachedThreadPool(new NamedThreadFactory(name)); service.execute(new Acceptor()); }
/** * Starts HTTP tunnel. Method ends when the tunnel is stopped. */ public void start() throws IOException { serverSocket = new ServerSocket(listenPort, socketBacklog); serverSocket.setReuseAddress(true); executorService = Executors.newFixedThreadPool(threadPoolSize); running = true; while (running) { Socket socket = serverSocket.accept(); socket.setKeepAlive(false); executorService.execute(onSocketConnection(socket)); } executorService.shutdown(); }
private ServerSocket createServerSocket( int port ) throws IOException { ServerSocket serverSocket = new ServerSocket(); serverSocket.setPerformancePreferences( 1, 2, 3 ); // order of importance: bandwidth, latency, connection time serverSocket.setReuseAddress( true ); serverSocket.bind( new InetSocketAddress( port ) ); } catch ( BindException e ) { long totalWait = 0L; + port + " after a " + ( totalWait / 1000 ) + " seconds wait..." ); Thread.sleep( 10000 ); // wait 10 seconds, try again... serverSocket.bind( new InetSocketAddress( port ), 100 ); } catch ( IOException ioe ) { ioException = ioe;
@Override public void reconfigure(InetSocketAddress addr) { ServerSocketChannel oldSS = ss; try { acceptThread.setReconfiguring(); tryClose(oldSS); acceptThread.wakeupSelector(); try { acceptThread.join(); } catch (InterruptedException e) { LOG.error("Error joining old acceptThread when reconfiguring client port {}", e.getMessage()); Thread.currentThread().interrupt(); } this.ss = ServerSocketChannel.open(); ss.socket().setReuseAddress(true); LOG.info("binding to port " + addr); ss.socket().bind(addr); ss.configureBlocking(false); acceptThread = new AcceptThread(ss, addr, selectorThreads); acceptThread.start(); } catch(IOException e) { LOG.error("Error reconfiguring client port to {} {}", addr, e.getMessage()); tryClose(oldSS); } }
private ServerSocket getServerSocket() throws IOException { ServerSocket serverSocket = null; try { serverSocket = new ServerSocket(port, 100); serverSocket.setReuseAddress(true); } catch (BindException e) { port = port + 1; serverSocket = getServerSocket(); if (portFindTimes++ > 50) { throw e; } } return serverSocket; }
final int maxClients = Integer.parseInt(args[arg++]); try (final ServerSocket s = new ServerSocket()) { s.setReuseAddress(true); s.setSoTimeout(30000); // initially 30 secs to give clients enough time to startup s.bind(new InetSocketAddress(hostname, 0)); final InetSocketAddress localAddr = (InetSocketAddress) s.getLocalSocketAddress(); System.out.println("Listening on " + localAddr + "...");