20 * or visit www.oracle.com if you need additional information or have any |
20 * or visit www.oracle.com if you need additional information or have any |
21 * questions. |
21 * questions. |
22 */ |
22 */ |
23 |
23 |
24 /* @test |
24 /* @test |
25 * @bug 4945514 |
25 * @bug 4945514 8042581 |
26 * @summary DatagramSocket should make handle not inherited |
26 * @summary DatagramSocket should make handle not inherited |
27 */ |
27 */ |
28 |
28 |
29 import java.net.*; |
29 import java.net.BindException; |
|
30 import java.net.DatagramSocket; |
|
31 import java.net.InetSocketAddress; |
30 |
32 |
31 public class InheritHandle { |
33 public class InheritHandle { |
|
34 private static final long SLEEPTIME_MS = 1000L; |
|
35 |
32 public static void main(String[] args) throws Exception { |
36 public static void main(String[] args) throws Exception { |
33 DatagramSocket sock = new DatagramSocket (0); |
37 int port; |
34 sock.setReuseAddress(true); |
38 try (DatagramSocket sock = new DatagramSocket(0);) { |
35 int port = sock.getLocalPort(); |
39 sock.setReuseAddress(true); |
|
40 port = sock.getLocalPort(); |
36 |
41 |
37 /** |
42 /** |
38 * spawn a child to check whether handle passed to it or not; |
43 * spawn a child to check whether handle passed to it or not; it |
39 * it shouldn't |
44 * shouldn't |
40 */ |
45 */ |
41 Runtime.getRuntime().exec ("sleep 10"); |
46 Runtime.getRuntime().exec("sleep 10"); |
|
47 } |
42 |
48 |
43 sock.close(); |
49 try (DatagramSocket sock = new DatagramSocket(null);) { |
44 sock = new DatagramSocket (null); |
50 sock.setReuseAddress(true); |
45 sock.setReuseAddress(true); |
51 int retries = 0; |
46 sock.bind(new InetSocketAddress(port)); |
52 boolean isWindows = System.getProperty("os.name").startsWith("Windows"); |
|
53 InetSocketAddress addr = new InetSocketAddress(port); |
|
54 while (true) { |
|
55 try { |
|
56 sock.bind(addr); |
|
57 break; |
|
58 } catch (BindException e) { |
|
59 if (isWindows && retries++ < 5) { |
|
60 Thread.sleep(SLEEPTIME_MS); |
|
61 System.out.println("BindException \"" + e.getMessage() + "\", retrying..."); |
|
62 continue; |
|
63 } else { |
|
64 throw e; |
|
65 } |
|
66 } |
|
67 } |
|
68 |
|
69 } |
47 } |
70 } |
48 } |
71 } |
|
72 |