Path: blob/master/test/jdk/java/net/PortUnreachableException/Concurrent.java
41149 views
/*1* Copyright (c) 2001, 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 443102026* @summary On Windows 2000 we observed behaviour that reflects the underlying27* implementation :-28* 1. PortUnreachableException throw per underlying reset29* 2. No PUE throw for DatagramSocket.send30*/31import java.net.*;3233public class Concurrent implements Runnable {3435DatagramSocket s;3637public void run() {38try {39byte b[] = new byte[512];40DatagramPacket p = new DatagramPacket(b, b.length);4142int pue_count = 0;43while (true) {44try {45System.out.println("receive...");46s.receive(p);47} catch (PortUnreachableException pue) {48System.out.println("receive threw PortUnreachableException");49pue_count++;50}51System.out.println("receiver sleeping");52Thread.currentThread().sleep(100*pue_count);53}5455} catch (Exception e) { }56}5758Concurrent(InetAddress ia, int port) throws Exception {5960System.out.println("");61System.out.println("***");62System.out.println("Test Description:");63System.out.println(" - Block reader thread on receive");64System.out.println(" - Send datagrams to bad destination with wee pauses");65System.out.println(" - Observe which thread gets the PUE");66System.out.println("");6768/*69* Create the datagram and connect it to destination70*/71s = new DatagramSocket();72s.connect(ia, port);73s.setSoTimeout(60000);7475/*76* Start the reader thread77*/78Thread thr = new Thread(this);79thr.start();80Thread.currentThread().sleep(2000);8182byte b[] = new byte[512];83DatagramPacket p = new DatagramPacket(b, b.length);8485/*86* Send a bunch of packets to the destination87*/88for (int i=0; i<10; i++) {89try {90System.out.println("Sending...");91s.send(p);92} catch (PortUnreachableException e) {93System.out.println("send threw PortUnreachableException");94}95Thread.currentThread().sleep(100);96}9798/*99* Give time for ICMP port unreachables to return100*/101Thread.currentThread().sleep(5000);102103s.close();104}105106107public static void main(String args[]) throws Exception {108109InetAddress ia;110int port;111if (args.length >= 2) {112ia = InetAddress.getByName(args[0]);113port = Integer.parseInt(args[1]);114} else {115ia = InetAddress.getLocalHost();116DatagramSocket s1 = new DatagramSocket();117port = s1.getLocalPort();118s1.close();119}120121new Concurrent(ia, port);122}123124}125126127