Path: blob/master/test/jdk/java/net/SocketInputStream/SocketClosedException.java
41149 views
/*1* Copyright (c) 2002, 2019, 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/*24* @test25* @bug 468155626* @library /test/lib27* @summary Wrong text if a read is performed on a socket after it28* has been closed29* @run main SocketClosedException30* @run main/othervm -Djava.net.preferIPv4Stack=true SocketClosedException31*/3233import java.io.*;34import java.net.*;35import jdk.test.lib.net.IPSupport;3637public class SocketClosedException {38static void doServerSide() throws Exception {39try {40Socket socket = serverSocket.accept();4142OutputStream os = socket.getOutputStream();4344os.write(85);45os.flush();46socket.close();47} finally {48serverSocket.close();49}50}5152static void doClientSide(int port) throws Exception {53InetAddress loopback = InetAddress.getLoopbackAddress();54Socket socket = new Socket(loopback, port);55InputStream is = socket.getInputStream();5657is.read();58socket.close();59is.read();60}6162static ServerSocket serverSocket;63static Exception serverException = null;6465public static void main(String[] args) throws Exception {66IPSupport.throwSkippedExceptionIfNonOperational();67InetAddress loopback = InetAddress.getLoopbackAddress();68serverSocket = new ServerSocket();69serverSocket.bind(new InetSocketAddress(loopback, 0));70startServer();71try {72doClientSide(serverSocket.getLocalPort());73} catch (SocketException e) {74if (!e.getMessage().equalsIgnoreCase("Socket closed")) {75throw new Exception("Received a wrong exception message: " +76e.getMessage());77}78System.out.println("PASSED: received the right exception message: "79+ e.getMessage());80}81if (serverException != null) {82throw serverException;83}84}8586static void startServer() {87(new Thread() {88public void run() {89try {90doServerSide();91} catch (Exception e) {92e.printStackTrace();93}94}95}).start();96}97}9899100