Advertisement
marlonyao

EchoServer2.java

Apr 17th, 2011
413
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.51 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<[email protected]>
  15.  *
  16.  */
  17. public class EchoServer2 {
  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_READ);
  39.                         ByteBuffer buffer = ByteBuffer.allocate(100);
  40.                         clientKey.attach(buffer);
  41.                     } else if (key.isReadable()) {
  42.                         SocketChannel client = (SocketChannel) key.channel();
  43.                         ByteBuffer buffer = (ByteBuffer) key.attachment();
  44.                         int n = client.read(buffer);
  45.                         if (n > 0) {
  46.                             buffer.flip();
  47.                             key.interestOps(SelectionKey.OP_WRITE);     // switch to OP_WRITE
  48.                         }
  49.                     } else if (key.isWritable()) {
  50.                         System.out.println("is writable...");
  51.                         SocketChannel client = (SocketChannel) key.channel();
  52.                         ByteBuffer buffer = (ByteBuffer) key.attachment();
  53.                         client.write(buffer);
  54.                         if (buffer.remaining() == 0) {  // write finished, switch to OP_READ
  55.                             buffer.clear();
  56.                             key.interestOps(SelectionKey.OP_READ);
  57.                         }
  58.                     }
  59.                 } catch (IOException e) {
  60.                     key.cancel();
  61.                     try { key.channel().close(); } catch (IOException ioe) { }
  62.                 }
  63.             }
  64.         }
  65.     }
  66.  
  67.     private static void initServer(Selector selector) throws IOException,
  68.             ClosedChannelException {
  69.         ServerSocketChannel serverChannel = ServerSocketChannel.open();
  70.         ServerSocket ss = serverChannel.socket();
  71.         ss.bind(new InetSocketAddress(DEFAULT_PORT));
  72.         serverChannel.configureBlocking(false);
  73.         serverChannel.register(selector, SelectionKey.OP_ACCEPT);
  74.     }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement