基于AIO的CS聊天室
生活随笔
收集整理的這篇文章主要介紹了
基于AIO的CS聊天室
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
所謂AIO,即是異步IO,它的IO操作交由操作系統(tǒng)完成。設(shè)置監(jiān)聽器(類似于一個(gè)信號(hào)處理函數(shù)),當(dāng)系統(tǒng)IO操作完成時(shí),會(huì)被監(jiān)聽器監(jiān)聽到,并執(zhí)行相應(yīng)的后續(xù)操作,然后返回。
監(jiān)聽器一般使用CompletionHandler。
服務(wù)器端代碼:?
package com.nanhao.AIOTest;import java.io.IOException; import java.net.InetSocketAddress; import java.nio.*; import java.nio.channels.AsynchronousChannelGroup; import java.nio.channels.AsynchronousServerSocketChannel; import java.nio.channels.AsynchronousSocketChannel; import java.nio.channels.CompletionHandler; import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.List; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors;public class Server {static final int PORT = 3000;final static String UTF_8 = "utf-8";static List<AsynchronousSocketChannel>channelList = new ArrayList<>();public void startListen()throws IOException,Exception{//創(chuàng)建一個(gè)線程池ExecutorService executorService = Executors.newFixedThreadPool(20);//以指定的線程池來(lái)創(chuàng)建一個(gè)AsynchronousChannelGroupAsynchronousChannelGroup channelGroup = AsynchronousChannelGroup.withThreadPool(executorService);//通過channelGroup來(lái)創(chuàng)建一個(gè)AsynchronousServerSocketChannelAsynchronousServerSocketChannel serverSocketChannel = AsynchronousServerSocketChannel.open(channelGroup)//指定監(jiān)聽本機(jī)的端口.bind(new InetSocketAddress("127.0.0.1",PORT));//使用監(jiān)聽器來(lái)接收來(lái)自客戶端的鏈接請(qǐng)求serverSocketChannel.accept(null,new AcceptHandler(serverSocketChannel));}public static void main(String[]args)throws Exception{Server server = new Server();server.startListen();}class AcceptHandler implements CompletionHandler<AsynchronousSocketChannel,Object>{private AsynchronousServerSocketChannel serverSocketChannel;public AcceptHandler(AsynchronousServerSocketChannel serverSocketChannel) {this.serverSocketChannel = serverSocketChannel;}//定義一個(gè)ByteBuffer準(zhǔn)備讀取數(shù)據(jù)ByteBuffer byteBuffer = ByteBuffer.allocate(1024);@Overridepublic void completed(final AsynchronousSocketChannel result, Object attachment) {Server.channelList.add(result);serverSocketChannel.accept(null,this);result.read(byteBuffer, null, new CompletionHandler<Integer, Object>() {@Overridepublic void completed(Integer result, Object attachment) {byteBuffer.flip();//將buffer中的字節(jié)轉(zhuǎn)換為字符String context = StandardCharsets.UTF_8.decode(byteBuffer).toString();//由于是聊天室,所以將所有的channel里面寫入這個(gè)消息for(AsynchronousSocketChannel ass:Server.channelList){try{ass.write(ByteBuffer.wrap(context.getBytes(Server.UTF_8))).get();}catch(Exception e){e.printStackTrace();}}byteBuffer.clear();result.read(byteBuffer,null,this);}@Overridepublic void failed(Throwable exc, Object attachment) {System.out.println("讀取數(shù)據(jù)失敗: "+exc);Server.channelList.remove(result);}});}@Overridepublic void failed(Throwable exc, Object attachment) {System.out.println("連接失敗 :"+exc);}}}?
總結(jié)
以上是生活随笔為你收集整理的基于AIO的CS聊天室的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: NIO : selector、chann
- 下一篇: SSH框架整合的流程