/** * Initiates the shuts down of the listener. */ public void shutdown() { shuttingDown = true; try { SocketAddress localAddress = serverSocket.getLocalAddress(); if (localAddress instanceof InetSocketAddress) { InetSocketAddress address = (InetSocketAddress) localAddress; Socket client = new Socket(address.getHostName(), address.getPort()); client.setSoTimeout(1000); // waking the acceptor loop should be quick new PingAgentProtocol().connect(client); } } catch (IOException e) { LOGGER.log(Level.FINE, "Failed to send Ping to wake acceptor loop", e); } try { serverSocket.close(); } catch (IOException e) { LOGGER.log(Level.WARNING, "Failed to close down TCP port",e); } }
public int getPort() throws IOException { SocketAddress localAddress = null; try { if (this.server != null && this.server.isOpen()) { localAddress = this.server.getLocalAddress(); } if (localAddress instanceof InetSocketAddress) { return ((InetSocketAddress) localAddress).getPort(); } } catch (IOException e) { LOG.error("Failed to get tunnel port", e); throw e; } return NON_EXISTENT_PORT; }
/** * */ 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); } }
@Override public void applyx(IgniteInternalFuture<?> future) throws IgniteCheckedException { try { if (log != null && log.isInfoEnabled()) log.info("Server socket closed " + ch.getLocalAddress()); ch.close(); } catch (Exception ex) { U.error(log, "Fail close socket.", ex); throw new IgniteCheckedException(ex); } } });
/** * 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); }
@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)); InetSocketAddress serverAddress = (InetSocketAddress) ss.getLocalAddress();
@Override @Nullable public SocketAddress getServerSocketAddress() { if (this.serverChannel != null) { try { return this.serverChannel.getLocalAddress(); } catch (IOException e) { } } return null; }
ss.bind(new InetSocketAddress(0)); InetSocketAddress serverAddress = (InetSocketAddress) ss.getLocalAddress();
@Override public InetAddress getLocalAddress() { try { return ((InetSocketAddress) server.getLocalAddress()).getAddress(); } catch (IOException e) { throw new IllegalStateException("Cannot determine local address", e); } }
@Override public int getLocalPort() { try { return ((InetSocketAddress) server.getLocalAddress()).getPort(); } catch (IOException e) { throw new IllegalStateException("Cannot determine local address", e); } } };
@Override public int getPort() { int port = super.getPort(); ServerSocketChannel channel = this.serverChannel; if (port == 0 && channel != null) { try { SocketAddress address = channel.getLocalAddress(); if (address instanceof InetSocketAddress) { port = ((InetSocketAddress) address).getPort(); } } catch (IOException e) { } } return port; }
/** * {@inheritDoc} * * Forwards to {@link #delegate}. */ @Override public SocketAddress getLocalAddress() throws IOException { return delegate.getLocalAddress(); }
public InetAddress getLocalAddress() { try { InetSocketAddress inetAddress = (InetSocketAddress) _ss.getLocalAddress(); return inetAddress.getAddress(); } catch (Exception e) { throw new RuntimeException(e); } }
@VisibleForTesting static int port(ServerSocketChannel channel) { try { return ((InetSocketAddress) channel.getLocalAddress()).getPort(); } catch (IOException e) { throw new UncheckedIOException(e); } }
@VisibleForTesting static int port(ServerSocketChannel channel) { try { return ((InetSocketAddress) channel.getLocalAddress()).getPort(); } catch (IOException e) { throw new UncheckedIOException(e); } }
public int getPortNumber() throws IOException { ServerSocketChannel ssc = (ServerSocketChannel)sel.getChannel(); return ((InetSocketAddress)ssc.getLocalAddress()).getPort(); }
public ServerSocketRule() { try { serverSocketChannel = (ServerSocketChannel) ServerSocketChannel .open() .bind(new InetSocketAddress(0)) .configureBlocking(false); address = (InetSocketAddress) serverSocketChannel.getLocalAddress(); } catch (IOException e) { throw new RuntimeException(e); } }
private void doInitSocketTcp() throws IOException { // Create a new non-blocking server socket channel this.serverChannelTcp = ServerSocketChannel.open(); this.serverChannelTcp.configureBlocking(false); // Bind the server socket to the specified address and port InetSocketAddress isa = new InetSocketAddress(this.hostAddress, this.hostport); this.serverChannelTcp.bind(isa); if (logger.isInfoEnabled()) { logger.info(String.format("ServerSocketChannel bound to=%s ", serverChannelTcp.getLocalAddress())); } }
@Override public void applyx(IgniteInternalFuture<?> future) throws IgniteCheckedException { try { if (log != null && log.isInfoEnabled()) log.info("Server socket closed " + ch.getLocalAddress()); ch.close(); } catch (Exception ex) { U.error(log, "Fail close socket.", ex); throw new IgniteCheckedException(ex); } } });