FileDocCategorySizeDatePackage
SelectTest.javaAPI DocExample2042Mon May 20 00:24:28 BST 2002com.ronsoft.books.nio.channels

SelectTest

public class SelectTest extends Object
Test select return value. Start this server, then connect to port 1234. The incoming connection will be registered with the selector but never read. Type something on the conection, the selector will see the channel ready but the channel is never serviced in the loop. Select will return 1 forever after.
author
Ron Hitchens (ron@ronsoft.com)
version
$Id: SelectTest.java,v 1.1 2002/05/20 07:24:29 ron Exp $

Fields Summary
public static int
PORT_NUMBER
Constructors Summary
Methods Summary
public static voidmain(java.lang.String[] argv)


	      
		 
	
		ServerSocketChannel ssc = ServerSocketChannel.open();
		Selector selector = Selector.open();

		ssc.socket().bind (new InetSocketAddress (PORT_NUMBER));
		ssc.configureBlocking (false);
		ssc.register (selector, SelectionKey.OP_ACCEPT);

		while (true) {
			int n = selector.select (1000);

			System.out.println ("selector returns: " + n);

			Iterator it = selector.selectedKeys().iterator();

			while (it.hasNext()) {
				SelectionKey key = (SelectionKey) it.next();

				// Is a new connection coming in?
				if (key.isAcceptable()) {
					ServerSocketChannel server =
						(ServerSocketChannel) key.channel();
					SocketChannel channel = server.accept();

					// set the new channel non-blocking
					channel.configureBlocking (false);

					// register it with the selector
					channel.register (selector,
						SelectionKey.OP_READ);

					it.remove();
				}

				// is there data to read on this channel?
				if (key.isReadable()) {
					System.out.println ("Channel is readable");
					// don't actually do anything
				}

				// it.remove();
			}
		}