-------------------------
import java.net.InetSocketAddress;
import java.nio.channels.*;
import java.util.Iterator;
public class TestConnect
{
public TestConnect()
{
try
{
Selector selector = Selector.open();
SocketChannel channel = SocketChannel.open();
channel.configureBlocking(false);
channel.connect(new InetSocketAddress("gcc.gnu.org", 80));
channel.register(selector, SelectionKey.OP_CONNECT);
while (true)
{
selector.select();
Iterator it = selector.selectedKeys().iterator();
while (it.hasNext())
{
SelectionKey selKey = (SelectionKey)it.next();
if (selKey.isConnectable())
{
System.out.println("Connection ready");
System.exit(0);
}
}
}
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
public static void main(String[] args)
{
new TestConnect();
}
}