您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 销售管理 > Java NIO非阻塞服务器示例
Mina2.0快速入门与源码剖析第0页共8页JavaNIO非阻塞服务器示例姓名:邓以克网名:phinecos(洞庭散人)MSN:phinecos@msn.com出处:本文版权归作者所有,欢迎传阅,但请保留此段声明。Mina2.0快速入门与源码剖析第1页共8页以前一直用的是“erverythreadperconnection”的服务器端模式,今天试了下NIO非阻塞模式的服务器。不过java不能实现I/O完成端口模型,这点很遗憾packagecom.vista.Server;importjava.io.IOException;importjava.net.InetSocketAddress;importjava.net.ServerSocket;importjava.nio.ByteBuffer;importjava.nio.channels.SelectionKey;importjava.nio.channels.Selector;importjava.nio.channels.ServerSocketChannel;importjava.nio.channels.SocketChannel;importjava.util.Iterator;importjava.util.LinkedList;importjava.util.Set;publicclassSelectorServer{privatestaticintDEFAULT_SERVERPORT=6018;//默认端口privatestaticintDEFAULT_BUFFERSIZE=1024;//默认缓冲区大小为1024字节privateServerSocketChannelchannel;privateLinkedListSocketChannelclients;privateSelectorreadSelector;privateByteBufferbuffer;//字节缓冲区privateintport;publicSelectorServer(intport)throwsIOException{this.port=port;this.clients=newLinkedListSocketChannel();this.channel=null;this.readSelector=Selector.open();//打开选择器this.buffer=ByteBuffer.allocate(DEFAULT_BUFFERSIZE);}//服务器程序在服务循环中调用sericeClients()方法为已接受的客户服务publicvoidserviceClients()throwsIOException{Setkeys;Iteratorit;SelectionKeykey;SocketChannelclient;//在readSelector上调用select()方法,参数1代表如果调用select的时候那Mina2.0快速入门与源码剖析第2页共8页么阻塞最多1秒钟等待可用的客户端连接if(readSelector.select(1)0){keys=readSelector.selectedKeys();//取得代表端通道的键集合it=keys.iterator();//遍历,为每一个客户服务while(it.hasNext()){key=(SelectionKey)it.next();if(key.isReadable()){//如果通道可读,那么读此通道到buffer中intbytes;client=(SocketChannel)key.channel();//取得键对应的通道buffer.clear();//清空缓冲区中的内容,设置好position,limit,准备接受数据bytes=client.read(buffer);//从通道中读数据到缓冲中,返回读取得字节数if(bytes=0){buffer.flip();//准备将缓冲中的数据写回到通道中client.write(buffer);//数据写回到通道中}elseif(bytes0){//如果返回小于零的值代表读到了流的末尾clients.remove(client);//通道关闭时,选择键也被取消client.close();}}}}}publicvoidregisterClient(SocketChannelclient)throwsIOException{//配置和注册代表客户连接的通道对象client.configureBlocking(false);//设置此通道使用非阻塞模式client.register(readSelector,SelectionKey.OP_READ);//将这个通道注册到选择器上clients.add(client);//保存这个通道对象}publicvoidlisten()throwsIOException{//服务器开始监听端口,提供服务ServerSocketsocket;SocketChannelclient;Mina2.0快速入门与源码剖析第3页共8页channel=ServerSocketChannel.open();//打开通道socket=channel.socket();//得到与通到相关的socket对象socket.bind(newInetSocketAddress(port),10);//将scoket榜定在制定的端口上//配置通到使用非阻塞模式,在非阻塞模式下,可以编写多道程序同时避免使用复杂的多线程channel.configureBlocking(false);try{while(true){//与通常的程序不同,这里使用channel.accpet()接受客户端连接请求,而不是在socket对象上调用accept(),这里在调用accept()方法时如果通道配置为非阻塞模式,那么accept()方法立即返回null,并不阻塞client=channel.accept();if(client!=null){registerClient(client);//注册客户信息}serviceClients();//为以连接的客户服务}}finally{socket.close();//关闭socket,关闭socket会同时关闭与此socket关联的通道}}publicstaticvoidmain(String[]args)throwsIOException{System.out.println(服务器启动);SelectorServerserver=newSelectorServer(SelectorServer.DEFAULT_SERVERPORT);server.listen();//服务器开始监听端口,提供服务}}修改版本:packagecom.vista.Server;Mina2.0快速入门与源码剖析第4页共8页importjava.io.BufferedWriter;importjava.io.FileInputStream;importjava.io.IOException;importjava.io.OutputStreamWriter;importjava.io.PrintWriter;importjava.net.InetSocketAddress;importjava.net.ServerSocket;importjava.nio.ByteBuffer;importjava.nio.CharBuffer;importjava.nio.channels.FileChannel;importjava.nio.channels.SelectionKey;importjava.nio.channels.Selector;importjava.nio.channels.ServerSocketChannel;importjava.nio.channels.SocketChannel;importjava.nio.charset.Charset;importjava.nio.charset.CharsetDecoder;importjava.util.Iterator;importjava.util.LinkedList;importjava.util.Set;publicclassSelectorServer{privatestaticintDEFAULT_SERVERPORT=6018;//默认端口privatestaticintDEFAULT_BUFFERSIZE=1024;//默认缓冲区大小为1024字节privatestaticStringDEFAULT_CHARSET=GB2312;//默认码集privatestaticStringDEFAULT_FILENAME=bigfile.dat;privateServerSocketChannelchannel;privateLinkedListSocketChannelclients;privateSelectorselector;//选择器privateByteBufferbuffer;//字节缓冲区privateintport;privateCharsetcharset;//字符集privateCharsetDecoderdecoder;//解码器publicSelectorServer(intport)throwsIOException{this.port=port;this.clients=newLinkedListSocketChannel();this.channel=null;this.selector=Selector.open();//打开选择器this.buffer=ByteBuffer.allocate(DEFAULT_BUFFERSIZE);this.charset=Charset.forName(DEFAULT_CHARSET);Mina2.0快速入门与源码剖析第5页共8页this.decoder=this.charset.newDecoder();}privateclassHandleClient{privateStringstrGreeting=welcometoVistaQQ;publicHandleClient()throwsIOException{}publicStringreadBlock(){//读块数据returnthis.strGreeting;}publicvoidclose(){}}protectedvoidhandleKey(SelectionKeykey)throwsIOException{//处理事件if(key.isAcceptable()){//接收请求ServerSocketChannelserver=(ServerSocketChannel)key.channel();//取出对应的服务器通道SocketChannelchannel=server.accept();channel.configureBlocking(false);channel.register(selector,SelectionKey.OP_READ);//客户socket通道注册读操作}elseif(key.isReadable()){//读信息SocketChannelchannel=(SocketChannel)key.channel();intcount=channel.read(this.buffer);if(count0){this.buffer.flip();CharBuffercharBuffer=decoder.decode(this.buffer);System.out.println(Client+charBuffer.toSt
本文标题:Java NIO非阻塞服务器示例
链接地址:https://www.777doc.com/doc-1579312 .html