JiveJdon Community Forums
在线505人   首页   主题表   培训咨询   标签   精华   查搜   注册    登陆 RSS
首页 » 论坛 » J2EE/JavaEE/JEE/EJB/JSF等技术讨论
???en_US.forumThreadPrev.name??? 上一主题
  Go back to the topic 返回本主题   Go back to the topic listing返回主题列表
???en_US.forumThreadNext.name??? 下一主题
Go 总共有 0 回复 / 1
 发表新帖子   回复该主题贴
student2005

悄悄话
发表文章: 1
注册时间: 2007年11月27日 21:02
请问一个java nio问题 2007年11月27日 21:11 到本帖网址 加入本帖到收藏夹 发送到手机 回复该主题
标签列表
在学习一本java网络编程的书的时候有一个程序看不懂,程序如下,
Example 12-2. A non-blocking chargen server
import java.nio.*;

import java.nio.channels.*;

import java.net.*;

import java.util.*;

import java.io.IOException;



public class ChargenServer {



public static int DEFAULT_PORT = 19;



public static void main(String[] args) {



int port;

try {

port = Integer.parseInt(args[0]);

}

catch (Exception ex) {

port = DEFAULT_PORT;

}

System.out.println("Listening for connections on port " + port);



byte[] rotation = new byte[95*2];

for (byte i = ' '; i <= '~'; i++) {

rotation[i-' '] = i;

rotation[i+95-' '] = i;

}



ServerSocketChannel serverChannel;

Selector selector;

try {

serverChannel = ServerSocketChannel.open( );

ServerSocket ss = serverChannel.socket( );

InetSocketAddress address = new InetSocketAddress(port);

ss.bind(address);

serverChannel.configureBlocking(false);

selector = Selector.open( );

serverChannel.register(selector, SelectionKey.OP_ACCEPT);

}

catch (IOException ex) {

ex.printStackTrace( );

return;

}



while (true) {



try {

selector.select( );

}

catch (IOException ex) {

ex.printStackTrace( );

break;

}



Set readyKeys = selector.selectedKeys( );

Iterator iterator = readyKeys.iterator( );

while (iterator.hasNext( )) {



SelectionKey key = (SelectionKey) iterator.next( );

iterator.remove( );

try {

if (key.isAcceptable( )) {

ServerSocketChannel server = (ServerSocketChannel) key.channel( );

SocketChannel client = server.accept( );

System.out.println("Accepted connection from " + client);

client.configureBlocking(false);

SelectionKey key2 = client.register(selector, SelectionKey.

OP_WRITE);

ByteBuffer buffer = ByteBuffer.allocate(74);

buffer.put(rotation, 0, 72);

buffer.put((byte) '\r');

buffer.put((byte) '\n');

buffer.flip( );

key2.attach(buffer);

}

else if (key.isWritable( )) {

SocketChannel client = (SocketChannel) key.channel( );

ByteBuffer buffer = (ByteBuffer) key.attachment( );

if (!buffer.hasRemaining( )) {

// Refill the buffer with the next line

buffer.rewind( );

// Get the old first character

int first = buffer.get( );

// Get ready to change the data in the buffer

buffer.rewind( );

// Find the new first characters position in rotation

int position = first - ' ' + 1;

// copy the data from rotation into the buffer

buffer.put(rotation, position, 72);

// Store a line break at the end of the buffer

buffer.put((byte) '\r');

buffer.put((byte) '\n');

// Prepare the buffer for writing

buffer.flip( );

}

client.write(buffer);

}

}

catch (IOException ex) {

key.cancel( );

try {

key.channel( ).close( );

}

catch (IOException cex) {}

}



}



}



}



}

请问
1是否可以多个channel在selector中注册?
2这个程序那里体现了使用java nio的好处,这个程序的目的是什么?
谢谢
这个主题有 0 回复 / 1Go
???en_US.forumThreadPrev.name??? 上一主题
  Go back to the topic 返回本主题   Go back to the topic listing返回主题列表    返回页首返回页首
???en_US.forumThreadNext.name??? 下一主题
热点TAG: AOP cache 缓存 DDD EJB 集群 设计模式 Hibernate IOC JiveJdon OO RBAC Seam Spring Struts
正在读取,请等待...
google yahoo 新浪ViVi 365Key网摘 天极网摘 CSDN网摘 添加到百度搜藏 POCO网摘 博采网摘
查询本论坛内 回复超过的热门帖子
     回复该主题贴
标题
 
粗体 斜体 下划线 插入图片 插入代码 插入url链接 插入附件
内容
  发贴前查询 标签列表勿重复发表问题

RSS 手机阅读 add to google add to yahoo
解惑之道在J道 ,打造中国最具影响力的的企业软件社区
OpenSource JIVEJDON v3.0 Powered by JdonFramework Code © 2002-08 jdon.com
anti spam