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<AsynchronousSocketChannel, Attachment><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) { } }
}
版权声明:
本文来源网络,所有图片文章版权属于原作者,如有侵权,联系删除。
本文网址:https://www.bianchenghao6.com/h6javajc/5698.html