1 /*
   2  * Copyright (c) 2000, 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 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 void test(TestClass.DayTimeServer daytimeServer) throws Exception {
  40         InetSocketAddress isa
  41             = new InetSocketAddress(daytimeServer.getAddress(),
  42                                     daytimeServer.getPort());
  43         SocketChannel sc = SocketChannel.open(isa);
  44         out.println("opened: " + sc);
  45         /*
  46         out.println("opts:   " + sc.options());
  47         ((SocketOpts.IP.TCP)sc.options())
  48             .noDelay(true)
  49             .typeOfService(SocketOpts.IP.TOS_THROUGHPUT)
  50             .broadcast(true)
  51             .keepAlive(true)
  52             .linger(42)
  53             .outOfBandInline(true)
  54             .receiveBufferSize(128)
  55             .sendBufferSize(128)
  56             .reuseAddress(true);
  57         out.println("        " + sc.options());
  58         */
  59         // sc.connect(isa);
  60         out.println("connected: " + sc);
  61         ByteBuffer bb = ByteBuffer.allocateDirect(100);
  62         int n = sc.read(bb);
  63         bb.position(bb.position() - 2);         // Drop CRLF
  64         bb.flip();
  65         CharBuffer cb = Charset.forName("US-ASCII").newDecoder().decode(bb);
  66         out.println(isa + " says: \"" + cb + "\"");
  67         sc.socket().shutdownInput();
  68         out.println("ishut: " + sc);
  69         sc.socket().shutdownOutput();
  70         out.println("oshut: " + sc);
  71         sc.close();
  72         out.println("closed: " + sc);
  73     }
  74 
  75     public static void main(String[] args) throws Exception {
  76         try (TestClass.DayTimeServer dayTimeServer
  77                 = TestClass.DayTimeServer.startNewServer(100)) {
  78             test(dayTimeServer);
  79         }
  80     }
  81 
  82 }