test/java/nio/channels/Selector/BasicConnect.java

Print this page


   1 /*
   2  * Copyright (c) 2001, 2010, 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  * @summary Test nonblocking connect and finishConnect
  26  * @bug 4457776
  27  * @library ..
  28  */
  29 
  30 import java.io.*;
  31 import java.net.*;
  32 import java.nio.*;
  33 import java.nio.channels.*;
  34 import java.nio.channels.spi.SelectorProvider;
  35 import java.nio.charset.*;
  36 import java.util.*;
  37 
  38 
  39 /**
  40  * Typically there would be more than one channel registered to select
  41  * on, this test is just a very simple version with only one channel
  42  * registered for the connectSelector.
  43  */
  44 
  45 public class BasicConnect {
  46 
  47     static final int PORT = 7;          // echo
  48     static final String HOST = TestUtil.HOST;
  49 
  50     public static void main(String[] args) throws Exception {
  51         Selector connectSelector =
  52             SelectorProvider.provider().openSelector();
  53         InetSocketAddress isa
  54             = new InetSocketAddress(InetAddress.getByName(HOST), PORT);


  55         SocketChannel sc = SocketChannel.open();
  56         sc.configureBlocking(false);
  57         boolean result = sc.connect(isa);





  58         while (!result) {
  59             SelectionKey connectKey = sc.register(connectSelector,
  60                                                   SelectionKey.OP_CONNECT);
  61             int keysAdded = connectSelector.select();
  62             if (keysAdded > 0) {
  63                 Set readyKeys = connectSelector.selectedKeys();
  64                 Iterator i = readyKeys.iterator();
  65                 while (i.hasNext()) {
  66                     SelectionKey sk = (SelectionKey)i.next();
  67                     i.remove();
  68                     SocketChannel nextReady = (SocketChannel)sk.channel();
  69                     result = nextReady.finishConnect();
  70                     if (result)
  71                         sk.cancel();
  72                 }
  73             }
  74         }
  75 
  76         byte[] bs = new byte[] { (byte)0xca, (byte)0xfe,
  77                                  (byte)0xba, (byte)0xbe };
  78         ByteBuffer bb = ByteBuffer.wrap(bs);
  79         sc.configureBlocking(true);
  80         sc.write(bb);
  81         bb.rewind();
  82 
  83         ByteBuffer bb2 = ByteBuffer.allocateDirect(100);
  84         int n = sc.read(bb2);
  85         bb2.flip();
  86 
  87         sc.close();
  88         connectSelector.close();
  89 
  90         if (!bb.equals(bb2))
  91             throw new Exception("Echoed bytes incorrect: Sent "
  92                                 + bb + ", got " + bb2);
  93     }
  94 
  95 }
   1 /*
   2  * Copyright (c) 2001, 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  * @summary Test nonblocking connect and finishConnect
  26  * @bug 4457776
  27  * @library ..
  28  */
  29 

  30 import java.net.*;
  31 import java.nio.*;
  32 import java.nio.channels.*;
  33 import java.nio.channels.spi.SelectorProvider;

  34 import java.util.*;
  35 
  36 
  37 /**
  38  * Typically there would be more than one channel registered to select
  39  * on, this test is just a very simple version with only one channel
  40  * registered for the connectSelector.
  41  */
  42 
  43 public class BasicConnect {
  44 



  45     public static void main(String[] args) throws Exception {
  46         Selector connectSelector =
  47             SelectorProvider.provider().openSelector();
  48         try (TestUtil.EchoServer echoServer =
  49                         TestUtil.EchoServer.startNewServer(100)) {
  50             InetSocketAddress isa = new InetSocketAddress(echoServer.getAddress(),
  51                     echoServer.getPort());
  52             SocketChannel sc = SocketChannel.open();
  53             sc.configureBlocking(false);
  54             boolean result = sc.connect(isa);
  55             if (result) {
  56                 System.err.println("Socket immediately connected on "
  57                         + System.getProperty("os.name")
  58                         + ": " + sc);
  59             }
  60             while (!result) {
  61                 SelectionKey connectKey = sc.register(connectSelector,
  62                                                       SelectionKey.OP_CONNECT);
  63                 int keysAdded = connectSelector.select();
  64                 if (keysAdded > 0) {
  65                     Set readyKeys = connectSelector.selectedKeys();
  66                     Iterator i = readyKeys.iterator();
  67                     while (i.hasNext()) {
  68                         SelectionKey sk = (SelectionKey)i.next();
  69                         i.remove();
  70                         SocketChannel nextReady = (SocketChannel)sk.channel();
  71                         result = nextReady.finishConnect();
  72                         if (result)
  73                             sk.cancel();
  74                     }
  75                 }
  76             }
  77 
  78             byte[] bs = new byte[] { (byte)0xca, (byte)0xfe,
  79                                      (byte)0xba, (byte)0xbe };
  80             ByteBuffer bb = ByteBuffer.wrap(bs);
  81             sc.configureBlocking(true);
  82             sc.write(bb);
  83             bb.rewind();
  84 
  85             ByteBuffer bb2 = ByteBuffer.allocateDirect(100);
  86             int n = sc.read(bb2);
  87             bb2.flip();
  88 
  89             sc.close();
  90             connectSelector.close();
  91 
  92             if (!bb.equals(bb2))
  93                 throw new Exception("Echoed bytes incorrect: Sent "
  94                                     + bb + ", got " + bb2);
  95         } 
  96     }
  97 }