1/*
2 * Copyright (c) 2001, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24/*
25 * @test
26 * @bug 4431020
27 * @summary On Windows 2000 we observed behaviour that reflects the underlying
28 * implementation :-
29 *   1. PortUnreachableException throw per underlying reset
30 *   2. No PUE throw for DatagramSocket.send
31 */
32import java.net.*;
33
34public class Concurrent implements Runnable {
35
36    DatagramSocket s;
37
38    public void run() {
39        try {
40            byte b[] = new byte[512];
41            DatagramPacket p = new DatagramPacket(b, b.length);
42
43            int pue_count = 0;
44            while (true) {
45                try {
46                    System.out.println("receive...");
47                    s.receive(p);
48                } catch (PortUnreachableException pue) {
49                    System.out.println("receive threw PortUnreachableException");
50                    pue_count++;
51                }
52                System.out.println("receiver sleeping");
53                Thread.currentThread().sleep(100*pue_count);
54            }
55
56        } catch (Exception e) { }
57    }
58
59    Concurrent(InetAddress ia, int port) throws Exception {
60
61        System.out.println("");
62        System.out.println("***");
63        System.out.println("Test Description:");
64        System.out.println("    - Block reader thread on receive");
65        System.out.println("    - Send datagrams to bad destination with wee pauses");
66        System.out.println("    - Observe which thread gets the PUE");
67        System.out.println("");
68
69        /*
70         * Create the datagram and connect it to destination
71         */
72        s = new DatagramSocket();
73        s.connect(ia, port);
74        s.setSoTimeout(60000);
75
76        /*
77         * Start the reader thread
78         */
79        Thread thr = new Thread(this);
80        thr.start();
81        Thread.currentThread().sleep(2000);
82
83        byte b[] = new byte[512];
84        DatagramPacket p = new DatagramPacket(b, b.length);
85
86        /*
87         * Send a bunch of packets to the destination
88         */
89        for (int i=0; i<10; i++) {
90            try {
91                System.out.println("Sending...");
92                s.send(p);
93            } catch (PortUnreachableException e) {
94                System.out.println("send threw PortUnreachableException");
95            }
96            Thread.currentThread().sleep(100);
97        }
98
99        /*
100         * Give time for ICMP port unreachables to return
101         */
102        Thread.currentThread().sleep(5000);
103
104        s.close();
105    }
106
107
108    public static void main(String args[]) throws Exception {
109
110        InetAddress ia;
111        int port;
112        if (args.length >= 2) {
113            ia = InetAddress.getByName(args[0]);
114            port = Integer.parseInt(args[1]);
115        } else {
116            ia = InetAddress.getLocalHost();
117            DatagramSocket s1 = new DatagramSocket();
118            port = s1.getLocalPort();
119            s1.close();
120        }
121
122        new Concurrent(ia, port);
123    }
124
125}
126