⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 运用异步输入输出流编写socket进程通信2.htm

📁 java非阻塞io编程
💻 HTM
📖 第 1 页 / 共 2 页
字号:
          <TD align=middle class=td1 height=33 vAlign=center><FONT 
            size=+1><B><BR>运用异步输入输出流编写Socket进程通信(2) </B></FONT><BR><BR>[ 作者: 杨健 
            &nbsp; 添加时间: 2001-12-3 10:26:31 ]<BR></TD></TR>
        <TR>
          <TD align=left class=td1 height=151 vAlign=top><BR>this.port = 
            port;<BR>&nbsp; &nbsp;}<BR>&nbsp; &nbsp;<BR>&nbsp; &nbsp;public void 
            initialize() throws IOException {<BR>&nbsp; &nbsp; 
            &nbsp;//初始化,分别实例化一个选择器,一个服务器端可选择通道<BR>&nbsp; &nbsp; 
            &nbsp;this.selector = Selector.open();<BR>&nbsp; &nbsp; 
            &nbsp;this.serverChannel = ServerSocketChannel.open();<BR>&nbsp; 
            &nbsp; &nbsp;this.serverChannel.configureBlocking(false);<BR>&nbsp; 
            &nbsp; &nbsp;InetAddress localhost = 
            InetAddress.getLocalHost();<BR>&nbsp; &nbsp; &nbsp;InetSocketAddress 
            isa = new InetSocketAddress(localhost, this.port );<BR>&nbsp; &nbsp; 
            &nbsp;this.serverChannel.socket().bind(isa);//将该套接字绑定到服务器某一可用端口<BR>&nbsp; 
            &nbsp;}<BR>&nbsp; &nbsp;//结束时释放资源<BR>&nbsp; &nbsp;public void 
            finalize() throws IOException {<BR>&nbsp; &nbsp; &nbsp; 
            &nbsp;this.serverChannel.close();<BR>&nbsp; &nbsp; &nbsp; 
            &nbsp;this.selector.close();<BR>&nbsp; &nbsp;}<BR>&nbsp; 
            &nbsp;//将读入字节缓冲的信息解码<BR>&nbsp; &nbsp;public String decode( 
            ByteBuffer byteBuffer ) throws <BR>CharacterCodingException 
            {<BR>&nbsp; &nbsp; &nbsp; &nbsp;Charset charset = Charset.forName( 
            "ISO-8859-1" );<BR>&nbsp; &nbsp; &nbsp; &nbsp;CharsetDecoder decoder 
            = charset.newDecoder();<BR>&nbsp; &nbsp; &nbsp; &nbsp;CharBuffer 
            charBuffer = decoder.decode( byteBuffer );<BR>&nbsp; &nbsp; &nbsp; 
            &nbsp;String result = charBuffer.toString();<BR>&nbsp; &nbsp; &nbsp; 
            &nbsp;return result;<BR>&nbsp; &nbsp;}<BR>&nbsp; 
            &nbsp;//监听端口,当通道准备好时进行相应操作<BR>&nbsp; &nbsp;public void 
            portListening() throws IOException, InterruptedException {<BR>&nbsp; 
            &nbsp; &nbsp;//服务器端通道注册OP_ACCEPT事件<BR>&nbsp; &nbsp; 
            &nbsp;SelectionKey acceptKey =this.serverChannel.register( 
            this.selector,<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SelectionKey.OP_ACCEPT 
            );<BR>&nbsp; &nbsp; &nbsp; 
            &nbsp;//当有已注册的事件发生时,select()返回值将大于0<BR>&nbsp; &nbsp; &nbsp; 
            &nbsp;while (acceptKey.selector().select() &gt; 0 ) {<BR>&nbsp; 
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.out.println("event 
            happened");<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
            &nbsp;//取得所有已经准备好的所有选择键<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
            &nbsp;Set readyKeys = this.selector.selectedKeys();<BR>&nbsp; &nbsp; 
            &nbsp; &nbsp; &nbsp; &nbsp;//使用迭代器对选择键进行轮询<BR>&nbsp; &nbsp; &nbsp; 
            &nbsp; &nbsp; &nbsp;Iterator i = readyKeys.iterator();<BR>&nbsp; 
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;while (i.hasNext()) {<BR>&nbsp; 
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SelectionKey key = 
            (SelectionKey)i.next();<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
            &nbsp; &nbsp;i.remove();//删除当前将要处理的选择键<BR>&nbsp; &nbsp; &nbsp; 
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if ( key.isAcceptable() ) 
            {//如果是有客户端连接请求<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
            &nbsp; &nbsp; &nbsp;System.out.println("more client connect 
            in!");<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
            &nbsp; &nbsp;ServerSocketChannel nextReady =<BR>&nbsp; &nbsp; &nbsp; 
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
            &nbsp;(ServerSocketChannel)key.channel();<BR>&nbsp; &nbsp; &nbsp; 
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//获取客户端套接字<BR>&nbsp; 
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Socket 
            s = nextReady.accept();<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
            &nbsp; &nbsp; &nbsp; &nbsp;//设置对应的通道为异步方式并注册感兴趣事件<BR>&nbsp; &nbsp; 
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
            &nbsp;s.getChannel().configureBlocking( false );<BR>&nbsp; &nbsp; 
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SelectionKey 
            readWriteKey =<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;s.getChannel().register( 
            this.selector,<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
            &nbsp;SelectionKey.OP_READ|SelectionKey.OP_WRITE &nbsp;);<BR>&nbsp; 
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
            &nbsp;//将注册的事件与该套接字联系起来<BR>readWriteKey.attach( s 
            );<BR>//将当前建立连接的客户端套接字及对应的通道存放在哈希表//clientChannelMap中<BR>&nbsp; 
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
            &nbsp;this.clientChannelMap.put( s, new <BR>ClientChInstance( 
            s.getChannel() ) );<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
            &nbsp; &nbsp; &nbsp; &nbsp;}<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
            &nbsp; &nbsp; &nbsp;else if ( key.isReadable() ) 
            {//如果是通道读准备好事件<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
            &nbsp; &nbsp; &nbsp;System.out.println("Readable");<BR>&nbsp; &nbsp; 
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
            &nbsp;//取得选择键对应的通道和套接字<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
            &nbsp; &nbsp; &nbsp; &nbsp;SelectableChannel nextReady =<BR>&nbsp; 
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
            &nbsp; &nbsp;(SelectableChannel) key.channel();<BR>&nbsp; &nbsp; 
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Socket socket 
            = (Socket) key.attachment();<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
            &nbsp; &nbsp; &nbsp; &nbsp; 
            &nbsp;//处理该事件,处理方法已封装在类ClientChInstance中<BR>&nbsp; &nbsp; &nbsp; 
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
            &nbsp;this.readFromChannel( socket.getChannel(),<BR>&nbsp; &nbsp; 
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
            &nbsp;(ClientChInstance)<BR>this.clientChannelMap.get( socket ) 
            );<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
            &nbsp;}<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
            &nbsp;else if ( key.isWritable() ) {//如果是通道写准备好事件<BR>&nbsp; &nbsp; 
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
            &nbsp;System.out.println("writeable");<BR>&nbsp; &nbsp; &nbsp; 
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
            &nbsp;//取得套接字后处理,方法同上<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
            &nbsp; &nbsp; &nbsp; &nbsp;Socket socket = (Socket) 
            key.attachment();<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
            &nbsp; &nbsp; &nbsp; &nbsp;SocketChannel channel = (SocketChannel) 
            <BR>socket.getChannel();<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;this.writeToChannel( channel,"This 
            is from server!");<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
            &nbsp; &nbsp;}<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
            &nbsp;}<BR>&nbsp; &nbsp; &nbsp; &nbsp;}<BR>&nbsp; &nbsp;}<BR>&nbsp; 
            &nbsp;//对通道的写操作<BR>&nbsp; &nbsp;public void writeToChannel( 
            SocketChannel channel, String message ) <BR>throws IOException 
            {<BR>&nbsp; &nbsp; &nbsp; &nbsp;ByteBuffer buf = ByteBuffer.wrap( 
            message.getBytes() &nbsp;);<BR>&nbsp; &nbsp; &nbsp; &nbsp;int nbytes 
            = channel.write( buf );<BR>&nbsp; &nbsp;}<BR>&nbsp; &nbsp; 
            //对通道的读操作<BR>&nbsp; &nbsp;public void readFromChannel( SocketChannel 
            channel, ClientChInstance clientInstance )<BR>&nbsp; &nbsp;throws 
            IOException, InterruptedException {<BR>&nbsp; &nbsp; &nbsp; 
            &nbsp;ByteBuffer byteBuffer = ByteBuffer.allocate( BUFFERSIZE 
            );<BR>&nbsp; &nbsp; &nbsp; &nbsp;int nbytes = channel.read( 
            byteBuffer ); <BR><BR></TD></TR>
        <TR>
          <TD align=middle class=td4 height=48 vAlign=center><A 
            href="http://www.5xsoft.com/data/200112/0310225001.htm"><FONT 
            face=Webdings>7</FONT> 上一页</A> &nbsp; &nbsp; <A 
            href="http://www.5xsoft.com/data/200112/0310225001_2.htm">下一页 <FONT 
            face=Webdings>8</FONT></A> </TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE>
<TABLE align=center border=0 cellPadding=0 cellSpacing=0 height=58 width=778>
  <TBODY>
  <TR>
    <TD align=middle height=58 width=784><BR>&copy; Copyright 化境编程界 稻香老农 2000.4 - 
      2002.12 | OICQ:593737 | Email: 5xsoft@21cn.com <IMG border=0 
      src="运用异步输入输出流编写Socket进程通信2.files/top.htm"></TD></TR></TBODY></TABLE></BODY></HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -