Path: blob/master/test/jdk/java/nio/channels/SocketChannel/Basic.java
41154 views
/*1* Copyright (c) 2000, 2018, 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 .. /test/lib26* @build jdk.test.lib.Utils TestServers27* @run main Basic28*/2930import java.net.*;31import java.nio.*;32import java.nio.channels.*;33import java.nio.charset.*;343536public class Basic {3738static java.io.PrintStream out = System.out;3940static void test(TestServers.DayTimeServer daytimeServer) throws Exception {41InetSocketAddress isa42= new InetSocketAddress(daytimeServer.getAddress(),43daytimeServer.getPort());44SocketChannel sc = SocketChannel.open(isa);45out.println("opened: " + sc);46/*47out.println("opts: " + sc.options());48((SocketOpts.IP.TCP)sc.options())49.noDelay(true)50.typeOfService(SocketOpts.IP.TOS_THROUGHPUT)51.broadcast(true)52.keepAlive(true)53.linger(42)54.outOfBandInline(true)55.receiveBufferSize(128)56.sendBufferSize(128)57.reuseAddress(true);58out.println(" " + sc.options());59*/60// sc.connect(isa);61out.println("connected: " + sc);62ByteBuffer bb = ByteBuffer.allocateDirect(100);63int n = sc.read(bb);64bb.position(bb.position() - 2); // Drop CRLF65bb.flip();66CharBuffer cb = Charset.forName("US-ASCII").newDecoder().decode(bb);67out.println(isa + " says: \"" + cb + "\"");68sc.socket().shutdownInput();69out.println("ishut: " + sc);70sc.socket().shutdownOutput();71out.println("oshut: " + sc);72sc.close();73out.println("closed: " + sc);74}7576public static void main(String[] args) throws Exception {77try (TestServers.DayTimeServer dayTimeServer78= TestServers.DayTimeServer.startNewServer(100)) {79test(dayTimeServer);80}81}8283}848586