List of usage examples for java.nio.channels SocketChannel socket
public abstract Socket socket();
From source file:client.MultiplexingClient.java
/** * A new client connected. Creates a PacketChannel to handle it. *//*from ww w . j a v a 2 s. com*/ public void connectionEstablished(Connector connector, SocketChannel sc) { try { // We should reduce the size of the TCP buffers or else we will // easily run out of memory when accepting several thousands of // connctions sc.socket().setReceiveBufferSize(2 * 1024); sc.socket().setSendBufferSize(2 * 1024); // The contructor enables reading automatically. this.pc = new PacketChannel(sc, channelFactory, st, new SimpleProtocolDecoder(), this); // Do not start sending packets right away. Waits for all sockets // to connect. Otherwise, the load created by sending and receiving // packets will increase dramatically the time taken for all // connections to be established. It is better to establish all // connections and only then to start sending packets. connectedClients.add(this); connectionsEstablished++; System.out.println("[" + connector + "] Connected: " + sc.socket().getInetAddress() + " (" + connectionsEstablished + "/" + connectionCount + ")"); // If if all connections are established. checkAllConnected(); } catch (Exception e) { e.printStackTrace(); } }
From source file:com.bittorrent.mpetazzoni.client.ConnectionHandler.java
/** * Return a human-readable representation of a connected socket channel. * * @param channel The socket channel to represent. * @return A textual representation (<em>host:port</em>) of the given * socket.// w w w . j a v a 2 s. c o m */ private String socketRepr(SocketChannel channel) { Socket s = channel.socket(); return String.format("%s:%d%s", s.getInetAddress().getHostName(), s.getPort(), channel.isConnected() ? "+" : "-"); }
From source file:com.tera.common.network.nio.MMOConnection.java
protected MMOConnection(PacketService packetService, SelectorThread selectorThread, SocketChannel socketChannel) throws ClosedChannelException { super(packetService); this.selectorThread = selectorThread; this.readWriteThread = getSelectorThread().getReadWriteThread(); this.socket = socketChannel.socket(); this.inetAddress = socket.getInetAddress(); this.hostAddress = inetAddress.getHostAddress(); this.selectionKey = socketChannel.register(getReadWriteThread().getSelector(), SelectionKey.OP_READ); this.selectionKey.attach(this); setChannelState(ChannelState.CONNECTED); connectionId = idFactory.nextId();//TODO deallocate after disconnect }
From source file:org.apache.pig.shock.SSHSocketImplFactory.java
public Socket createSocket(String host, int port) throws IOException, UnknownHostException { String socksHost = System.getProperty("socksProxyHost"); Socket s;// w ww.j a v a 2s . com InetSocketAddress addr = new InetSocketAddress(host, port); if (socksHost != null) { Proxy proxy = new Proxy(Type.SOCKS, new InetSocketAddress(socksHost, 1080)); s = new Socket(proxy); s.connect(addr); } else { log.error(addr); SocketChannel sc = SocketChannel.open(addr); s = sc.socket(); } s.setTcpNoDelay(true); return s; }
From source file:org.reunionemu.jreunion.server.Network.java
public void disconnect(SocketChannel socketChannel) { if (socketChannel.isConnected() && socketChannel.isOpen()) { processOutput(socketChannel);/*from w w w.j av a 2 s .c o m*/ } LoggerFactory.getLogger(Network.class) .info("Disconnecting {local=" + socketChannel.socket().getLocalSocketAddress() + " remote=" + socketChannel.socket().getRemoteSocketAddress() + "}\n"); fireEvent(NetworkDisconnectEvent.class, socketChannel); try { socketChannel.close(); } catch (IOException e) { LoggerFactory.getLogger(this.getClass()).warn("Exception", e); } }
From source file:net.timewalker.ffmq4.transport.tcp.nio.NIOTcpPacketTransport.java
/** * Connect the transport to its remote endpoint */// w w w .ja va 2 s . c om private SocketChannel connect(URI transportURI) throws PacketTransportException { String host = transportURI.getHost(); int port = transportURI.getPort(); try { SocketChannel socketChannel = SocketChannel.open(); socketChannel.configureBlocking(false); SocketUtils.setupSocket(socketChannel.socket(), socketSendBufferSize, socketRecvBufferSize); log.debug("#" + id + " opening a TCP connection to " + host + ":" + port); socketChannel.connect(new InetSocketAddress(host, port)); return socketChannel; } catch (Exception e) { log.error("#" + id + " could not connect to " + host + ":" + port, e); throw new PacketTransportException("Could not connect to " + host + ":" + port + " : " + e.toString()); } }
From source file:jp.queuelinker.system.net.SelectorThread.java
public int connectChannel(final SocketAddress remoteAddress, final SelectorCallBack callBack) throws IOException { SocketChannel socketChannel = SocketChannel.open(remoteAddress); socketChannel.configureBlocking(false); // socketChannel.socket().setTcpNoDelay(true); socketChannel.socket().setReuseAddress(true); return addSocketChannel(socketChannel, callBack); }
From source file:com.openteach.diamond.network.waverider.network.DefaultNetWorkServer.java
/** * ?, ?Session, ?Session, //from ww w . j a v a 2s.c om * Session? * @param key * @throws IOException */ private void onAccept(SelectionKey key) throws IOException { SocketChannel channel = ((ServerSocketChannel) key.channel()).accept(); opsChangeRequstMap.put(channel, new SocketChannelOPSChangeRequest(channel, 0)); if (logger.isWarnEnabled()) logger.warn("Accept client from : " + channel.socket().getRemoteSocketAddress()); channel.configureBlocking(false); Session session = sessionManager.newSession(this, channel, false); channel.register(selector, SelectionKey.OP_READ, session); session.start(); }
From source file:com.navjagpal.fileshare.WebServer.java
public void runWebServer() { while (true) { Log.i(TAG, "Running main webserver thread"); try {/*from w w w . j a v a2 s . c o m*/ SocketChannel channel = mServerSocketChannel.accept(); final Socket socket = channel.socket(); Log.d(TAG, "Socket accepted"); Thread t = new Thread() { @Override public void run() { handleRequest(socket); } }; t.start(); } catch (ClosedByInterruptException e) { Log.i(TAG, "Received interrupt to shutdown."); return; } catch (IOException e) { Log.e(TAG, "Unexpected error, shutting down. " + e.toString()); return; } } }
From source file:ee.ria.xroad.proxy.clientproxy.FastestSocketSelector.java
private SocketInfo initConnections(Selector selector) throws IOException { log.trace("initConnections()"); for (URI target : addresses) { SocketChannel channel = SocketChannel.open(); channel.setOption(StandardSocketOptions.TCP_NODELAY, true); channel.configureBlocking(false); try {/*from w w w. j a v a 2 s. c o m*/ channel.register(selector, SelectionKey.OP_CONNECT, target); if (channel.connect(new InetSocketAddress(target.getHost(), target.getPort()))) { // connected immediately channel.configureBlocking(true); return new SocketInfo(target, channel.socket()); } } catch (Exception e) { closeQuietly(channel); log.trace("Error connecting to '{}': {}", target, e); } } return null; }