Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/net/SocketInputStream/SocketTimeout.java
38811 views
/*1* Copyright (c) 2000, 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 415802126* @summary cannot distinguish Thread.interrupt and Socket.setSoTimeout exceptions27*/2829import java.net.*;30import java.io.*;3132public class SocketTimeout {33static final int TIMEOUT = 1000;3435public static void main(String args[]) throws Exception {36InetAddress sin = InetAddress.getLocalHost();37Socket soc = null,soc1 = null;38InputStream is = null;39ServerSocket srv = null;40int port = 0;4142srv = new ServerSocket(0);43port = srv.getLocalPort();44soc = new Socket(sin, port);45soc1 = srv.accept();46soc.setSoTimeout(TIMEOUT);47srv.setSoTimeout(TIMEOUT);4849try {50is = soc.getInputStream();51is.read();52} catch(InterruptedIOException e) {53try {54if (! (e instanceof java.net.SocketTimeoutException))55throw new Exception ("Wrong exception class thrown");56} catch(NoClassDefFoundError e1) {57throw new Exception ("SocketTimeoutException: not found");58}59} finally {60soc.close();61soc1.close();62}6364// now check accept6566try {67srv.accept ();68} catch(InterruptedIOException e) {69try {70if (! (e instanceof java.net.SocketTimeoutException))71throw new Exception ("Wrong exception class thrown");72} catch(NoClassDefFoundError e1) {73throw new Exception ("SocketTimeoutException: not found");74}75} finally {76srv.close();77}7879// Now check DatagramSocket.receive()8081DatagramSocket dg = new DatagramSocket ();82dg.setSoTimeout (TIMEOUT);8384try {85dg.receive (new DatagramPacket (new byte [64], 64));86} catch(InterruptedIOException e) {87try {88if (! (e instanceof java.net.SocketTimeoutException))89throw new Exception ("Wrong exception class thrown");90} catch(NoClassDefFoundError e1) {91throw new Exception ("SocketTimeoutException: not found");92}93} finally {94dg.close();95}96}97}9899100