FileDocCategorySizeDatePackage
ConnectAsync.javaAPI DocExample1084Sat Apr 27 18:47:58 BST 2002com.ronsoft.books.nio.channels

ConnectAsync

public class ConnectAsync extends Object
Demonstrate asynchronous connection of a SocketChannel. Created: April 2002
author
Ron Hitchens (ron@ronsoft.com)
version
$Id: ConnectAsync.java,v 1.2 2002/04/28 01:47:58 ron Exp $

Fields Summary
Constructors Summary
Methods Summary
private static voiddoSomethingUseful()

		System.out.println ("doing something useless");
	
public static voidmain(java.lang.String[] argv)

		String host = "localhost";
		int port = 80;

		if (argv.length == 2) {
			host = argv [0];
			port = Integer.parseInt (argv [1]);
		}

		InetSocketAddress addr = new InetSocketAddress (host, port);
		SocketChannel sc = SocketChannel.open();

		sc.configureBlocking (false);

		System.out.println ("initiating connection");

		sc.connect (addr);

		while ( ! sc.finishConnect()) {
			doSomethingUseful();
		}

		System.out.println ("connection established");

		// Do something with the connected socket
		// The SocketChannel is still non-blocking

		sc.close();