numRead = channel.read(buf);
public final boolean rxready() { try { ByteBuffer buf = ByteBuffer.allocate(1024); int n = channel.read(buf); UnixSocketAddress remote = channel.getRemoteSocketAddress(); System.out.printf("Read in %d bytes from %s\n", n, remote); if (n > 0) { buf.flip(); channel.write(buf); return true; } else if (n < 0) { return false; } } catch (IOException ex) { ex.printStackTrace(); return false; } return true; } }
public void process(SelectionKey key) { try { ByteBuffer buf = ByteBuffer.allocate(1024); int result = sessionChannel.read(buf); if (result > 0) { buf.flip(); messageReceived(new Buffer(buf.array(), buf.position(), buf.remaining())); return; } if (result == -1) { // EOF => remote closed the connection, cancel the selection key and close the channel. key.cancel(); sessionChannel.close(); } } catch (IOException e) { LOGGER.log(Level.INFO, "Could not write response to socket", e); key.cancel(); safelyClose(sessionChannel); } }