当前位置:网站首页 > Java教程 > 正文

java aio 教程



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

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


    相关文章:

  • java ssh教程 pdf2025-03-21 08:58:00
  • java自学线上教程2025-03-21 08:58:00
  • java me 教程2025-03-21 08:58:00
  • java菜鸟教程shipin2025-03-21 08:58:00
  • java安装之后教程2025-03-21 08:58:00
  • java2-6教程2025-03-21 08:58:00
  • java视频教程集合2025-03-21 08:58:00
  • idea java开发教程2025-03-21 08:58:00
  • aqs视频教程java2025-03-21 08:58:00
  • java432教程免费2025-03-21 08:58:00