@Override public Socket createSocket(String host, int port) throws IOException { return SocketChannel.open(new InetSocketAddress(host, port)).socket(); }
private void configureSocketChannel(SocketChannel socketChannel, int sendBufferSize, int receiveBufferSize) throws IOException { socketChannel.configureBlocking(false); Socket socket = socketChannel.socket(); socket.setKeepAlive(true); if (sendBufferSize != Selectable.USE_DEFAULT_BUFFER_SIZE) socket.setSendBufferSize(sendBufferSize); if (receiveBufferSize != Selectable.USE_DEFAULT_BUFFER_SIZE) socket.setReceiveBufferSize(receiveBufferSize); socket.setTcpNoDelay(true); }
private void onChannelAcceptable(SelectionKey key) throws IOException { ServerSocketChannel serverSocketChannel = (ServerSocketChannel) key.channel(); SocketChannel socketChannel = serverSocketChannel.accept(); socketChannel.configureBlocking(false); SelectionKey readKey = socketChannel.register(selector, SelectionKey.OP_READ); readKey.attach(key.attachment()); }
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"); selector.select(); Set<SelectionKey> keys=selector.selectedKeys(); for(Iterator<SelectionKey> it=keys.iterator(); it.hasNext();) { SelectionKey key=it.next(); if(!key.isValid()) { it.remove(); continue; if(key.isAcceptable()) { SocketChannel client_ch=ch.accept(); if(client_ch != null) { // accept() may return null... System.out.printf("accepted connection from %s\n", client_ch.getRemoteAddress()); client_ch.configureBlocking(false); client_ch.register(selector, SelectionKey.OP_READ, create(SIZE, direct)); else if(key.isReadable()) { if(!handle((SocketChannel)key.channel(), (ByteBuffer)key.attachment())) { key.cancel();
select.select(5000); Set<SelectionKey> keys = select.selectedKeys(); for (SelectionKey k : keys) { if (k.isAcceptable() && k.channel() == socks) { SocketChannel csock = socks.accept(); continue; addClient(csock); csock.register(select, SelectionKey.OP_READ); } else if (k.isReadable()) { cl.client.close(); if (cl.remote != null) cl.remote.close(); k.cancel(); clients.remove(cl); SocksClient cl = clients.get(i); if ((System.currentTimeMillis() - cl.lastData) > 30000L) { cl.client.close(); if (cl.remote != null) cl.remote.close(); clients.remove(cl);
@Override public void onConnectionEstablished(SocketChannel socketChannel) throws IOException { this.myNext = new HandshakeReceiver( myContext, socketChannel.socket().getInetAddress().getHostAddress(), socketChannel.socket().getPort(), false); }
@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()); }
public static boolean connect(SocketChannel ch, SocketAddress dest) throws IOException { if(dest instanceof InetSocketAddress) { InetAddress addr=((InetSocketAddress)dest).getAddress(); if(addr instanceof Inet6Address) { Inet6Address tmp=(Inet6Address)addr; if(tmp.getScopeId() != 0) { dest=new InetSocketAddress(InetAddress.getByAddress(tmp.getAddress()),((InetSocketAddress)dest).getPort()); } } } return ch.connect(dest); }
System.out.println("Sender Start"); ServerSocketChannel ssChannel = ServerSocketChannel.open(); ssChannel.configureBlocking(true); int port = 12345; ssChannel.socket().bind(new InetSocketAddress(port)); SocketChannel sChannel = ssChannel.accept(); ObjectOutputStream(sChannel.socket().getOutputStream()); oos.writeObject(obj); oos.close();
@Override public Socket createSocket(String address, int port, InetAddress localAddresss, int localPort) throws IOException, UnknownHostException { SocketChannel channel = SocketChannel.open(); channel.socket().bind(new InetSocketAddress(localAddresss, localPort)); channel.connect(new InetSocketAddress(address, port)); return channel.socket(); }
public NIOServerCnxn(ZooKeeperServer zk, SocketChannel sock, SelectionKey sk, NIOServerCnxnFactory factory, SelectorThread selectorThread) throws IOException { super(zk); this.sock = sock; this.sk = sk; this.factory = factory; this.selectorThread = selectorThread; if (this.factory.login != null) { this.zooKeeperSaslServer = new ZooKeeperSaslServer(factory.login); } sock.socket().setTcpNoDelay(true); /* set socket linger to false, so that socket close does not block */ sock.socket().setSoLinger(false, -1); InetAddress addr = ((InetSocketAddress) sock.socket() .getRemoteSocketAddress()).getAddress(); addAuthInfo(new Id("ip", addr.getHostAddress())); this.sessionTimeout = factory.sessionlessCnxnTimeout; }
public Socket createSocket(String type, InetSocketAddress inetSocketAddress) throws IOException { SocketChannel socketChannel = null; Socket socket = null; if (orb.getORBData().connectionSocketType().equals(ORBConstants.SOCKETCHANNEL)) { socketChannel = SocketChannel.open(inetSocketAddress); socket = socketChannel.socket(); } else { socket = new Socket(inetSocketAddress.getHostName(), inetSocketAddress.getPort()); } // Disable Nagle's algorithm (i.e., always send immediately). socket.setTcpNoDelay(true); return socket; }
public StandardCommsSession(final String hostname, final int port, final int timeoutMillis) throws IOException { socketChannel = SocketChannel.open(); socketChannel.socket().connect(new InetSocketAddress(hostname, port), timeoutMillis); socketChannel.configureBlocking(false); in = new SocketChannelInputStream(socketChannel); bufferedIn = new InterruptableInputStream(new BufferedInputStream(in)); out = new SocketChannelOutputStream(socketChannel); bufferedOut = new InterruptableOutputStream(new BufferedOutputStream(out)); this.hostname = hostname; this.port = port; }
public SocketAddress getPeerAddress() { final Socket socket = conduit.getSocketChannel().socket(); return new InetSocketAddress(socket.getInetAddress(), socket.getPort()); }
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(); } }
public SocketAddress getLocalAddress() { final Socket socket = conduit.getSocketChannel().socket(); return new InetSocketAddress(socket.getLocalAddress(), socket.getLocalPort()); }
void doAccept(SelectionKey key) throws InterruptedException, IOException, OutOfMemoryError { ServerSocketChannel server = (ServerSocketChannel) key.channel(); SocketChannel channel; while ((channel = server.accept()) != null) { channel.configureBlocking(false); channel.socket().setTcpNoDelay(tcpNoDelay); channel.socket().setKeepAlive(tcpKeepAlive); Reader reader = getReader(); SimpleServerRpcConnection c = connectionManager.register(channel); // If the connectionManager can't take it, close the connection. if (c == null) { if (channel.isOpen()) { IOUtils.cleanup(null, channel); } continue; } key.attach(c); // so closeCurrentConnection can get the object reader.addConnection(c); } }
protected TransportLayer buildTransportLayer(String id, SelectionKey key, SocketChannel socketChannel) throws IOException { if (this.securityProtocol == SecurityProtocol.SASL_SSL) { return SslTransportLayer.create(id, key, sslFactory.createSslEngine(socketChannel.socket().getInetAddress().getHostName(), socketChannel.socket().getPort())); } else { return new PlaintextTransportLayer(key); } }