1 /* |
1 /* |
2 * Copyright (c) 2007, 2011, Oracle and/or its affiliates. All rights reserved. |
2 * Copyright (c) 2007, 2016, Oracle and/or its affiliates. All rights reserved. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 * |
4 * |
5 * This code is free software; you can redistribute it and/or modify it |
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 |
6 * under the terms of the GNU General Public License version 2 only, as |
7 * published by the Free Software Foundation. |
7 * published by the Free Software Foundation. |
48 public static void main(String[] args) throws IOException { |
48 public static void main(String[] args) throws IOException { |
49 DatagramChannel dc = DatagramChannel.open(); |
49 DatagramChannel dc = DatagramChannel.open(); |
50 |
50 |
51 // check supported options |
51 // check supported options |
52 Set<SocketOption<?>> options = dc.supportedOptions(); |
52 Set<SocketOption<?>> options = dc.supportedOptions(); |
53 List<? extends SocketOption<?>> expected = Arrays.asList(SO_SNDBUF, SO_RCVBUF, |
53 boolean reuseport = options.contains(SO_REUSEPORT); |
54 SO_REUSEADDR, SO_BROADCAST, IP_TOS, IP_MULTICAST_IF, IP_MULTICAST_TTL, |
54 List<? extends SocketOption<?>> expected; |
55 IP_MULTICAST_LOOP); |
55 if (reuseport) { |
|
56 expected = Arrays.asList(SO_SNDBUF, SO_RCVBUF, |
|
57 SO_REUSEADDR, SO_REUSEPORT, SO_BROADCAST, IP_TOS, IP_MULTICAST_IF, |
|
58 IP_MULTICAST_TTL, IP_MULTICAST_LOOP); |
|
59 } else { |
|
60 expected = Arrays.asList(SO_SNDBUF, SO_RCVBUF, |
|
61 SO_REUSEADDR, SO_BROADCAST, IP_TOS, IP_MULTICAST_IF, IP_MULTICAST_TTL, |
|
62 IP_MULTICAST_LOOP); |
|
63 } |
56 for (SocketOption opt: expected) { |
64 for (SocketOption opt: expected) { |
57 if (!options.contains(opt)) |
65 if (!options.contains(opt)) |
58 throw new RuntimeException(opt.name() + " should be supported"); |
66 throw new RuntimeException(opt.name() + " should be supported"); |
59 } |
67 } |
60 |
68 |
81 throw new RuntimeException("setOption caused SO_RCVBUF to decrease"); |
89 throw new RuntimeException("setOption caused SO_RCVBUF to decrease"); |
82 dc.setOption(SO_REUSEADDR, true); |
90 dc.setOption(SO_REUSEADDR, true); |
83 checkOption(dc, SO_REUSEADDR, true); |
91 checkOption(dc, SO_REUSEADDR, true); |
84 dc.setOption(SO_REUSEADDR, false); |
92 dc.setOption(SO_REUSEADDR, false); |
85 checkOption(dc, SO_REUSEADDR, false); |
93 checkOption(dc, SO_REUSEADDR, false); |
86 |
94 if (reuseport) { |
|
95 dc.setOption(SO_REUSEPORT, true); |
|
96 checkOption(dc, SO_REUSEPORT, true); |
|
97 dc.setOption(SO_REUSEPORT, false); |
|
98 checkOption(dc, SO_REUSEPORT, false); |
|
99 } |
87 // bind socket |
100 // bind socket |
88 dc.bind(new InetSocketAddress(0)); |
101 dc.bind(new InetSocketAddress(0)); |
89 |
102 |
90 // allow to change when bound |
103 // allow to change when bound |
91 dc.setOption(SO_BROADCAST, true); |
104 dc.setOption(SO_BROADCAST, true); |