1 /*
   2  * Copyright (c) 2000, 2001, 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 Unit test for socket channels
  26  * @library ..
  27  */
  28 
  29 import java.net.*;
  30 import java.nio.*;
  31 import java.nio.channels.*;
  32 import java.nio.charset.*;
  33 
  34 
  35 public class Basic {
  36 
  37     static java.io.PrintStream out = System.out;
  38 
  39     static final int DAYTIME_PORT = 13;
  40     static final String DAYTIME_HOST = TestUtil.HOST;
  41 
  42     static void test() throws Exception {
  43         InetSocketAddress isa
  44             = new InetSocketAddress(InetAddress.getByName(DAYTIME_HOST),
  45                                     DAYTIME_PORT);
  46         SocketChannel sc = SocketChannel.open(isa);
  47         out.println("opened: " + sc);
  48         /*
  49         out.println("opts:   " + sc.options());
  50         ((SocketOpts.IP.TCP)sc.options())
  51             .noDelay(true)
  52             .typeOfService(SocketOpts.IP.TOS_THROUGHPUT)
  53             .broadcast(true)
  54             .keepAlive(true)
  55             .linger(42)
  56             .outOfBandInline(true)
  57             .receiveBufferSize(128)
  58             .sendBufferSize(128)
  59             .reuseAddress(true);
  60         out.println("        " + sc.options());
  61         */
  62         // sc.connect(isa);
  63         out.println("connected: " + sc);
  64         ByteBuffer bb = ByteBuffer.allocateDirect(100);
  65         int n = sc.read(bb);
  66         bb.position(bb.position() - 2);         // Drop CRLF
  67         bb.flip();
  68         CharBuffer cb = Charset.forName("US-ASCII").newDecoder().decode(bb);
  69         out.println(isa + " says: \"" + cb + "\"");
  70         sc.socket().shutdownInput();
  71         out.println("ishut: " + sc);
  72         sc.socket().shutdownOutput();
  73         out.println("oshut: " + sc);
  74         sc.close();
  75         out.println("closed: " + sc);
  76     }
  77 
  78     public static void main(String[] args) throws Exception {
  79         test();
  80     }
  81 
  82 }