package telnet; import java.io.IOException; import java.net.Socket; import java.net.InetAddress; import java.net.InetSocketAddress; import java.net.SocketException; import java.net.UnknownHostException; import java.util.Set; import java.util.Iterator; import java.nio.channels.Selector; import java.nio.channels.SocketChannel; import java.nio.channels.ServerSocketChannel; import java.nio.channels.SelectionKey; import java.nio.channels.ClosedChannelException; import java.nio.channels.spi.SelectorProvider; public class TelnetServer { public static void main(String args[]) { serve(); } private static void serve() { Selector selector; try { selector = SelectorProvider.provider().openSelector(); } catch (IOException e) { System.err.println("could not create channel selector: " + e); return; } ServerSocketChannel channel; try { channel = ServerSocketChannel.open(); } catch (IOException e) { System.err.println("could not create channel: " + e); return; } try { channel.configureBlocking(false); } catch (IOException e) { System.err.println("could not set channel non-blocking mode: " + e); return; } final String ANY_ADDR = "0.0.0.0"; InetAddress anyAddr; try { anyAddr = InetAddress.getByName(ANY_ADDR); } catch (UnknownHostException e) { System.err.println("could not solve wildcard address: " + ANY_ADDR + ": " + e); return; } final int listenPort = 1234; InetSocketAddress sockAddr = new InetSocketAddress(anyAddr, listenPort); if (sockAddr == null) { System.err.println("could not create socket for wildcard address on port " + listenPort); return; } try { channel.socket().bind(sockAddr); } catch (IOException e) { System.err.println("could not bind to local address: " + anyAddr.getHostAddress() + ":" + listenPort + ": " + e); return; } SelectionKey acceptKey; try { acceptKey = channel.register(selector, SelectionKey.OP_ACCEPT); } catch (ClosedChannelException e) { System.err.println("could not register accept interest: " + e); return; } System.out.println("waiting for connections on " + listenPort); for (;;) { int addedKeys; try { addedKeys = selector.select(); } catch (IOException e) { System.err.println("channel selection failed: " + e); continue; } if (addedKeys <= 0) { System.err.println("no key selected"); break; } System.out.println("selected keys: " + addedKeys); Set readyKeys = selector.selectedKeys(); for (Iterator i = readyKeys.iterator(); i.hasNext(); ) { SelectionKey sk = (SelectionKey) i.next(); i.remove(); if (sk.isAcceptable()) { ServerSocketChannel nextReady = (ServerSocketChannel) sk.channel(); SocketChannel ch; try { ch = nextReady.accept(); } catch (IOException e) { System.err.println("could not accept client: " + e); continue; } try { ch.configureBlocking(false); } catch (IOException io) { System.err.println("could not set socket as non-blocking: " + io); try { ch.close(); } catch (IOException e) {} continue; } Socket s = ch.socket(); System.out.println("registering client socket for listening"); try { acceptKey = ch.register(selector, SelectionKey.OP_READ); } catch (ClosedChannelException e) { System.err.println("could not register socket for reading: " + e); continue; } continue; } if (sk.isReadable()) { SocketChannel nextReady = (SocketChannel) sk.channel(); Socket s = nextReady.socket(); System.out.println("disconnecting client socket"); try { nextReady.close(); // calls sk.cancel(), de-registers key } catch (IOException e) { System.err.println("failure closing socket: " + e); } continue; } System.err.println("select LOOP"); } } } }