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