音乐播放网站开发pc端,WordPress开发过程,重庆电子网站建设,淘宝pc端官网1 非阻塞(Nonblocking)体系结构在这一部分#xff0c;我将从理论的角度来解释非阻塞体系的结构及其工作原理。这部“喜剧”(当然#xff0c;如果你喜欢的话也可以称做戏剧)的“人物”如下#xff1a;●服务器端#xff1a;接收请求的应用程序。●客户端#xff1a;向…1 非阻塞(Nonblocking)体系结构在这一部分我将从理论的角度来解释非阻塞体系的结构及其工作原理。这部“喜剧”(当然如果你喜欢的话也可以称做戏剧)的“人物”如下●服务器端接收请求的应用程序。●客户端向服务器端发出请求的应用程序。●套接字通道客户端与服务器端之间的通信通道。它能识别服务器端的地址和端口号。数据以Buffer中元素的形式通过套接字通道传送。●选择器所有非阻塞技术的主要对象。它监视着已注册的套接字通道并序列化服务器需要应答的请求。●关键字选择器用来对对象的请求进行排序。每个关键字代表一个单独的客户端子请求并包含识别客户端和请求类型的信息。2 SocketChannel 类SocketAddress rama new SocketAddress(localhost , 8888) ;利用静态工厂方法得到SocketChannel的实例。SocketChannel client SocketChannel.open(rama) ;如果这是传统的套接字那么就会寻求得到socket的输入或者输出流利用通道我们可以直接写入通道本身不是写入字节数组而是要写入ByteBuffer对象将此对象写入 client的read 方法。客户端应用程序同时执行对服务器端的请求接着选择器将其集中起来创建关键字然后将其发送至服务器端。这看起来像是阻塞(Blocking)体系因为在一定时间内只处理一个请求但事实并非如此。实际上每个关键字不代表从客户端发至服务器端的整个信息流仅仅只是一部分。我们不要忘了选择器能分割那些被关键字标识的子请求里的数据。因此如果有更多连续地数据发送至服务器端那么选择器就会创建更多的根据时间共享策略(Time-sharing policy)来进行处理的关键字。强调一下在图一中关键字的颜色与客户端的颜色相对应。服务器端非阻塞(Server Nonblocking)客户端和服务器端是两个Java应用程序。套接字通道是SocketChannel类的实例这个类允许通过网络传送数据。它们能被Java程序员看作是一个新的套接字。SocketChannel类被定义在java.nio.channel包中。选择器是一个Selector类的对象。该类的每个实例均能监视更多的套接字通道进而建立更多的连接。当一些有意义的事发生在通道上(如客户端试图连接服务器端或进行读/写操作)选择器便会通知应用程序处理请求。选择器会创建一个关键字这个关键字是SelectionKey类的一个实例。每个关键字都保存着应用程序的标识及请求的类型。其中请求的类型可以是如下之一基本上服务器端的实现是由选择器等待事件和创建关键字的无限循环组成的。根据关键字的类型及时的执行操作。关键字存在以下4种可能的类型。Acceptable: 相应的客户端要求连接。Connectable:服务器端接受连接。Readable:服务器端可读。Writeable:服务器端可写。一个通用的实现非阻塞服务器的算法如下create SocketChannel;create Selectorassociate the SocketChannel to the Selectorfor(;;) {waiting events from the Selector;event arrived; create keys;for each key created by Selector {check the type of request;isAcceptable:get the client SocketChannel;associate that SocketChannel to the Selector;record it for read/write operationscontinue;isReadable:get the client SocketChannel;read from the socket;continue;isWriteable:get the client SocketChannel;write on the socket;continue;}}3 下面为一个实例(1)客户端1packagecn.bupt.channel;23importjava.io.IOException;4importjava.net.InetSocketAddress;5importjava.net.SocketAddress;6importjava.nio.ByteBuffer;7importjava.nio.channels.Channels;8importjava.nio.channels.SocketChannel;9importjava.nio.channels.WritableByteChannel;1011importcn.bupt.constant.Default;1213publicclassChargenClient{1415publicstaticintDEFAULT_PORT8778;16/** *//**17 *paramargs18*/19publicstaticvoidmain(String[] args){20//TODO Auto-generated method stub21if(args.length0)22{23 System.out.println(please input the port);24return;25 }2627intport ;282930 portDEFAULT_PORT ;3132 SocketAddress addressnewInetSocketAddress(args[0] , port) ;33try{34 SocketChannel clientSocketChannel.open(address) ;35 ByteBuffer bufferByteBuffer.allocate(74) ;36 WritableByteChannel outChannels.newChannel(System.out) ;3738while(client.read(buffer)!-1)39{40 buffer.flip() ;41 out.write(buffer) ;42 buffer.clear() ;4344 }45464748495051 }catch(IOException e){52//TODO Auto-generated catch block53e.printStackTrace();54 }55565758 }5960}61(2) 服务器端1packagecn.bupt.channel;23importjava.io.IOException;4importjava.net.InetSocketAddress;5importjava.net.ServerSocket;6importjava.nio.ByteBuffer;7importjava.nio.channels.SelectionKey;8importjava.nio.channels.Selector;9importjava.nio.channels.ServerSocketChannel;10importjava.nio.channels.SocketChannel;11importjava.util.Iterator;12importjava.util.Set;1314publicclassChargenServer{1516publicstaticfinalintDEFAULT_PORT8778;17/** *//**18 *paramargs19*/20publicstaticvoidmain(String[] args){21//TODO Auto-generated method stub22intport ;23 portDEFAULT_PORT ;2425byte[] rotationnewbyte[95*2] ;26for(bytei; i{28 rotation[i-]i ;29 rotation[i95-]i ;30 }3132 ServerSocketChannel serverChannelnull;33 Selector selectornull;343536/** *//**37 * 先建立服务器端的通道38 *39*/4041try{42 serverChannelServerSocketChannel.open() ;43 ServerSocket ssserverChannel.socket() ;44 InetSocketAddress addressnewInetSocketAddress(port) ;45 ss.bind(address) ;46 serverChannel.configureBlocking(false) ;47 selectorSelector.open() ;48 serverChannel.register(selector, SelectionKey.OP_ACCEPT) ;49505152 }catch(IOException e){53//TODO Auto-generated catch block54e.printStackTrace();55 }565758while(true)59{6061try{62 selector.select() ;63 }catch(IOException e){64 e.printStackTrace();65 }6667 Set readyKeysselector.selectedKeys() ;68 Iterator iterreadyKeys.iterator() ;69while(iter.hasNext())70{71 SelectionKey key(SelectionKey) iter.next() ;72 iter.remove() ;7374if(key.isAcceptable())75{76 ServerSocketChannel server(ServerSocketChannel) key.channel() ;77try{78 SocketChannel clientserver.accept() ;79 System.out.println(Accept connection fromclient) ;80 client.configureBlocking(false) ;81 SelectionKey key2client.register(selector, SelectionKey.OP_WRITE) ;82 ByteBuffer bufferByteBuffer.allocate(74) ;83 buffer.put(rotation ,0,72) ;84 buffer.put((byte)\r) ;85 buffer.put((byte)\n) ;86 buffer.flip() ;87 key2.attach(buffer) ;8889909192 }catch(IOException e){93//TODO Auto-generated catch block94e.printStackTrace();95 }96979899100 }101102else103if(key.isWritable())104{105106/** *//**107 * 建立客户端通道108 *109*/110 SocketChannel client(SocketChannel)key.channel() ;111 ByteBuffer buffer(ByteBuffer) key.attachment() ;112if(!buffer.hasRemaining())113{114 buffer.rewind() ;115intfirstbuffer.get() ;116 buffer.rewind() ;117intpositionfirst-1;118 buffer.put(rotation , position ,72) ;119 buffer.put((byte)\r) ;120 buffer.put((byte)\n);121 buffer.flip() ;122 }123try{124 client.write(buffer) ;125 }catch(IOException e){126//TODO Auto-generated catch block127e.printStackTrace();128 }129 }130131132133134135136137138 key.cancel() ;139try{140 key.channel().close() ;141 }catch(IOException e){142//TODO Auto-generated catch block143e.printStackTrace();144 }145 }146147148 }149150151152153154155156157158 }159160}161posted on 2010-08-01 21:19 buptduming 阅读(4114) 评论(0) 编辑 收藏