Advertisement
perception-shs

NIO Headaches

Feb 17th, 2012
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.63 KB | None | 0 0
  1. package bg.testing.network;
  2.  
  3. import java.io.IOException;
  4. import java.net.InetSocketAddress;
  5. import java.nio.ByteBuffer;
  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. import java.util.Set;
  12.  
  13. public class SelectorTest {
  14.  
  15.     /**
  16.      * @param args
  17.      */
  18.     public static void main(String[] args) {
  19.         new Thread(new Server()).start();
  20.         new Thread(new Client()).start();
  21.     }
  22.  
  23.     static class Server implements Runnable {
  24.         Selector sel;
  25.  
  26.         @Override
  27.         public void run() {
  28.             try {
  29.                 ServerSocketChannel server = ServerSocketChannel.open();
  30.                 server.socket().bind(new InetSocketAddress(5555));
  31.                 server.configureBlocking(false);
  32.                 sel = Selector.open();
  33.                 server.register(sel, SelectionKey.OP_ACCEPT);
  34.  
  35.                 boolean running = true;
  36.                 while(running) {
  37.                     int count = sel.select();
  38.                     if(sel.isOpen() && count > 0) {
  39.                         Set<SelectionKey> keyset = sel.selectedKeys();
  40.                         synchronized(keyset) {
  41.                             Iterator<SelectionKey> i = keyset.iterator();
  42.                             while(i.hasNext()) {
  43.                                 SelectionKey key = i.next();
  44.                                 i.remove();
  45.                                 processKey(key);
  46.                             }
  47.                         }
  48.                     } else if(!sel.isOpen())
  49.                         running = false;
  50.                 }
  51.             } catch (IOException e) {
  52.                 e.printStackTrace();
  53.             }
  54.         }
  55.  
  56.         private void processKey(SelectionKey key) {
  57.  
  58.             if(key.isValid() && key.isAcceptable()) {
  59.                 try {
  60.                     SocketChannel chan = ((ServerSocketChannel)key.channel()).accept();
  61.                     chan.configureBlocking(false);
  62.                     chan.register(sel, SelectionKey.OP_READ);
  63.                 } catch (IOException e) {
  64.                     e.printStackTrace();
  65.                 }
  66.             }
  67.  
  68.             if(key.isValid() && key.isReadable()) {
  69.                 System.out.println("Read starting...");
  70.                 SocketChannel chan = (SocketChannel) key.channel();
  71.                 ByteBuffer buff = ByteBuffer.allocate(1024);
  72.                 try {
  73.                     while((chan.read(buff))>=0) {
  74.                         buff.flip();
  75.                         System.out.println("read some");
  76.                         buff.clear();
  77.                     }
  78.                     chan.close();
  79.                     System.out.println("Read complete");
  80.                 } catch (IOException e) {
  81.                     e.printStackTrace();
  82.                 }
  83.             }
  84.         }
  85.     }
  86.  
  87.     static class Client implements Runnable {
  88.         @Override
  89.         public void run() {
  90.             try {
  91.                 SocketChannel chan = SocketChannel.open();
  92.                 chan.connect(new InetSocketAddress("localhost", 5555));
  93.                 while(!chan.finishConnect());
  94.                 ByteBuffer buff = ByteBuffer.allocate(1024);
  95.                 for(int i=0;i<1000;i++) {
  96.                     buff.flip();
  97.                     chan.write(buff);
  98.                     buff.compact();
  99.                 }
  100.                 chan.close();
  101.             } catch (IOException e) {
  102.                 e.printStackTrace();
  103.             }
  104.         }
  105.     }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement