test/java/nio/channels/SocketChannel/Connect.java

Print this page


   1 /*
   2  * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /* @test
  25  * @bug 4650679
  26  * @summary Unit test for socket channels
  27  * @library ..
  28  */
  29 

  30 import java.nio.*;
  31 import java.nio.channels.*;
  32 import java.net.*;
  33 import java.util.*;
  34 
  35 public class Connect {
  36 
  37     private static final long INCREMENTAL_DELAY = 30L * 1000L;
  38 
  39     public static void main(String args[]) throws Exception {
  40         test1(TestUtil.HOST);





  41         try {
  42             test1(TestUtil.REFUSING_HOST);


  43             throw new Exception("Refused connection throws no exception");
  44         } catch (ConnectException ce) {
  45             // Correct result
  46         }
  47     }
  48 
  49     static void test1(String hostname) throws Exception {
  50         Selector selector;
  51         SocketChannel sc;
  52         SelectionKey sk;
  53         InetSocketAddress isa = new InetSocketAddress(
  54             InetAddress.getByName (hostname), 80);
  55         sc = SocketChannel.open();
  56         sc.configureBlocking(false);
  57 
  58         selector = Selector.open();
  59         sk = sc.register(selector, SelectionKey.OP_CONNECT);
  60         if (sc.connect(isa)) {
  61             System.err.println("Connected immediately!");
  62             sc.close();
  63             selector.close();
  64             return;
  65         } else {
  66             ByteBuffer buf = ByteBuffer.allocateDirect(100);
  67             buf.asCharBuffer().put(new String(
  68                 "The quick brown fox jumped over the lazy dog."
  69                 ).toCharArray());
  70             buf.flip();
  71             long startTime = System.currentTimeMillis();
  72             while(true) {
  73                 selector.select(INCREMENTAL_DELAY);
  74                 Set selectedKeys = selector.selectedKeys();


   1 /*
   2  * Copyright (c) 2002, 2012, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /* @test
  25  * @bug 4650679
  26  * @summary Unit test for socket channels
  27  * @library ..
  28  */
  29 
  30 import java.net.*;
  31 import java.nio.*;
  32 import java.nio.channels.*;

  33 import java.util.*;
  34 
  35 public class Connect {
  36 
  37     private static final long INCREMENTAL_DELAY = 30L * 1000L;
  38 
  39     public static void main(String args[]) throws Exception {
  40         // We use an EchoServer rather than an HttpServer here = but hopefully
  41         // it doesn't matter for the test purposes.
  42         try (TestUtil.EchoServer echoServer =
  43                         TestUtil.EchoServer.startNewServer(1000)) {
  44             test1(echoServer);
  45         }
  46         try {
  47             TestUtil.RefusingServer refusingServer =
  48                     TestUtil.RefusingServer.startNewServer();
  49             test1(refusingServer);
  50             throw new Exception("Refused connection throws no exception");
  51         } catch (ConnectException ce) {
  52             // Correct result
  53         }
  54     }
  55 
  56     static void test1(TestUtil.AbstractServer server) throws Exception {
  57         Selector selector;
  58         SocketChannel sc;
  59         SelectionKey sk;
  60         InetSocketAddress isa = new InetSocketAddress(
  61                 server.getAddress(), server.getPort());
  62         sc = SocketChannel.open();
  63         sc.configureBlocking(false);
  64 
  65         selector = Selector.open();
  66         sk = sc.register(selector, SelectionKey.OP_CONNECT);
  67         if (sc.connect(isa)) {
  68             System.err.println("Connected immediately!");
  69             sc.close();
  70             selector.close();
  71             return;
  72         } else {
  73             ByteBuffer buf = ByteBuffer.allocateDirect(100);
  74             buf.asCharBuffer().put(new String(
  75                 "The quick brown fox jumped over the lazy dog."
  76                 ).toCharArray());
  77             buf.flip();
  78             long startTime = System.currentTimeMillis();
  79             while(true) {
  80                 selector.select(INCREMENTAL_DELAY);
  81                 Set selectedKeys = selector.selectedKeys();