J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2001 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 21 | * have any 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 | if (!bb.equals(bb2)) |
| 87 | throw new Exception("Echoed bytes incorrect: Sent " |
| 88 | + bb + ", got " + bb2); |
| 89 | sc.close(); |
| 90 | } |
| 91 | |
| 92 | } |