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; } }
default ServerSocketChannel createServerSocketChannel(String service_name, int port, int backlog, InetAddress bindAddr) throws IOException { return createServerSocketChannel(service_name).bind(new InetSocketAddress(bindAddr, port), backlog); }
public ServerSocketChannel createServerSocketChannel(String service_name, int port, int backlog, InetAddress bindAddr) throws IOException { return createServerSocketChannel(service_name).bind(new InetSocketAddress(bindAddr, port), backlog); }
default ServerSocketChannel createServerSocketChannel(String service_name, int port, int backlog) throws IOException { return createServerSocketChannel(service_name).bind(new InetSocketAddress(port), backlog); }
public ServerSocketChannel createServerSocketChannel(String service_name, int port) throws IOException { return createServerSocketChannel(service_name).bind(new InetSocketAddress(port)); }
default ServerSocketChannel createServerSocketChannel(String service_name, int port) throws IOException { return createServerSocketChannel(service_name).bind(new InetSocketAddress(port)); }
public ServerSocketChannel createServerSocketChannel(String service_name, int port, int backlog) throws IOException { return createServerSocketChannel(service_name).bind(new InetSocketAddress(port), backlog); }
@Override protected void doBind(SocketAddress localAddress) throws Exception { if (PlatformDependent.javaVersion() >= 7) { javaChannel().bind(localAddress, config.getBacklog()); } else { javaChannel().socket().bind(localAddress, config.getBacklog()); } }
/** * */ public InetSocketAddress start() throws IgniteCheckedException { try { ServerSocketChannel ch = ServerSocketChannel.open(); ch.bind(null); serverChannel = ch; return (InetSocketAddress)ch.getLocalAddress(); } catch (Exception ex) { throw new IgniteCheckedException(ex); } }
public static void bind(final ServerSocketChannel ch, InetAddress bind_addr, int start_port, int end_port, int backlog) throws Exception { int original_start_port=start_port; while(true) { try { ch.bind(new InetSocketAddress(bind_addr, start_port), 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; } }
@Override protected void doBind(SocketAddress localAddress) throws Exception { if (PlatformDependent.javaVersion() >= 7) { javaChannel().bind(localAddress, config.getBacklog()); } else { javaChannel().socket().bind(localAddress, config.getBacklog()); } }
ch.bind(new InetSocketAddress("0.0.0.0", 7500)); ch.configureBlocking(false); ch.register(selector, SelectionKey.OP_ACCEPT, null);
public void start(String ... options) throws Exception { options(options); if(server) { // simple single threaded server, can only handle a single connection at a time srv_channel=ServerSocketChannel.open(); srv_channel.bind(new InetSocketAddress(host, port), 50); System.out.println("server started (ctrl-c to kill)"); for(;;) { client_channel=srv_channel.accept(); // client_channel.socket().setTcpNoDelay(true); // we're concerned about latency receiver_thread=new Receiver(); receiver_thread.start(); } } else { client_channel=SocketChannel.open(); //client_channel.socket().setTcpNoDelay(true); client_channel.connect(new InetSocketAddress(host, port)); receiver_thread=new Receiver(); receiver_thread.start(); } }
/** * Adds a server socket channel for listening to connections. * * @param nicIPAddress - if null binds to wildcard address * @param port - port to bind to * @param receiveBufferSize - size of OS receive buffer to request. If less * than 0 then will not be set and OS default will win. * @throws IOException if unable to add socket */ public void addServerSocket(final InetAddress nicIPAddress, final int port, final int receiveBufferSize) throws IOException { final ServerSocketChannel ssChannel = ServerSocketChannel.open(); ssChannel.configureBlocking(false); if (receiveBufferSize > 0) { ssChannel.setOption(StandardSocketOptions.SO_RCVBUF, receiveBufferSize); final int actualReceiveBufSize = ssChannel.getOption(StandardSocketOptions.SO_RCVBUF); if (actualReceiveBufSize < receiveBufferSize) { LOGGER.warn(this + " attempted to set TCP Receive Buffer Size to " + receiveBufferSize + " bytes but could only set to " + actualReceiveBufSize + "bytes. You may want to consider changing the Operating System's " + "maximum receive buffer"); } } ssChannel.setOption(StandardSocketOptions.SO_REUSEADDR, true); ssChannel.bind(new InetSocketAddress(nicIPAddress, port)); ssChannel.register(serverSocketSelector, SelectionKey.OP_ACCEPT); }
@Override protected void doBind(SocketAddress localAddress) throws Exception { if (PlatformDependent.javaVersion() >= 7) { javaChannel().bind(localAddress, config.getBacklog()); } else { javaChannel().socket().bind(localAddress, config.getBacklog()); } }
public static ServerSocketChannel createServerSocketChannel(SocketFactory factory,String service_name, InetAddress bind_addr, int start_port, int end_port) throws Exception { int original_start_port=start_port; ServerSocketChannel ch=null; while(true) { try { if(ch != null) Util.close(ch); ch=factory.createServerSocketChannel(service_name); ch.bind(new InetSocketAddress(bind_addr, start_port), 50); return ch; } 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++; } } }
@Test public void testInboundConnectionsCountInConnectionCreationMetric() throws Exception { int conns = 5; try (ServerSocketChannel ss = ServerSocketChannel.open()) { ss.bind(new InetSocketAddress(0)); InetSocketAddress serverAddress = (InetSocketAddress) ss.getLocalAddress(); for (int i = 0; i < conns; i++) { Thread sender = createSender(serverAddress, randomPayload(1)); sender.start(); SocketChannel channel = ss.accept(); channel.configureBlocking(false); selector.register(Integer.toString(i), channel); } } assertEquals((double) conns, getMetric("connection-creation-total").metricValue()); assertEquals((double) conns, getMetric("connection-count").metricValue()); }
ss.bind(new InetSocketAddress(0));
@Test public void shouldGivePortConflictErrorWithPortNumberInIt() throws Throwable { // Given an occupied port int port = 16000; try ( ServerSocketChannel ignore = ServerSocketChannel.open().bind( new InetSocketAddress( "localhost", port ) ) ) { final ListenSocketAddress address = new ListenSocketAddress( "localhost", port ); // Expect exception.expect( PortBindException.class ); // When Map<BoltConnector,NettyServer.ProtocolInitializer> initializersMap = genericMap( new BoltConnector( "test" ), protocolOnAddress( address ) ); new NettyServer( new NamedThreadFactory( "mythreads" ), initializersMap, new ConnectorPortRegister(), NullLog.getInstance() ).start(); } }
ss.bind(new InetSocketAddress(0));