Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/net/SocketInputStream/SocketClosedException.java
38821 views
/*1* Copyright (c) 2002, 2010, 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* @summary Wrong text if a read is performed on a socket after it27* has been closed28*/2930import java.io.*;31import java.net.*;3233public class SocketClosedException {34static void doServerSide() throws Exception {35try {36Socket socket = serverSocket.accept();3738OutputStream os = socket.getOutputStream();3940os.write(85);41os.flush();42socket.close();43} finally {44serverSocket.close();45}46}4748static void doClientSide(int port) throws Exception {49Socket socket = new Socket("localhost", port);50InputStream is = socket.getInputStream();5152is.read();53socket.close();54is.read();55}5657static ServerSocket serverSocket;58static Exception serverException = null;5960public static void main(String[] args) throws Exception {61serverSocket = new ServerSocket(0);62startServer();63try {64doClientSide(serverSocket.getLocalPort());65} catch (SocketException e) {66if (!e.getMessage().equalsIgnoreCase("Socket closed")) {67throw new Exception("Received a wrong exception message: " +68e.getMessage());69}70System.out.println("PASSED: received the right exception message: "71+ e.getMessage());72}73if (serverException != null) {74throw serverException;75}76}7778static void startServer() {79(new Thread() {80public void run() {81try {82doServerSide();83} catch (Exception e) {84e.printStackTrace();85}86}87}).start();88}89}909192