Path: blob/master/test/jdk/java/net/SocketInputStream/SocketTimeout.java
41149 views
/*1* Copyright (c) 2000, 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 415802126* @library /test/lib27* @summary cannot distinguish Thread.interrupt and Socket.setSoTimeout exceptions28* @run main SocketTimeout29* @run main/othervm -Djava.net.preferIPv4Stack=true SocketTimeout30*/3132import java.net.*;33import java.io.*;34import jdk.test.lib.net.IPSupport;3536public class SocketTimeout {37static final int TIMEOUT = 1000;3839public static void main(String args[]) throws Exception {40IPSupport.throwSkippedExceptionIfNonOperational();41InetAddress sin = InetAddress.getLocalHost();42Socket soc = null,soc1 = null;43InputStream is = null;44ServerSocket srv = null;45int port = 0;4647srv = new ServerSocket();48srv.bind(new InetSocketAddress(sin, 0));49port = srv.getLocalPort();50soc = new Socket(sin, port);51soc1 = srv.accept();52soc.setSoTimeout(TIMEOUT);53srv.setSoTimeout(TIMEOUT);5455try {56is = soc.getInputStream();57is.read();58} catch(InterruptedIOException e) {59try {60if (! (e instanceof java.net.SocketTimeoutException))61throw new Exception ("Wrong exception class thrown");62} catch(NoClassDefFoundError e1) {63throw new Exception ("SocketTimeoutException: not found");64}65} finally {66soc.close();67soc1.close();68}6970// now check accept7172try {73srv.accept ();74} catch(InterruptedIOException e) {75try {76if (! (e instanceof java.net.SocketTimeoutException))77throw new Exception ("Wrong exception class thrown");78} catch(NoClassDefFoundError e1) {79throw new Exception ("SocketTimeoutException: not found");80}81} finally {82srv.close();83}8485// Now check DatagramSocket.receive()8687DatagramSocket dg = new DatagramSocket ();88dg.setSoTimeout (TIMEOUT);8990try {91dg.receive (new DatagramPacket (new byte [64], 64));92} catch(InterruptedIOException e) {93try {94if (! (e instanceof java.net.SocketTimeoutException))95throw new Exception ("Wrong exception class thrown");96} catch(NoClassDefFoundError e1) {97throw new Exception ("SocketTimeoutException: not found");98}99} finally {100dg.close();101}102}103}104105106