Path: blob/master/test/jdk/java/nio/channels/SocketChannel/Hangup.java
41154 views
/*1* Copyright (c) 2002, 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* @bug 461716525* @summary Ensure that socket hangups are handled correctly26* @library ..27* @build TestUtil28* @run main Hangup29*/3031import java.io.*;32import java.net.*;33import java.nio.*;34import java.nio.channels.*;35import java.util.*;363738public class Hangup {3940static PrintStream log = System.err;41static int failures = 0;4243private static class Failure44extends RuntimeException45{4647Failure(String s) {48super(s);49}5051}5253static void doSelect(Selector sel, SelectionKey sk, int count)54throws IOException55{56int n = sel.select();57if (n != 1)58throw new Failure("Select returned zero");59Set sks = sel.selectedKeys();60if (sks.size() != 1)61throw new Failure("Wrong size for selected-key set: "62+ sks.size());63if (!sks.remove(sk))64throw new Failure("Key not in selected-key set");65log.println("S: Socket selected #" + count);66}6768static void dally() {69try {70Thread.sleep(100);71} catch (InterruptedException x) { }72}7374static void test(boolean writeFromClient, boolean readAfterClose)75throws IOException76{7778ServerSocketChannel ssc = null;79SocketChannel cl = null; // client end80SocketChannel sv = null; // server end81Selector sel = null;8283log.println();84log.println("Test: writeFromClient = " + writeFromClient85+ ", readAfterClose = " + readAfterClose);8687try {8889int ns = 0; // Number of selection operations done9091// Set up server socket92ssc = ServerSocketChannel.open();93SocketAddress sa = TestUtil.bindToRandomPort(ssc);94log.println("S: Listening on port "95+ ssc.socket().getLocalPort());9697// Connect client98cl = SocketChannel.open(sa);99log.println("C: Connected via port "100+ cl.socket().getLocalPort());101102// Accept client connection103sv = ssc.accept();104log.println("S: Client connection accepted");105106// Create selector and register server side107sel = Selector.open();108sv.configureBlocking(false);109SelectionKey sk = sv.register(sel, SelectionKey.OP_READ);110111ByteBuffer stuff = ByteBuffer.allocate(10);112int n;113114if (writeFromClient) {115116// Write from client, read from server117118stuff.clear();119if (cl.write(stuff) != stuff.capacity())120throw new Failure("Incorrect number of bytes written");121log.println("C: Wrote stuff");122dally();123124doSelect(sel, sk, ++ns);125126stuff.clear();127if (sv.read(stuff) != stuff.capacity())128throw new Failure("Wrong number of bytes read");129log.println("S: Read stuff");130}131132// Close client side133cl.close();134log.println("C: Socket closed");135dally();136137// Select again138doSelect(sel, sk, ++ns);139140if (readAfterClose) {141// Read from client after client has disconnected142stuff.clear();143if (sv.read(stuff) != -1)144throw new Failure("Wrong number of bytes read");145log.println("S: Read EOF");146}147148// Select a couple more times just to make sure we're doing149// the right thing150151doSelect(sel, sk, ++ns);152doSelect(sel, sk, ++ns);153154} finally {155if (ssc != null)156ssc.close();157if (cl != null)158cl.close();159if (sv != null)160sv.close();161if (sel != null)162sel.close();163}164165}166167public static void main(String[] args) throws IOException {168169for (boolean writeFromClient = false;; writeFromClient = true) {170for (boolean readAfterClose = false;; readAfterClose = true) {171try {172test(writeFromClient, readAfterClose);173} catch (Failure x) {174x.printStackTrace(log);175failures++;176}177if (readAfterClose)178break;179}180if (writeFromClient)181break;182}183184if (failures > 0) {185log.println();186throw new RuntimeException("Some tests failed");187}188189}190191}192193194