当前位置:网站首页 > Java基础 > 正文

java aio 教程



public class Server {

</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span> main(String[] args) <span style="color: rgba(0, 0, 255, 1)">throws</span><span style="color: rgba(0, 0, 0, 1)"> IOException { </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 实例化,并监听端口</span> AsynchronousServerSocketChannel server =<span style="color: rgba(0, 0, 0, 1)"> AsynchronousServerSocketChannel.open().bind(</span><span style="color: rgba(0, 0, 255, 1)">new</span> InetSocketAddress(8080<span style="color: rgba(0, 0, 0, 1)">)); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 自己定义一个 Attachment 类,用于传递一些信息</span> Attachment att = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Attachment(); att.setServer(server); server.accept(att, </span><span style="color: rgba(0, 0, 255, 1)">new</span> CompletionHandler&lt;AsynchronousSocketChannel, Attachment&gt;<span style="color: rgba(0, 0, 0, 1)">() { @Override </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> completed(AsynchronousSocketChannel client, Attachment att) { </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> { SocketAddress clientAddr </span>=<span style="color: rgba(0, 0, 0, 1)"> client.getRemoteAddress(); System.out.println(</span>"收到新的连接:" +<span style="color: rgba(0, 0, 0, 1)"> clientAddr); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 收到新的连接后,server 应该重新调用 accept 方法等待新的连接进来</span> att.getServer().accept(att, <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">); Attachment newAtt </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Attachment(); newAtt.setServer(server); newAtt.setClient(client); newAtt.setReadMode(</span><span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">); newAtt.setBuffer(ByteBuffer.allocate(</span>2048<span style="color: rgba(0, 0, 0, 1)">)); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 这里也可以继续使用匿名实现类,不过代码不好看,所以这里专门定义一个类</span> client.read(newAtt.getBuffer(), newAtt, <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ChannelHandler()); } </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (IOException ex) { ex.printStackTrace(); } } @Override </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> failed(Throwable t, Attachment att) { System.out.println(</span>"accept failed"<span style="color: rgba(0, 0, 0, 1)">); } }); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 为了防止 main 线程退出</span> <span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> { Thread.currentThread().join(); } </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (InterruptedException e) { } } 

}

  • 上一篇: java ssh教程 pdf
  • 下一篇: java2-6教程
  • 版权声明


    相关文章:

  • java ssh教程 pdf2026-02-12 22:58:02
  • java自学线上教程2026-02-12 22:58:02
  • java me 教程2026-02-12 22:58:02
  • java菜鸟教程shipin2026-02-12 22:58:02
  • java安装之后教程2026-02-12 22:58:02
  • java2-6教程2026-02-12 22:58:02
  • java视频教程集合2026-02-12 22:58:02
  • idea java开发教程2026-02-12 22:58:02
  • aqs视频教程java2026-02-12 22:58:02
  • java432教程免费2026-02-12 22:58:02