Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 26th, 2012  |  syntax: None  |  size: 5.46 KB  |  hits: 15  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. How to be notified when a SocketChannel is closed?
  2. /*     */   protected final SelectionKey register(AbstractSelectableChannel paramAbstractSelectableChannel, int paramInt, Object paramObject)
  3. /*     */   {
  4. /* 128 */     if (!(paramAbstractSelectableChannel instanceof SelChImpl))
  5. /* 129 */       throw new IllegalSelectorException();
  6.        
  7. import java.io.IOException;
  8. import java.net.InetAddress;
  9. import java.net.InetSocketAddress;
  10. import java.net.Socket;
  11. import java.net.SocketAddress;
  12. import java.net.SocketOption;
  13. import java.net.UnknownHostException;
  14. import java.nio.ByteBuffer;
  15. import java.nio.channels.SelectionKey;
  16. import java.nio.channels.Selector;
  17. import java.nio.channels.ServerSocketChannel;
  18. import java.nio.channels.SocketChannel;
  19. import java.util.Iterator;
  20. import java.util.Set;
  21.  
  22. public class SocketChannelWrapper extends SocketChannel {
  23.     private static interface CloseListener {
  24.         public void socketChannelClosed(SocketChannel channel);
  25.     }
  26.  
  27.     private final SocketChannel socket;
  28.     private final CloseListener listener;
  29.  
  30.     public SocketChannelWrapper(SocketChannel socket, CloseListener l) {
  31.         super(socket.provider());
  32.         this.socket = socket;
  33.         listener = l;
  34.     }
  35.  
  36.     @Override
  37.     public SocketAddress getLocalAddress() throws IOException {
  38.         return socket.getLocalAddress();
  39.     }
  40.  
  41.     @Override
  42.     public <T> T getOption(SocketOption<T> name) throws IOException {
  43.         return socket.getOption(name);
  44.     }
  45.  
  46.     @Override
  47.     public Set<SocketOption<?>> supportedOptions() {
  48.         return socket.supportedOptions();
  49.     }
  50.  
  51.     @Override
  52.     public SocketChannel bind(SocketAddress local) throws IOException {
  53.         return socket.bind(local);
  54.     }
  55.  
  56.     @Override
  57.     public <T> SocketChannel setOption(SocketOption<T> name, T value)
  58.             throws IOException {
  59.         return socket.setOption(name, value);
  60.     }
  61.  
  62.     @Override
  63.     public SocketChannel shutdownInput() throws IOException {
  64.         return socket.shutdownInput();
  65.     }
  66.  
  67.     @Override
  68.     public SocketChannel shutdownOutput() throws IOException {
  69.         return socket.shutdownOutput();
  70.     }
  71.  
  72.     @Override
  73.     public Socket socket() {
  74.         return socket.socket();
  75.     }
  76.  
  77.     @Override
  78.     public boolean isConnected() {
  79.         return socket.isConnected();
  80.     }
  81.  
  82.     @Override
  83.     public boolean isConnectionPending() {
  84.         return socket.isConnectionPending();
  85.     }
  86.  
  87.     @Override
  88.     public boolean connect(SocketAddress remote) throws IOException {
  89.         return socket.connect(remote);
  90.     }
  91.  
  92.     @Override
  93.     public boolean finishConnect() throws IOException {
  94.         return socket.finishConnect();
  95.     }
  96.  
  97.     @Override
  98.     public SocketAddress getRemoteAddress() throws IOException {
  99.         return socket.getRemoteAddress();
  100.     }
  101.  
  102.     @Override
  103.     public int read(ByteBuffer dst) throws IOException {
  104.         return socket.read(dst);
  105.     }
  106.  
  107.     @Override
  108.     public long read(ByteBuffer[] dsts, int offset, int length)
  109.             throws IOException {
  110.         return socket.read(dsts, offset, length);
  111.     }
  112.  
  113.     @Override
  114.     public int write(ByteBuffer src) throws IOException {
  115.         return socket.write(src);
  116.     }
  117.  
  118.     @Override
  119.     public long write(ByteBuffer[] srcs, int offset, int length)
  120.             throws IOException {
  121.         return socket.write(srcs, offset, length);
  122.     }
  123.  
  124.     @Override
  125.     protected void implCloseSelectableChannel() throws IOException {
  126.         socket.close();
  127.         listener.socketChannelClosed(this);
  128.     }
  129.  
  130.     @Override
  131.     protected void implConfigureBlocking(boolean block) throws IOException {
  132.         socket.configureBlocking(block);
  133.     }
  134.  
  135.     public static void main(String[] args) throws UnknownHostException,
  136.             IOException {
  137.         final Selector selector = Selector.open();
  138.         Thread t = new Thread(new Runnable() {
  139.             @Override
  140.             public void run() {
  141.                 while (true) {
  142.                     try {
  143.                         selector.select();
  144.                         Iterator<SelectionKey> itr = selector.selectedKeys()
  145.                                 .iterator();
  146.                         while (itr.hasNext()) {
  147.                             SelectionKey key = itr.next();
  148.                             itr.remove();
  149.  
  150.                             if (key.isValid()) {
  151.                                 if (key.isAcceptable()) {
  152.                                     ((ServerSocketChannel) key.channel())
  153.                                             .accept();
  154.                                 }
  155.                             }
  156.                         }
  157.                     } catch (IOException e) {
  158.                         e.printStackTrace();
  159.                     }
  160.                 }
  161.             }
  162.         });
  163.         t.setDaemon(true);
  164.  
  165.         ServerSocketChannel server = ServerSocketChannel.open().bind(
  166.                 new InetSocketAddress(1234));
  167.         server.configureBlocking(false);
  168.  
  169.         server.register(selector, SelectionKey.OP_ACCEPT);
  170.         t.start();
  171.  
  172.         SocketChannel socket = new SocketChannelWrapper(
  173.                 SocketChannel.open(new InetSocketAddress(InetAddress
  174.                         .getLocalHost(), 1234)), new CloseListener() {
  175.                     @Override
  176.                     public void socketChannelClosed(SocketChannel channel) {
  177.                         System.out.println("Socket closed!");
  178.                     }
  179.                 });
  180.         socket.configureBlocking(false);
  181.         // socket.close(); //prints out "Socket closed!"
  182.         socket.register(selector, SelectionKey.OP_READ);
  183.     }
  184. }