Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/nio/channels/SocketChannel/Basic.java
38828 views
/*1* Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24* @summary Unit test for socket channels25* @library ..26*/2728import java.net.*;29import java.nio.*;30import java.nio.channels.*;31import java.nio.charset.*;323334public class Basic {3536static java.io.PrintStream out = System.out;3738static void test(TestServers.DayTimeServer daytimeServer) throws Exception {39InetSocketAddress isa40= new InetSocketAddress(daytimeServer.getAddress(),41daytimeServer.getPort());42SocketChannel sc = SocketChannel.open(isa);43out.println("opened: " + sc);44/*45out.println("opts: " + sc.options());46((SocketOpts.IP.TCP)sc.options())47.noDelay(true)48.typeOfService(SocketOpts.IP.TOS_THROUGHPUT)49.broadcast(true)50.keepAlive(true)51.linger(42)52.outOfBandInline(true)53.receiveBufferSize(128)54.sendBufferSize(128)55.reuseAddress(true);56out.println(" " + sc.options());57*/58// sc.connect(isa);59out.println("connected: " + sc);60ByteBuffer bb = ByteBuffer.allocateDirect(100);61int n = sc.read(bb);62bb.position(bb.position() - 2); // Drop CRLF63bb.flip();64CharBuffer cb = Charset.forName("US-ASCII").newDecoder().decode(bb);65out.println(isa + " says: \"" + cb + "\"");66sc.socket().shutdownInput();67out.println("ishut: " + sc);68sc.socket().shutdownOutput();69out.println("oshut: " + sc);70sc.close();71out.println("closed: " + sc);72}7374public static void main(String[] args) throws Exception {75try (TestServers.DayTimeServer dayTimeServer76= TestServers.DayTimeServer.startNewServer(100)) {77test(dayTimeServer);78}79}8081}828384