test/java/nio/channels/Selector/BasicConnect.java
Print this page
@@ -1,7 +1,7 @@
/*
- * Copyright (c) 2001, 2010, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2001, 2012, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
@@ -25,16 +25,14 @@
* @summary Test nonblocking connect and finishConnect
* @bug 4457776
* @library ..
*/
-import java.io.*;
import java.net.*;
import java.nio.*;
import java.nio.channels.*;
import java.nio.channels.spi.SelectorProvider;
-import java.nio.charset.*;
import java.util.*;
/**
* Typically there would be more than one channel registered to select
@@ -42,21 +40,25 @@
* registered for the connectSelector.
*/
public class BasicConnect {
- static final int PORT = 7; // echo
- static final String HOST = TestUtil.HOST;
-
public static void main(String[] args) throws Exception {
Selector connectSelector =
SelectorProvider.provider().openSelector();
- InetSocketAddress isa
- = new InetSocketAddress(InetAddress.getByName(HOST), PORT);
+ try (TestUtil.EchoServer echoServer =
+ TestUtil.EchoServer.startNewServer(100)) {
+ InetSocketAddress isa = new InetSocketAddress(echoServer.getAddress(),
+ echoServer.getPort());
SocketChannel sc = SocketChannel.open();
sc.configureBlocking(false);
boolean result = sc.connect(isa);
+ if (result) {
+ System.err.println("Socket immediately connected on "
+ + System.getProperty("os.name")
+ + ": " + sc);
+ }
while (!result) {
SelectionKey connectKey = sc.register(connectSelector,
SelectionKey.OP_CONNECT);
int keysAdded = connectSelector.select();
if (keysAdded > 0) {
@@ -89,7 +91,7 @@
if (!bb.equals(bb2))
throw new Exception("Echoed bytes incorrect: Sent "
+ bb + ", got " + bb2);
}
-
+ }
}