Package java.nio.channels

Examples of java.nio.channels.SocketChannel


        // Now try each address
        try {
            SocketException lastFailure = null;
            for (InetAddress address : loopBackAddresses) {
                LOGGER.debug("Trying to connect to address {}.", address);
                SocketChannel socketChannel;
                try {
                    socketChannel = SocketChannel.open(new InetSocketAddress(address, destinationUri.getPort()));
                } catch (SocketException e) {
                    LOGGER.debug("Cannot connect to address {}, skipping.", address);
                    lastFailure = e;
                    continue;
                }
                LOGGER.debug("Connected to address {}.", address);
                URI localAddress = new URI(String.format("tcp://localhost:%d", socketChannel.socket().getLocalPort()));
                return new SocketConnection<T>(socketChannel, localAddress, destinationUri, classLoader);
            }
            throw lastFailure;
        } catch (java.net.ConnectException e) {
            throw new ConnectException(String.format("Could not connect to server %s. Tried addresses: %s.",
View Full Code Here


        if(!file.canRead()) {
            throw new IllegalArgumentException(file.getAbsolutePath() + " cannot read");
        }

        final SocketAddress dstSockAddr = new InetSocketAddress(dstAddr, dstPort);
        SocketChannel channel = null;
        Socket socket = null;
        final OutputStream out;
        try {
            channel = SocketChannel.open();
            socket = channel.socket();
            socket.connect(dstSockAddr);
            out = socket.getOutputStream();
        } catch (IOException e) {
            LOG.error("failed to connect: " + dstSockAddr, e);
            IOUtils.closeQuietly(channel);
View Full Code Here

    }

    public static void send(@Nonnull final FastByteArrayOutputStream data, @Nonnull final String fileName, @Nullable final String writeDirPath, @Nonnull final InetAddress dstAddr, final int dstPort, final boolean append, final boolean sync, @Nullable final TransferClientHandler handler)
            throws IOException {
        final SocketAddress dstSockAddr = new InetSocketAddress(dstAddr, dstPort);
        SocketChannel channel = null;
        Socket socket = null;
        final OutputStream out;
        try {
            channel = SocketChannel.open();
            socket = channel.socket();
            socket.connect(dstSockAddr);
            out = socket.getOutputStream();
        } catch (IOException e) {
            LOG.error("failed to connect: " + dstSockAddr, e);
            IOUtils.closeQuietly(channel);
View Full Code Here

    long  failed_accepts    = 0;

    while(true){
     
      try{       
        SocketChannel socket_channel = ssc.accept();
           
        successfull_accepts++;
       
        if ( !( allow_external_access || socket_channel.socket().getInetAddress().isLoopbackAddress())){
         
          if (Logger.isEnabled())
            Logger.log(new LogEvent(LOGID, LogEvent.LT_WARNING,
                "AEProxy: incoming connection from '"
                    + socket_channel.socket().getInetAddress()
                    + "' - closed as not local"));
       
          socket_channel.close();
         
        }else{
           
          socket_channel.configureBlocking(false);
 
          AEProxyConnectionImpl processor = new AEProxyConnectionImpl(this, socket_channel, proxy_handler);
         
          if ( !processor.isClosed()){
           
View Full Code Here

  void connect() {
    connecting_thread = new JMThread() {
      public void run() {     
        try {
          connection_status = ConnectionStatus.CONNECTING;
          SocketChannel channel = SocketChannel.open();
         
          jm_socket_channel = new JMuleSocketChannel(channel);
         
          jm_socket_channel.connect(remote_inet_socket_address, ConfigurationManager.SERVER_CONNECTING_TIMEOUT);
         
View Full Code Here

        Iterator i = selectedKeys.iterator();
        while (i.hasNext()) {
            SelectionKey key = (SelectionKey) i.next();
            if (key.isAcceptable()) {
                ServerSocketChannel ssc = (ServerSocketChannel) key.channel();
                SocketChannel sc = ssc.accept();
                sc.configureBlocking(false);
                sc.register(this.selector, SelectionKey.OP_READ);
            } else if (key.isReadable()) {
                SocketChannel sc = (SocketChannel) key.channel();
                ByteBuffer buffer = ByteBuffer.allocate(10);
                buffer.clear();
                sc.read(buffer);
                buffer.flip();
                sc.write(buffer);
                sc.close();
            }
            i.remove();
        }
    }
View Full Code Here

    // For an accept to be pending the channel must be a server socket
    // channel.
    ServerSocketChannel serverSocketChannel = (ServerSocketChannel) key.channel();

    // Accept the connection and make it non-blocking
    SocketChannel socketChannel = serverSocketChannel.accept();
    socketChannel.configureBlocking(false);

    // Register the new SocketChannel with our Selector, indicating
    // we'd like to be notified when there's data waiting to be read
    socketChannel.register(this.selector, SelectionKey.OP_READ);

    worker.onConnect(socketChannel);
  }
View Full Code Here

    worker.onConnect(socketChannel);
  }

  private void read(SelectionKey key) {
    SocketChannel socketChannel = (SocketChannel) key.channel();

    // Clear out our read buffer so it's ready for new data
    this.readBuffer.clear();

    // Attempt to read off the channel
    int numRead;
    try {
      numRead = socketChannel.read(this.readBuffer);
    } catch (IOException e) {
      // The remote forcibly closed the connection, cancel
      // the selection key and close the channel.
      logger.debug("Remote closed connnection", e);
      key.cancel();
View Full Code Here

    // Hand the data off to our worker thread
    this.worker.onData(this, socketChannel, this.readBuffer.array(), numRead);
  }

  private void write(SelectionKey key) {
    SocketChannel socketChannel = (SocketChannel) key.channel();

    synchronized (this.pendingData) {
      List<ByteBuffer> queue = this.pendingData.get(socketChannel);

      try {
        // Write until there's not more data ...
        while (!queue.isEmpty()) {
          ByteBuffer buf = queue.get(0);
          socketChannel.write(buf);

          if (buf.remaining() > 0) {
            // ... or the socket's buffer fills up
            break;
          }
View Full Code Here

   * @throws IOException in case of an I/O error
   * @throws InvalidVersionException if the version of Marauroa used to create
   *         the dump and the one used to parse it are incompatible
   */
  public void dump(InputStream is, boolean dumpRawData) throws IOException, InvalidVersionException {
    SocketChannel channel = new FakeSocketChannel(InetAddress.getByName("localhost"), 32123);

    while (true) {
      //read a packet from the opened file
      byte[] data = new byte[100];
      int cnt = is.read(data);
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.