Package java.nio.channels

Examples of java.nio.channels.SocketChannel


        while (!closed) {
            try {
                readBuffer.clear();
                readBuffer.limit(0);

                SocketChannel sc = SocketChannel.open(address);
                sc.socket().setReceiveBufferSize(256 * 1024);
                logger.info("Connected to " + address);
                ByteBuffer bb = ByteBuffer.allocate(8);
                bb.putLong(0, chronicle.size());
                IOTools.writeAllOrEOF(sc, bb);
                return sc;
View Full Code Here


        @Override
        public void run() {
            Thread.currentThread().setName(name + "-acceptor");
            try {
                while (!closed) {
                    SocketChannel socket = server.accept();
                    service.execute(new Handler(socket));
                }
            } catch (IOException e) {
                if (!closed)
                    logger.log(Level.SEVERE, "Acceptor dying", e);
View Full Code Here

        @Override
        public void run() {
            Thread.currentThread().setName(name + "-acceptor");
            try {
                while (!closed) {
                    SocketChannel socket = server.accept();
                    service.execute(new Handler(socket));
                }
            } catch (IOException e) {
                if (!closed)
                    logger.log(Level.SEVERE, "Acceptor dying", e);
View Full Code Here

        public void run() {
            try {
                ByteBuffer bb = ByteBuffer.allocateDirect(64 * 1024);
                System.err.println("Accepting connect on " + ssc.socket().getLocalPort());
                while (!Thread.interrupted()) {
                    SocketChannel sc = ssc.accept();
                    if (BUSY)
                        sc.configureBlocking(false);

                    String ra = sc.socket().getInetAddress().toString();
                    System.err.println("... connect to " + ra);
                    try {
                        sc.socket().setTcpNoDelay(true);
                        while (sc.read(bb) >= 0) {
                            bb.flip();
//                            System.out.println("e "+bb.remaining());
                            IOTools.writeAllOrEOF(sc, bb);
                            bb.clear();
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    } finally {
                        sc.close();
                    }
                }
            } catch (ClosedByInterruptException ignored) {
            } catch (IOException e) {
                e.printStackTrace();
View Full Code Here

        @SuppressWarnings("unchecked")
        final Excerpt excerpt = chronicle.createExcerpt();

        @Override
        public void run() {
            SocketChannel sc = null;
            try {
                while (!closed) {
                    if (sc == null || !sc.isOpen())
                        sc = createConnection();
                    else
                        readNextExcerpt(sc);
                }
            } finally {
View Full Code Here

            if (closed) {
                return null;
            }
            do {
                try {
                    SocketChannel sc = SocketChannel.open(address);
                    ByteBuffer bb = ByteBuffer.allocate(8);
                    bb.putLong(0, chronicle.size());
                    IOTools.writeAllOrEOF(sc, bb);
                    return sc;
View Full Code Here

 
  /**
   * construct a NIOConnection from a selection key
   */
  PacketConnection(SelectionKey sk) {
    SocketChannel sch = (SocketChannel) sk.channel();
    if (sch.isConnected()) // connected immediatedly if local on *nix
    {
      sk.interestOps(SelectionKey.OP_READ);
      state = Connection.OPEN;
    } else if (sch.isConnectionPending()) {
      sk.interestOps(SelectionKey.OP_CONNECT);
      state = Connection.OPENING;
    }
    this.sk = sk; // link this to the key
    sk.attach(this);
View Full Code Here

  /**
   * process a connect complete selection
   */
  public void doConnect() {
    SocketChannel sc = (SocketChannel) sk.channel();
    try {
      sc.finishConnect();
      sk.interestOps(sk.interestOps() & ~SelectionKey.OP_CONNECT);
      log.fine("connect complete");
      state = Connection.OPEN;
    } catch (IOException e) {
      e.printStackTrace();
View Full Code Here

  /**
   * process a read ready selection
   */
  public void doRead() {
    SocketChannel sc = (SocketChannel) sk.channel();
    if (sc.isOpen()) {

      int len;
      recvBuffer.clear();
      try {
        len = sc.read(recvBuffer);
      } catch (IOException e) {
        //e.printStackTrace();
        len = -1;
      } // error look like eof
      log.finer("read len=" + len);
View Full Code Here

  /*
   * close the connection and its socket
   */
  public void close() {
    if (state != Connection.CLOSED) {
      SocketChannel sc = (SocketChannel) sk.channel();
      if (sc.isOpen()) {
        if (state == Connection.OPEN) // open attempt graceful
        // shutdown
        {
          log.fine("shutting down");
          state = Connection.CLOSING;
          Socket sock = sc.socket();
          try {
            sock.shutdownOutput();
          } catch (IOException se) {
            log.severe("shutdown failed: " + se.getMessage());
            log.log(Level.FINE, "details: ", se);
View Full Code Here

TOP

Related Classes of java.nio.channels.SocketChannel

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.