Advertisement
Guest User

AsynchronousSocketChannelChat2

a guest
Dec 11th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.77 KB | None | 0 0
  1. package test;
  2.  
  3. import java.io.ByteArrayOutputStream;
  4. import java.io.IOException;
  5. import java.net.InetSocketAddress;
  6. import java.nio.ByteBuffer;
  7. import java.nio.channels.AsynchronousServerSocketChannel;
  8. import java.nio.channels.AsynchronousSocketChannel;
  9. import java.nio.channels.CompletionHandler;
  10. import java.nio.charset.StandardCharsets;
  11. import java.util.LinkedList;
  12. import java.util.Queue;
  13. import java.util.concurrent.ExecutionException;
  14.  
  15. public class GeneralAioEchoServer {
  16.   private AsynchronousServerSocketChannel assc;
  17.  
  18.   public static void main(String[] args) throws Exception {
  19.     GeneralAioEchoServer server = new GeneralAioEchoServer();
  20.     server.start();
  21.     System.out.println("歡迎來到胖世界");
  22.     while (true) {
  23.       Thread.sleep(5000L);
  24.     }
  25.   }
  26.  
  27.   private void start() throws IOException {
  28.     assc = AsynchronousServerSocketChannel.open();
  29.     assc.bind(new InetSocketAddress(4000));
  30.     assc.accept(null, new AcceptHandler());
  31.   }
  32.  
  33.   private class AcceptHandler implements CompletionHandler<AsynchronousSocketChannel, Object> {
  34.     @Override
  35.     public void completed(AsynchronousSocketChannel asc, Object o) {
  36.       assc.accept(null, this);
  37.       try {
  38.         asc.write(StandardCharsets.UTF_8.encode("歡迎來到胖世界")).get();
  39.       } catch (InterruptedException | ExecutionException e) {
  40.         e.printStackTrace();
  41.       }
  42.       ByteBuffer bb = ByteBuffer.allocate(1024);
  43.       asc.read(bb, null, new ReadHandler(asc, bb));
  44.     }
  45.  
  46.     @Override
  47.     public void failed(Throwable throwable, Object o) {
  48.    
  49.     }
  50.  
  51.   }
  52.  
  53.   private class ReadHandler implements CompletionHandler<Integer, Object> {
  54.     private AsynchronousSocketChannel asc;
  55.     private ByteBuffer bb;
  56.     private ByteArrayOutputStream baos = new ByteArrayOutputStream();
  57.    
  58.     public ReadHandler(AsynchronousSocketChannel asc, ByteBuffer bb) {
  59.       this.asc = asc;
  60.       this.bb = bb;
  61.     }
  62.    
  63.     @Override
  64.     public void completed(Integer result, Object o) {
  65.       long start = System.currentTimeMillis();
  66.      
  67.       if (result == -1) return;      
  68.      
  69.       // read all bytes start
  70.       byte[] bytes = new byte[bb.flip().limit()];
  71.       bb.get(bytes).clear();
  72.       Queue<String> inputs = new LinkedList<>();
  73.       for (byte b : bytes) {
  74.         if (b != '\n') {
  75.           baos.write(b);
  76.         } else {
  77.           inputs.add(new String(baos.toByteArray(), StandardCharsets.UTF_8));
  78.           baos.reset();
  79.         }
  80.       }
  81.       // read all bytes end
  82.            
  83.       asc.read(bb, null, this);
  84.       long end = System.currentTimeMillis() - start;
  85.       System.out.println("total time = " + end);
  86.     }
  87.    
  88.    
  89.     @Override
  90.     public void failed(Throwable throwable, Object o) {
  91.    
  92.     }
  93.    
  94.   }
  95.  
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement