Java 8 SocketChannel - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for Java 8 SocketChannel class.

This code example shows how to use the following methods: socket, configureBlocking
    public SocketConnection(SocketChannel socket, MessageSerializer<T> serializer) {
        this.socket = socket;
        try { 
            // NOTE: we use non-blocking IO as there is no reliable way when using blocking IO to shutdown reads while 
            // keeping writes active. For example, Socket.shutdownInput() does not work on Windows. 
            socket.configureBlocking(false);
            outstr = new SocketOutputStream(socket);
            instr = new SocketInputStream(socket);
        } catch (IOException e) {
            throw UncheckedException.throwAsUncheckedException(e);
        } 
        InetSocketAddress localSocketAddress = (InetSocketAddress) socket.socket().getLocalSocketAddress();
        localAddress = new SocketInetAddress(localSocketAddress.getAddress(), localSocketAddress.getPort());
        InetSocketAddress remoteSocketAddress = (InetSocketAddress) socket.socket().getRemoteSocketAddress();
        remoteAddress = new SocketInetAddress(remoteSocketAddress.getAddress(), remoteSocketAddress.getPort());
        objectReader = serializer.newReader(instr, localAddress, remoteAddress);
        objectWriter = serializer.newWriter(outstr);
    } 
 
    @Override 
