Java AsynchronousSocketChannel create asynchronous client socket channel

Description

Java AsynchronousSocketChannel create asynchronous client socket channel

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousSocketChannel;
import java.nio.channels.CompletionHandler;
import java.nio.charset.Charset;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

class Attachment {
  AsynchronousSocketChannel channel;
  ByteBuffer buffer;//from w w w .j  a  v a2s .  c  o  m
  Thread mainThread;
  boolean isRead;
}

class ReadWriteHandler implements CompletionHandler<Integer, Attachment> {
  @Override
  public void completed(Integer result, Attachment attach) {
    if (attach.isRead) {
      attach.buffer.flip();

      // Get the text read from the server
      Charset cs = Charset.forName("UTF-8");

      int limits = attach.buffer.limit();
      byte bytes[] = new byte[limits];
      attach.buffer.get(bytes, 0, limits);
      String msg = new String(bytes, cs);

      System.out.println("Server Responded:"+ msg);

      msg = this.getTextFromUser();
      if (msg.equalsIgnoreCase("bye")) {
        attach.mainThread.interrupt();
        return;
      }
      attach.buffer.clear();
      byte[] data = msg.getBytes(cs);
      attach.buffer.put(data);

      attach.buffer.flip();

      attach.isRead = false;

      attach.channel.write(attach.buffer, attach, this);
    } else {
      attach.isRead = true;

      attach.buffer.clear();

      attach.channel.read(attach.buffer, attach, this);
    }
  }

  @Override
  public void failed(Throwable e, Attachment attach) {
    e.printStackTrace();
  }

  private String getTextFromUser() {
    System.out.print("Please enter a message (Bye to quit):");
    String msg = null;

    BufferedReader consoleReader = new BufferedReader(new InputStreamReader(System.in));
    try {
      msg = consoleReader.readLine();
    } catch (IOException e) {
      e.printStackTrace();
    }

    return msg;
  }
}

public class Main {
  public static void main(String[] args) {
    try (AsynchronousSocketChannel channel = AsynchronousSocketChannel.open()) {
      String serverName = "localhost";
      int serverPort = 8989;
      SocketAddress serverAddr = new InetSocketAddress(serverName, serverPort);

      Future<Void> result = channel.connect(serverAddr);
      System.out.println("Connecting to the server...");

      result.get();

      System.out.println("Connected to the server...");

      Attachment attach = new Attachment();
      attach.channel = channel;
      attach.buffer = ByteBuffer.allocate(2048);
      attach.isRead = false;
      attach.mainThread = Thread.currentThread();

      // Place the "Hello" message in the buffer
      Charset cs = Charset.forName("UTF-8");
      String msg = "Hello";
      byte[] data = msg.getBytes(cs);
      attach.buffer.put(data);
      attach.buffer.flip();

      // Write to the server
      ReadWriteHandler readWriteHandler = new ReadWriteHandler();
      channel.write(attach.buffer, attach, readWriteHandler);

      // Let this thread wait for ever on its own death until interrupted
      attach.mainThread.join();
    } catch (ExecutionException | IOException e) {
      e.printStackTrace();
    } catch (InterruptedException e) {
      System.out.println("Disconnected from the server.");
    }
  }
}



PreviousNext

Related