Advertisement
Guest User

Untitled

a guest
Feb 26th, 2020
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.10 KB | None | 0 0
  1. import java.io.IOException;
  2. import java.net.InetSocketAddress;
  3. import java.nio.ByteBuffer;
  4. import java.nio.channels.AsynchronousServerSocketChannel;
  5. import java.nio.channels.AsynchronousSocketChannel;
  6. import java.util.concurrent.ExecutionException;
  7. import java.util.concurrent.Future;
  8. import java.util.concurrent.TimeUnit;
  9. import java.util.concurrent.TimeoutException;
  10.  
  11. public class Server {
  12.     public static void main(String[] args) {
  13.         try (AsynchronousServerSocketChannel server = AsynchronousServerSocketChannel.open()) {
  14.             server.bind(new InetSocketAddress("127.0.0.1", 1234));
  15.             Future<AsynchronousSocketChannel> acceptCon = server.accept();
  16.             AsynchronousSocketChannel client = acceptCon.get(10, TimeUnit.SECONDS);
  17.             if ((client != null) && (client.isOpen())) {
  18.                 ByteBuffer buffer = ByteBuffer.allocate(1024);
  19.                 Future<Integer> readval = client.read(buffer);
  20.                 System.out.println("Received from client: " + new String(buffer.array()).trim());
  21.                 readval.get();
  22.                 buffer.flip();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement