Chinaunix首页 | 论坛 | 博客
  • 博客访问: 83793
  • 博文数量: 31
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 340
  • 用 户 组: 普通用户
  • 注册时间: 2013-04-02 20:25
文章分类

全部博文(31)

文章存档

2015年(2)

2014年(29)

我的朋友

分类: Java

2014-09-10 15:06:00


点击(此处)折叠或打开

  1. import java.io.IOException;
  2. import java.net.InetSocketAddress;
  3. import java.nio.ByteBuffer;
  4. import java.nio.channels.SelectionKey;
  5. import java.nio.channels.Selector;
  6. import java.nio.channels.ServerSocketChannel;
  7. import java.nio.channels.SocketChannel;
  8. import java.util.Iterator;
  9. import java.util.Set;

  10. public class EchoServer {

  11.     public static void main(String[] args) throws IOException {
  12.         
  13.         ByteBuffer bytes = ByteBuffer.allocate(1024);
  14.             
  15.         ServerSocketChannel servChannel = ServerSocketChannel.open();
  16.         servChannel.socket().bind(new InetSocketAddress(8888));
  17.         servChannel.configureBlocking(false);

  18.         Selector selector = Selector.open();
  19.         servChannel.register(selector, SelectionKey.OP_ACCEPT);
  20.         while (true) {
  21.             int rds = selector.select();
  22.             if (rds == 0)
  23.                 continue;
  24.             Set<SelectionKey> selectedKeys = selector.selectedKeys();
  25.             Iterator<SelectionKey> keyIterator = selectedKeys.iterator();
  26.             while (keyIterator.hasNext()) {
  27.                 SelectionKey key = keyIterator.next();
  28.                 if (key.isAcceptable()) {
  29.                     // a connection was accepted by a ServerSocketChannel.
  30.                     ServerSocketChannel server = (ServerSocketChannel )key.channel();
  31.                     SocketChannel cli = server.accept();
  32.                     cli.configureBlocking(false);
  33.                     cli.register(selector, SelectionKey.OP_READ);
  34.                 } else if (key.isConnectable()) {
  35.                     // a connection was established with a remote server.
  36.                 } else if (key.isReadable()) {
  37.                     // a channel is ready for reading
  38.                     SocketChannel cli = (SocketChannel)key.channel();
  39.                     bytes.clear();
  40.                     int cnt = cli.read(bytes);
  41.                     if(cnt == -1){
  42.                         System.out.println(cli.getRemoteAddress() + "连接断开!");
  43.                         cli.close();
  44.                     }else{
  45.                         bytes.flip();
  46.                         byte[] dst = new byte[bytes.limit()];
  47.                         bytes.get(dst);
  48.                         
  49.                         String s = new String(dst,"UTF-8");
  50.                         System.out.println(cli.getRemoteAddress() + ":" + s);
  51.                         cli.register(selector, SelectionKey.OP_WRITE, s);
  52.                     }                    
  53.                 } else if (key.isWritable()) {
  54.                     // a channel is ready for writing
  55.                     SocketChannel cli = (SocketChannel)key.channel();
  56.                     String s = (String)key.attachment();
  57.                     byte[] bs = s.getBytes("UTF-8");
  58.                     bytes.clear();
  59.                     bytes.put(bs);
  60.                     bytes.flip(); //转读
  61.                     cli.write(bytes);
  62.                     cli.register(selector, SelectionKey.OP_READ);
  63.                 }
  64.                 keyIterator.remove();
  65.             }
  66.         }
  67.     }

  68. }

阅读(842) | 评论(0) | 转发(0) |
0

上一篇:2. NIO概述

下一篇:4. tcp

给主人留下些什么吧!~~