blob: 98576285efcab4262b67b67dcda577aa8f8ce097 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2002-2003 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any 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
36 /*
37 * Is the server ready to serve?
38 */
39 volatile static boolean serverReady = false;
40
41 /*
42 * Define the server side of the test.
43 *
44 * If the server prematurely exits, serverReady will be set to true
45 * to avoid infinite hangs.
46 */
47 static void doServerSide() throws Exception {
48 ServerSocket serverSocket = new ServerSocket(serverPort);
49 serverPort = serverSocket.getLocalPort();
50
51 /*
52 * Signal Client, we're ready for a connect.
53 */
54 serverReady = true;
55
56 Socket socket = serverSocket.accept();
57
58 InputStream is = socket.getInputStream();
59 OutputStream os = socket.getOutputStream();
60
61 os.write(85);
62 os.flush();
63 socket.close();
64 }
65
66 /*
67 * Define the client side of the test.
68 *
69 * If the server prematurely exits, serverReady will be set to true
70 * to avoid infinite hangs.
71 */
72 static void doClientSide() throws Exception {
73
74 /*
75 * Wait for server to get started.
76 */
77 while (!serverReady) {
78 Thread.sleep(5000);
79 }
80
81 Socket socket = new Socket("localhost", serverPort);
82 InputStream is = socket.getInputStream();
83 OutputStream os = socket.getOutputStream();
84
85 int read = is.read();
86 socket.close();
87 read = is.read();
88 }
89
90 static int serverPort = 0;
91 static Exception serverException = null;
92
93 public static void main(String[] args) throws Exception {
94 startServer();
95 try {
96 doClientSide();
97 } catch (SocketException e) {
98 if (!e.getMessage().equalsIgnoreCase("Socket closed")) {
99 throw new Exception("Received a wrong exception message: " +
100 e.getMessage());
101 }
102 System.out.println("PASSED: received the right exception message: "
103 + e.getMessage());
104 }
105 if (serverException != null) {
106 throw serverException;
107 }
108 }
109
110 static void startServer() {
111 Thread serverThread = new Thread() {
112 public void run() {
113 try {
114 doServerSide();
115 } catch (Exception e) {
116 /*
117 * server thread just died.
118 * Release the client, if not active already...
119 */
120 System.err.println("Server died...");
121 serverReady = true;
122 serverException = e;
123 }
124 }
125 };
126 serverThread.start();
127 }
128}