当前位置: 代码迷 >> J2EE >> 自个儿写了一个java nio 的小例子,但是迷惑不解
  详细解决方案

自个儿写了一个java nio 的小例子,但是迷惑不解

热度:344   发布时间:2016-04-17 23:38:14.0
自己写了一个java nio 的小例子,但是迷惑不解
本帖最后由 iteyer 于 2014-10-06 18:55:57 编辑
这两天假日看了一点java nio的博文,自己模仿者写了一个小例子:
服务端:

public class NIOServer {

private Selector selector = null;

public static void main(String[] args)
throws IOException {
new NIOServer();
}
public NIOServer() throws IOException{
initNIOServer();
start();
}

private void start() throws IOException{
while(true){
selector.select();// 监听所有客户端的请求

Set<SelectionKey> keys = selector.selectedKeys();
for (SelectionKey selectionKey : keys) {// 遍历找到是那个SelectionKey
keys.remove(selectionKey);
try{
if(selectionKey.isAcceptable()){
ServerSocketChannel ssChannel = 
(ServerSocketChannel) selectionKey.channel();
SocketChannel sChannel = ssChannel.accept();
System.out.println("接受到请求,来自:" + sChannel);
sChannel.configureBlocking(false);

ByteBuffer buffer = ByteBuffer.allocate(100);

SelectionKey skey = sChannel.register(selector,
SelectionKey.OP_WRITE|SelectionKey.OP_READ);// 重新注册一下?
skey.attach(buffer);
}
if(selectionKey.isReadable()){
SocketChannel sChannel = (SocketChannel) 
selectionKey.channel();
ByteBuffer buffer = (ByteBuffer) 
selectionKey.attachment();
sChannel.read(buffer);// 将信息从channel写入缓存中
buffer.flip();
byte[] array = buffer.array();
System.out.println("服务端接收到:" + new String(array));// 再输出给用户
SelectionKey skey = selectionKey
.interestOps(SelectionKey.OP_WRITE);// 重新注册一下?
skey.attach(buffer);
}
if(selectionKey.isWritable()){
SocketChannel sChannel = (SocketChannel) 
selectionKey.channel();
ByteBuffer buffer = (ByteBuffer) 
selectionKey.attachment();
System.out.print("请向客户端发送信息:");
String msg = UIUtil.readString();// 读取用户录入的信息
buffer.put(msg.getBytes("UTF-8"));
buffer.flip();// 缓存中读取的信息
while(buffer.hasRemaining())
sChannel.write(buffer);// 将信息从缓存中写入到channel中
buffer.compact();// 清空缓存...
SelectionKey skey = sChannel.register(selector,
SelectionKey.OP_READ);// 重新注册一下?
skey.attach(buffer);
}
}catch(IOException e){
selectionKey.cancel();
                    try {
                     selectionKey
                     .channel().close();
                    }catch (IOException ioe){
                     ioe.printStackTrace();
                    }  
}
}
}
}

private void initNIOServer() throws IOException{
selector = Selector.open();
ServerSocketChannel serverChannel 
= ServerSocketChannel.open();
ServerSocket ss = serverChannel.socket();
ss.bind(new InetSocketAddress(8888));
serverChannel.configureBlocking(false);
serverChannel.register(selector, SelectionKey.OP_ACCEPT);
}

}

客户端:

public class NIOClient {

private Selector selector;

public static void main(String[] args) throws IOException {
new NIOClient("127.0.0.1", 8888);
}

private void start() throws IOException{
while (true) {  
         selector.select();
         Set<SelectionKey> keys = selector.selectedKeys();
         for (SelectionKey selectionKey : keys) {
         keys.remove(selectionKey);
         if(selectionKey.isConnectable()){
         SocketChannel sChannel = (SocketChannel) selectionKey  
                            .channel();
         if(sChannel.isConnectionPending()){  
         sChannel.finishConnect();  
                    }
  相关解决方案