1/*
2 * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24/*
25 * @test
26 * @bug 4681556
27 * @summary Wrong text if a read is performed on a socket after it
28 *      has been closed
29 */
30
31import java.io.*;
32import java.net.*;
33
34public class SocketClosedException {
35    static void doServerSide() throws Exception {
36        try {
37            Socket socket = serverSocket.accept();
38
39            OutputStream os = socket.getOutputStream();
40
41            os.write(85);
42            os.flush();
43            socket.close();
44        } finally {
45            serverSocket.close();
46        }
47    }
48
49    static void doClientSide(int port) throws Exception {
50        Socket socket = new Socket("localhost", port);
51        InputStream is = socket.getInputStream();
52
53        is.read();
54        socket.close();
55        is.read();
56    }
57
58    static ServerSocket serverSocket;
59    static Exception serverException = null;
60
61    public static void main(String[] args) throws Exception {
62        serverSocket = new ServerSocket(0);
63        startServer();
64        try {
65            doClientSide(serverSocket.getLocalPort());
66        } catch (SocketException e) {
67            if (!e.getMessage().equalsIgnoreCase("Socket closed")) {
68                throw new Exception("Received a wrong exception message: " +
69                                        e.getMessage());
70            }
71            System.out.println("PASSED: received the right exception message: "
72                                        + e.getMessage());
73        }
74        if (serverException != null) {
75            throw serverException;
76        }
77    }
78
79    static void startServer() {
80        (new Thread() {
81            public void run() {
82                try {
83                    doServerSide();
84                } catch (Exception e) {
85                    e.printStackTrace();
86                }
87            }
88        }).start();
89    }
90}
91