Full Snippet Info
8
CodeRank
Contextual code suggestions in your IDE  Get Codota for Java
This code example shows how to use the following methods: connect, finishConnect, isConnected, isConnectionPending, close
        this.channel1.close();
        assertFalse(this.channel1.isOpen());
    } 
 
    public void testIsConnected() throws Exception {
        assertFalse(this.channel1.isConnected());// not connected
        this.channel1.configureBlocking(false);
        assertFalse(this.channel1.connect(localAddr1));
        assertFalse(this.channel1.isConnected());
        assertTrue(this.channel1.isConnectionPending());
        assertTrue(tryFinish());
        assertTrue(this.channel1.isConnected());
        this.channel1.close();
        assertFalse(this.channel1.isConnected());
    } 
 
    public void testIsConnectionPending() throws Exception {
        // ensure 
        ensureServerClosed(); 
        this.channel1.configureBlocking(false);

7
CodeRank
Contextual code suggestions in your IDE  Get Codota for Java
This code example shows how to use the following methods: configureBlocking
	} 
 
	public void connect() { 
		try { 
			channel = SocketChannel.open(new InetSocketAddress(host, port));
			channel.configureBlocking(false);
		} catch (IOException e) { e.printStackTrace(); }
		socket = new AsynchronousSocket(channel);
		IOLoop.INSTANCE.addHandler(channel, socket, SelectionKey.OP_READ, ByteBuffer.allocate(1024));
	} 
	 
	public void get(String value, AsyncResult<String> cb) {
		socket.write("GET deft\r\n", new WriteCallback(cb));
	} 
	 
	private class WriteCallback implements AsyncCallback { 
 
		private final AsyncResult<String> cb;
		 
		public WriteCallback(AsyncResult<String> cb) { 
Full Snippet Info
6
CodeRank
Contextual code suggestions in your IDE  Get Codota for Java
This code example shows how to use the following methods: connect, socket, configureBlocking, AbstractSelectableChannel.register
        return false; 
    } 
 
    private void configureSocket() throws IOException {
        if (socketChannel!=null) {
            socketChannel.configureBlocking(false);
            socketChannel.socket().setSendBufferSize(getTxBufSize());
            socketChannel.socket().setReceiveBufferSize(getRxBufSize());
            socketChannel.socket().setSoTimeout((int)getTimeout());
            socketChannel.socket().setSoLinger(getSoLingerOn(),getSoLingerOn()?getSoLingerTime():0);
            socketChannel.socket().setTcpNoDelay(getTcpNoDelay());
            socketChannel.socket().setKeepAlive(getSoKeepAlive());
            socketChannel.socket().setReuseAddress(getSoReuseAddress());
            socketChannel.socket().setOOBInline(getOoBInline());
            socketChannel.socket().setSoLinger(getSoLingerOn(),getSoLingerTime());
            socketChannel.socket().setTrafficClass(getSoTrafficClass());
        } else if (dataChannel!=null) {
            dataChannel.configureBlocking(false);
            dataChannel.socket().setSendBufferSize(getUdpTxBufSize());
            dataChannel.socket().setReceiveBufferSize(getUdpRxBufSize());

6
CodeRank
Contextual code suggestions in your IDE  Get Codota for Java
This code example shows how to use the following methods: toString, socket, isOpen
	} 
	 
	@Override 
	public String toString() {
		 
		return "InetClientSocket[" + ch.toString()  
			+ " open="+ch.isOpen()
			+ " addr="+ch.socket().getLocalSocketAddress()
			+ " remote="+ch.socket().getRemoteSocketAddress()
		    + "]";		 
	} 
} 
Full Snippet Info
7
CodeRank
Contextual code suggestions in your IDE  Get Codota for Java
This code example shows how to use the following methods: close
    } 
 
    public void close() { 
        closed = true;
        try { 
            if (sc != null) sc.close();
        } catch (IOException ignored) {
        } 
    } 
 
    public boolean isClosed() { 
        return closed;
    } 
 
    public Excerpt excerpt() { 
        return excerpt;
    } 
} 
Full Snippet Info
6
CodeRank
Contextual code suggestions in your IDE  Get Codota for Java
This code example shows how to use the following methods: write
		if (key.attachment() instanceof MappedByteBuffer) {
			writeMappedByteBuffer(key, channel);
		} else if (key.attachment() instanceof DynamicByteBuffer) {
			writeDynamicByteBuffer(key, channel);
		} 
		if (ioLoop.hasKeepAliveTimeout(channel)) {
			prolongKeepAliveTimeout(channel);
		} 
 
	} 
 
	private void writeMappedByteBuffer(SelectionKey key, SocketChannel channel) {
		MappedByteBuffer mbb = (MappedByteBuffer) key.attachment();
		if (mbb.hasRemaining()) {
			try { 
				channel.write(mbb);
			} catch (IOException e) {
				logger.error("Failed to send data to client: {}", e.getMessage());
				Closeables.closeQuietly(channel);
			} 

6
CodeRank
Contextual code suggestions in your IDE  Get Codota for Java
This code example shows how to use the following methods: isConnected, isOpen
  /** 
   * Checks whether the socket is connected. 
   */ 
  public boolean isOpen() { 
    // isConnected() does not return false after close(), but isOpen() does 
    return socketChannel_.isOpen() && socketChannel_.isConnected();
  } 
 
  /** 
   * Do not call, the implementation provides its own lazy non-blocking connect. 
   */ 
  public void open() throws TTransportException { 
    throw new RuntimeException("open() is not implemented for TNonblockingSocket");
  } 
 
  /** 
   * Perform a nonblocking read into buffer. 
   */ 
  public int read(ByteBuffer buffer) throws IOException {
    return socketChannel_.read(buffer);
Full Snippet Info
6
CodeRank
Contextual code suggestions in your IDE  Get Codota for Java
This code example shows how to use the following methods: isOpen
        return socketChannel.configureBlocking(b);
    } 
 
    @Override 
    public boolean isOpen() { 
        return socketChannel.isOpen();
    } 
 
    @Override 
    public void closeInbound() throws IOException {
    } 
 
    @Override 
    public void closeOutbound() throws IOException {
    } 
 
    @Override 
    public void close() throws IOException {
        socketChannel.close();
    } 
Full Snippet Info
6
CodeRank
Contextual code suggestions in your IDE  Get Codota for Java
This code example shows how to use the following methods: socket, close, configureBlocking
	protected void doAccept(final Selector selector, ServerSocketChannel server, long now) throws IOException {
		logger.debug("New accept"); 
		SocketChannel channel = server.accept();
		if (this.isShuttingDown()) {
			if (logger.isInfoEnabled()) { 
				logger.info("New connection from " + channel.socket().getInetAddress().getHostAddress()
						+ " rejected; the server is in the process of shutting down."); 
			} 
			channel.close();
		} 
		else { 
			try { 
				channel.configureBlocking(false);
				Socket socket = channel.socket();
				setSocketAttributes(socket);
				TcpNioConnection connection = createTcpNioConnection(channel);
				if (connection == null) {
					return; 
				} 
				connection.setTaskExecutor(this.getTaskExecutor());

6
CodeRank
Contextual code suggestions in your IDE  Get Codota for Java

Related Java 8 SocketChannel Questions & Answers:

See Code Examples for Java 8 SocketChannel Methods: