Advertisement
marlonyao

EchoServer.java

Apr 17th, 2011
404
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.30 KB | None | 0 0
  1. import java.io.IOException;
  2. import java.net.InetSocketAddress;
  3. import java.net.ServerSocket;
  4. import java.nio.ByteBuffer;
  5. import java.nio.channels.ClosedChannelException;
  6. import java.nio.channels.SelectionKey;
  7. import java.nio.channels.Selector;
  8. import java.nio.channels.ServerSocketChannel;
  9. import java.nio.channels.SocketChannel;
  10. import java.util.Iterator;
  11.  
  12.  
  13. /**
  14.  * @author marlonyao<yaolei135@gmail.com>
  15.  *
  16.  */
  17. public class EchoServer {
  18.     public static int DEFAULT_PORT = 7777;
  19.  
  20.     public static void main(String[] args) throws IOException {
  21.         System.out.println("Listening for connection on port " + DEFAULT_PORT);
  22.  
  23.         Selector selector = Selector.open();
  24.         initServer(selector);
  25.  
  26.         while (true) {
  27.             selector.select();
  28.  
  29.             for (Iterator<SelectionKey> itor = selector.selectedKeys().iterator(); itor.hasNext();) {
  30.                 SelectionKey key = (SelectionKey) itor.next();
  31.                 itor.remove();
  32.                 try {
  33.                     if (key.isAcceptable()) {
  34.                         ServerSocketChannel server = (ServerSocketChannel) key.channel();
  35.                         SocketChannel client = server.accept();
  36.                         System.out.println("Accepted connection from " + client);
  37.                         client.configureBlocking(false);
  38.                         SelectionKey clientKey = client.register(selector, SelectionKey.OP_WRITE|SelectionKey.OP_READ);
  39.                         ByteBuffer buffer = ByteBuffer.allocate(100);
  40.                         clientKey.attach(buffer);
  41.                     }
  42.                     if (key.isReadable()) {
  43.                         SocketChannel client = (SocketChannel) key.channel();
  44.                         ByteBuffer output = (ByteBuffer) key.attachment();
  45.                         client.read(output);
  46.                     }
  47.                     if (key.isWritable()) {
  48.                         // System.out.println("is writable...");
  49.                         SocketChannel client = (SocketChannel) key.channel();
  50.                         ByteBuffer output = (ByteBuffer) key.attachment();
  51.                         output.flip();
  52.                         client.write(output);
  53.                         output.compact();
  54.                     }
  55.                 } catch (IOException e) {
  56.                     key.cancel();
  57.                     try { key.channel().close(); } catch (IOException ioe) { }
  58.                 }
  59.             }
  60.         }
  61.     }
  62.  
  63.     private static void initServer(Selector selector) throws IOException,
  64.             ClosedChannelException {
  65.         ServerSocketChannel serverChannel = ServerSocketChannel.open();
  66.         ServerSocket ss = serverChannel.socket();
  67.         ss.bind(new InetSocketAddress(DEFAULT_PORT));
  68.         serverChannel.configureBlocking(false);
  69.         serverChannel.register(selector, SelectionKey.OP_ACCEPT);
  70.     }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement