J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2000 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 4158021 |
| 27 | * @summary cannot distinguish Thread.interrupt and Socket.setSoTimeout exceptions |
| 28 | */ |
| 29 | |
| 30 | import java.net.*; |
| 31 | import java.io.*; |
| 32 | |
| 33 | public class SocketTimeout { |
| 34 | public static void main(String args[]) throws Exception { |
| 35 | InetAddress sin = null; |
| 36 | Socket soc = null,soc1 = null; |
| 37 | InputStream is = null; |
| 38 | OutputStream os = null; |
| 39 | ServerSocket srv = null; |
| 40 | int port = 0; |
| 41 | int tout = 1000; |
| 42 | |
| 43 | sin = InetAddress.getLocalHost(); |
| 44 | srv = new ServerSocket(port); |
| 45 | port = srv.getLocalPort(); |
| 46 | soc = new Socket(sin, port); |
| 47 | soc1 = srv.accept(); |
| 48 | soc.setSoTimeout(tout); |
| 49 | srv.setSoTimeout(tout); |
| 50 | |
| 51 | try { |
| 52 | is = soc.getInputStream(); |
| 53 | os = soc1.getOutputStream(); |
| 54 | is.read(); |
| 55 | } catch(InterruptedIOException e) { |
| 56 | try { |
| 57 | if (! (e instanceof java.net.SocketTimeoutException)) |
| 58 | throw new Exception ("Wrong exception class thrown"); |
| 59 | } catch(NoClassDefFoundError e1) { |
| 60 | throw new Exception ("SocketTimeoutException: not found"); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | // now check accept |
| 65 | |
| 66 | try { |
| 67 | srv.accept (); |
| 68 | } catch(InterruptedIOException e) { |
| 69 | try { |
| 70 | if (! (e instanceof java.net.SocketTimeoutException)) |
| 71 | throw new Exception ("Wrong exception class thrown"); |
| 72 | } catch(NoClassDefFoundError e1) { |
| 73 | throw new Exception ("SocketTimeoutException: not found"); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | // Now check DatagramSocket.receive() |
| 78 | |
| 79 | DatagramSocket dg = new DatagramSocket (); |
| 80 | dg.setSoTimeout (tout); |
| 81 | |
| 82 | try { |
| 83 | dg.receive (new DatagramPacket (new byte [64], 64)); |
| 84 | } catch(InterruptedIOException e) { |
| 85 | try { |
| 86 | if (! (e instanceof java.net.SocketTimeoutException)) |
| 87 | throw new Exception ("Wrong exception class thrown"); |
| 88 | } catch(NoClassDefFoundError e1) { |
| 89 | throw new Exception ("SocketTimeoutException: not found"); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | soc.close(); |
| 94 | soc1.close(); |
| 95 | srv.close(); |
| 96 | dg.close(); |
| 97 | } |
| 98 | } |