Source Code : Managing asynchronous communication using the AsynchronousServerSocketChannel

Java Is Open Source Programming Language You Can Download From Java and Java Libraries From http://www.oracle.com. Click Here to download
We provide this code related to title for you to solve your developing problem easily. Libraries which is import in this program you can download from http://www.oracle.com. Click Here or search from google with Libraries Name you get jar file related it

Managing asynchronous communication using the AsynchronousServerSocketChannel


import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousSocketChannel;
import java.util.Scanner;
import java.util.concurrent.Future;

public class Test {

  public static void main(String[] args) throws Exception {

    AsynchronousSocketChannel client = AsynchronousSocketChannel.open();
    InetSocketAddress address = new InetSocketAddress("localhost", 5000);

    Future<Void> future = client.connect(address);
    System.out.println("Client: Waiting for the connection to complete");
    future.get();

    String message = "";
    while (!message.equals("quit")) {
      System.out.print("Enter a message: ");
      Scanner scanner = new Scanner(System.in);
      message = scanner.nextLine();
      System.out.println("Client: Sending ...");
      ByteBuffer buffer = ByteBuffer.wrap(message.getBytes());
      System.out.println("Client: Message sent: " + new String(buffer.array()));
      client.write(buffer);
    }
  }

}

 

Thank with us