1152
|
1 |
/*
|
|
2 |
* Copyright 2007-2008 Sun Microsystems, Inc. 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. Sun designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Sun in the LICENSE file that accompanied this code.
|
|
10 |
*
|
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
15 |
* accompanied this code).
|
|
16 |
*
|
|
17 |
* You should have received a copy of the GNU General Public License version
|
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
20 |
*
|
|
21 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
23 |
* have any questions.
|
|
24 |
*/
|
|
25 |
|
|
26 |
package java.net;
|
|
27 |
|
|
28 |
/**
|
|
29 |
* Defines the <em>standard</em> socket options.
|
|
30 |
*
|
|
31 |
* <p> The {@link SocketOption#name name} of each socket option defined by this
|
|
32 |
* class is its field name.
|
|
33 |
*
|
|
34 |
* <p> In this release, the socket options defined here are used by {@link
|
|
35 |
* java.nio.channels.NetworkChannel network} channels in the {@link
|
|
36 |
* java.nio.channels channels} package.
|
|
37 |
*
|
|
38 |
* @since 1.7
|
|
39 |
*/
|
|
40 |
|
|
41 |
public final class StandardSocketOption {
|
|
42 |
private StandardSocketOption() { }
|
|
43 |
|
|
44 |
// -- SOL_SOCKET --
|
|
45 |
|
|
46 |
/**
|
|
47 |
* Allow transmission of broadcast datagrams.
|
|
48 |
*
|
|
49 |
* <p> The value of this socket option is a {@code Boolean} that represents
|
|
50 |
* whether the option is enabled or disabled. The option is specific to
|
|
51 |
* datagram-oriented sockets sending to {@link java.net.Inet4Address IPv4}
|
|
52 |
* broadcast addresses. When the socket option is enabled then the socket
|
|
53 |
* can be used to send <em>broadcast datagrams</em>.
|
|
54 |
*
|
|
55 |
* <p> The initial value of this socket option is {@code FALSE}. The socket
|
|
56 |
* option may be enabled or disabled at any time. Some operating systems may
|
|
57 |
* require that the Java virtual machine be started with implementation
|
|
58 |
* specific privileges to enable this option or send broadcast datagrams.
|
|
59 |
*
|
|
60 |
* @see <a href="http://www.ietf.org/rfc/rfc919.txt">RFC 929:
|
|
61 |
* Broadcasting Internet Datagrams</a>
|
|
62 |
*/
|
|
63 |
public static final SocketOption<Boolean> SO_BROADCAST =
|
|
64 |
new StdSocketOption<Boolean>("SO_BROADCAST", Boolean.class);
|
|
65 |
|
|
66 |
/**
|
|
67 |
* Keep connection alive.
|
|
68 |
*
|
|
69 |
* <p> The value of this socket option is a {@code Boolean} that represents
|
|
70 |
* whether the option is enabled or disabled. When the {@code SO_KEEPALIVE}
|
|
71 |
* option is enabled the operating system may use a <em>keep-alive</em>
|
|
72 |
* mechanism to periodically probe the other end of a connection when the
|
|
73 |
* connection is otherwise idle. The exact semantics of the keep alive
|
|
74 |
* mechanism is system dependent and therefore unspecified.
|
|
75 |
*
|
|
76 |
* <p> The initial value of this socket option is {@code FALSE}. The socket
|
|
77 |
* option may be enabled or disabled at any time.
|
|
78 |
*
|
|
79 |
* @see <a href="http://www.ietf.org/rfc/rfc1122.txt">RFC 1122
|
|
80 |
* Requirements for Internet Hosts -- Communication Layers</a>
|
|
81 |
*/
|
|
82 |
public static final SocketOption<Boolean> SO_KEEPALIVE =
|
|
83 |
new StdSocketOption<Boolean>("SO_KEEPALIVE", Boolean.class);
|
|
84 |
|
|
85 |
/**
|
|
86 |
* The size of the socket send buffer.
|
|
87 |
*
|
|
88 |
* <p> The value of this socket option is an {@code Integer} that is the
|
|
89 |
* size of the socket send buffer in bytes. The socket send buffer is an
|
|
90 |
* output buffer used by the networking implementation. It may need to be
|
|
91 |
* increased for high-volume connections. The value of the socket option is
|
|
92 |
* a <em>hint</em> to the implementation to size the buffer and the actual
|
|
93 |
* size may differ. The socket option can be queried to retrieve the actual
|
|
94 |
* size.
|
|
95 |
*
|
|
96 |
* <p> For datagram-oriented sockets, the size of the send buffer may limit
|
|
97 |
* the size of the datagrams that may be sent by the socket. Whether
|
|
98 |
* datagrams larger than the buffer size are sent or discarded is system
|
|
99 |
* dependent.
|
|
100 |
*
|
|
101 |
* <p> The initial/default size of the socket send buffer and the range of
|
|
102 |
* allowable values is system dependent although a negative size is not
|
|
103 |
* allowed. An attempt to set the socket send buffer to larger than its
|
|
104 |
* maximum size causes it to be set to its maximum size.
|
|
105 |
*
|
|
106 |
* <p> An implementation allows this socket option to be set before the
|
|
107 |
* socket is bound or connected. Whether an implementation allows the
|
|
108 |
* socket send buffer to be changed after the socket is bound is system
|
|
109 |
* dependent.
|
|
110 |
*/
|
|
111 |
public static final SocketOption<Integer> SO_SNDBUF =
|
|
112 |
new StdSocketOption<Integer>("SO_SNDBUF", Integer.class);
|
|
113 |
|
|
114 |
|
|
115 |
/**
|
|
116 |
* The size of the socket receive buffer.
|
|
117 |
*
|
|
118 |
* <p> The value of this socket option is an {@code Integer} that is the
|
|
119 |
* size of the socket receive buffer in bytes. The socket receive buffer is
|
|
120 |
* an input buffer used by the networking implementation. It may need to be
|
|
121 |
* increased for high-volume connections or decreased to limit the possible
|
|
122 |
* backlog of incoming data. The value of the socket option is a
|
|
123 |
* <em>hint</em> to the implementation to size the buffer and the actual
|
|
124 |
* size may differ.
|
|
125 |
*
|
|
126 |
* <p> For datagram-oriented sockets, the size of the receive buffer may
|
|
127 |
* limit the size of the datagrams that can be received. Whether datagrams
|
|
128 |
* larger than the buffer size can be received is system dependent.
|
|
129 |
* Increasing the socket receive buffer may be important for cases where
|
|
130 |
* datagrams arrive in bursts faster than they can be processed.
|
|
131 |
*
|
|
132 |
* <p> In the case of stream-oriented sockets and the TCP/IP protocol, the
|
|
133 |
* size of the socket receive buffer may be used when advertising the size
|
|
134 |
* of the TCP receive window to the remote peer.
|
|
135 |
*
|
|
136 |
* <p> The initial/default size of the socket receive buffer and the range
|
|
137 |
* of allowable values is system dependent although a negative size is not
|
|
138 |
* allowed. An attempt to set the socket receive buffer to larger than its
|
|
139 |
* maximum size causes it to be set to its maximum size.
|
|
140 |
*
|
|
141 |
* <p> An implementation allows this socket option to be set before the
|
|
142 |
* socket is bound or connected. Whether an implementation allows the
|
|
143 |
* socket receive buffer to be changed after the socket is bound is system
|
|
144 |
* dependent.
|
|
145 |
*
|
|
146 |
* @see <a href="http://www.ietf.org/rfc/rfc1323.txt">RFC 1323: TCP
|
|
147 |
* Extensions for High Performance</a>
|
|
148 |
*/
|
|
149 |
public static final SocketOption<Integer> SO_RCVBUF =
|
|
150 |
new StdSocketOption<Integer>("SO_RCVBUF", Integer.class);
|
|
151 |
|
|
152 |
/**
|
|
153 |
* Re-use address.
|
|
154 |
*
|
|
155 |
* <p> The value of this socket option is a {@code Boolean} that represents
|
|
156 |
* whether the option is enabled or disabled. The exact semantics of this
|
|
157 |
* socket option are socket type and system dependent.
|
|
158 |
*
|
|
159 |
* <p> In the case of stream-oriented sockets, this socket option will
|
|
160 |
* usually determine whether the socket can be bound to a socket address
|
|
161 |
* when a previous connection involving that socket address is in the
|
|
162 |
* <em>TIME_WAIT</em> state. On implementations where the semantics differ,
|
|
163 |
* and the socket option is not required to be enabled in order to bind the
|
|
164 |
* socket when a previous connection is in this state, then the
|
|
165 |
* implementation may choose to ignore this option.
|
|
166 |
*
|
|
167 |
* <p> For datagram-oriented sockets the socket option is used to allow
|
|
168 |
* multiple programs bind to the same address. This option should be enabled
|
|
169 |
* when the socket is to be used for Internet Protocol (IP) multicasting.
|
|
170 |
*
|
|
171 |
* <p> An implementation allows this socket option to be set before the
|
|
172 |
* socket is bound or connected. Changing the value of this socket option
|
|
173 |
* after the socket is bound has no effect. The default value of this
|
|
174 |
* socket option is system dependent.
|
|
175 |
*
|
|
176 |
* @see <a href="http://www.ietf.org/rfc/rfc793.txt">RFC 793: Transmission
|
|
177 |
* Control Protocol</a>
|
|
178 |
*/
|
|
179 |
public static final SocketOption<Boolean> SO_REUSEADDR =
|
|
180 |
new StdSocketOption<Boolean>("SO_REUSEADDR", Boolean.class);
|
|
181 |
|
|
182 |
/**
|
|
183 |
* Linger on close if data is present.
|
|
184 |
*
|
|
185 |
* <p> The value of this socket option is an {@code Integer} that controls
|
|
186 |
* the action taken when unsent data is queued on the socket and a method
|
|
187 |
* to close the socket is invoked. If the value of the socket option is zero
|
|
188 |
* or greater, then it represents a timeout value, in seconds, known as the
|
|
189 |
* <em>linger interval</em>. The linger interval is the timeout for the
|
|
190 |
* {@code close} method to block while the operating system attempts to
|
|
191 |
* transmit the unsent data or it decides that it is unable to transmit the
|
|
192 |
* data. If the value of the socket option is less than zero then the option
|
|
193 |
* is disabled. In that case the {@code close} method does not wait until
|
|
194 |
* unsent data is transmitted; if possible the operating system will transmit
|
|
195 |
* any unsent data before the connection is closed.
|
|
196 |
*
|
|
197 |
* <p> This socket option is intended for use with sockets that are configured
|
|
198 |
* in {@link java.nio.channels.SelectableChannel#isBlocking() blocking} mode
|
|
199 |
* only. The behavior of the {@code close} method when this option is
|
|
200 |
* enabled on a non-blocking socket is not defined.
|
|
201 |
*
|
|
202 |
* <p> The initial value of this socket option is a negative value, meaning
|
|
203 |
* that the option is disabled. The option may be enabled, or the linger
|
|
204 |
* interval changed, at any time. The maximum value of the linger interval
|
|
205 |
* is system dependent. Setting the linger interval to a value that is
|
|
206 |
* greater than its maximum value causes the linger interval to be set to
|
|
207 |
* its maximum value.
|
|
208 |
*/
|
|
209 |
public static final SocketOption<Integer> SO_LINGER =
|
|
210 |
new StdSocketOption<Integer>("SO_LINGER", Integer.class);
|
|
211 |
|
|
212 |
|
|
213 |
// -- IPPROTO_IP --
|
|
214 |
|
|
215 |
/**
|
|
216 |
* The Type of Service (ToS) octet in the Internet Protocol (IP) header.
|
|
217 |
*
|
|
218 |
* <p> The value of this socket option is an {@code Integer}, the least
|
|
219 |
* significant 8 bits of which represents the value of the ToS octet in IP
|
|
220 |
* packets sent by sockets to an {@link StandardProtocolFamily#INET IPv4}
|
|
221 |
* socket. The interpretation of the ToS octet is network specific and
|
|
222 |
* is not defined by this class. Further information on the ToS octet can be
|
|
223 |
* found in <a href="http://www.ietf.org/rfc/rfc1349.txt">RFC 1349</a>
|
|
224 |
* and <a href="http://www.ietf.org/rfc/rfc2474.txt">RFC 2474</a>. The
|
|
225 |
* value of the socket option is a <em>hint</em>. An implementation may
|
|
226 |
* ignore the value, or ignore specific values.
|
|
227 |
*
|
|
228 |
* <p> The initial/default value of the TOS field in the ToS octet is
|
|
229 |
* implementation specific but will typically be {@code 0}. For
|
|
230 |
* datagram-oriented sockets the option may be configured at any time after
|
|
231 |
* the socket has been bound. The new value of the octet is used when sending
|
|
232 |
* subsequent datagrams. It is system dependent whether this option can be
|
|
233 |
* queried or changed prior to binding the socket.
|
|
234 |
*
|
|
235 |
* <p> The behavior of this socket option on a stream-oriented socket, or an
|
|
236 |
* {@link StandardProtocolFamily#INET6 IPv6} socket, is not defined in this
|
|
237 |
* release.
|
|
238 |
*/
|
|
239 |
public static final SocketOption<Integer> IP_TOS =
|
|
240 |
new StdSocketOption<Integer>("IP_TOS", Integer.class);
|
|
241 |
|
|
242 |
/**
|
|
243 |
* The network interface for Internet Protocol (IP) multicast datagrams.
|
|
244 |
*
|
|
245 |
* <p> The value of this socket option is a {@link NetworkInterface} that
|
|
246 |
* represents the outgoing interface for multicast datagrams sent by the
|
|
247 |
* datagram-oriented socket. For {@link StandardProtocolFamily#INET6 IPv6}
|
|
248 |
* sockets then it is system dependent whether setting this option also
|
|
249 |
* sets the outgoing interface for multlicast datagrams sent to IPv4
|
|
250 |
* addresses.
|
|
251 |
*
|
|
252 |
* <p> The initial/default value of this socket option may be {@code null}
|
|
253 |
* to indicate that outgoing interface will be selected by the operating
|
|
254 |
* system, typically based on the network routing tables. An implementation
|
|
255 |
* allows this socket option to be set after the socket is bound. Whether
|
|
256 |
* the socket option can be queried or changed prior to binding the socket
|
|
257 |
* is system dependent.
|
|
258 |
*
|
|
259 |
* @see java.nio.channels.MulticastChannel
|
|
260 |
*/
|
|
261 |
public static final SocketOption<NetworkInterface> IP_MULTICAST_IF =
|
|
262 |
new StdSocketOption<NetworkInterface>("IP_MULTICAST_IF", NetworkInterface.class);
|
|
263 |
|
|
264 |
/**
|
|
265 |
* The <em>time-to-live</em> for Internet Protocol (IP) multicast datagrams.
|
|
266 |
*
|
|
267 |
* <p> The value of this socket option is an {@code Integer} in the range
|
|
268 |
* <tt>0 <= value <= 255</tt>. It is used to control
|
|
269 |
* the scope of multicast datagrams sent by the datagram-oriented socket.
|
|
270 |
* In the case of an {@link StandardProtocolFamily#INET IPv4} socket
|
|
271 |
* the option is the time-to-live (TTL) on multicast datagrams sent by the
|
|
272 |
* socket. Datagrams with a TTL of zero are not transmitted on the network
|
|
273 |
* but may be delivered locally. In the case of an {@link
|
|
274 |
* StandardProtocolFamily#INET6 IPv6} socket the option is the
|
|
275 |
* <em>hop limit</em> which is number of <em>hops</em> that the datagram can
|
|
276 |
* pass through before expiring on the network. For IPv6 sockets it is
|
|
277 |
* system dependent whether the option also sets the <em>time-to-live</em>
|
|
278 |
* on multicast datagrams sent to IPv4 addresses.
|
|
279 |
*
|
|
280 |
* <p> The initial/default value of the time-to-live setting is typically
|
|
281 |
* {@code 1}. An implementation allows this socket option to be set after
|
|
282 |
* the socket is bound. Whether the socket option can be queried or changed
|
|
283 |
* prior to binding the socket is system dependent.
|
|
284 |
*
|
|
285 |
* @see java.nio.channels.MulticastChannel
|
|
286 |
*/
|
|
287 |
public static final SocketOption<Integer> IP_MULTICAST_TTL =
|
|
288 |
new StdSocketOption<Integer>("IP_MULTICAST_TTL", Integer.class);
|
|
289 |
|
|
290 |
/**
|
|
291 |
* Loopback for Internet Protocol (IP) multicast datagrams.
|
|
292 |
*
|
|
293 |
* <p> The value of this socket option is a {@code Boolean} that controls
|
|
294 |
* the <em>loopback</em> of multicast datagrams. The value of the socket
|
|
295 |
* option represents if the option is enabled or disabled.
|
|
296 |
*
|
|
297 |
* <p> The exact semantics of this socket options are system dependent.
|
|
298 |
* In particular, it is system dependent whether the loopback applies to
|
|
299 |
* multicast datagrams sent from the socket or received by the socket.
|
|
300 |
* For {@link StandardProtocolFamily#INET6 IPv6} sockets then it is
|
|
301 |
* system dependent whether the option also applies to multicast datagrams
|
|
302 |
* sent to IPv4 addresses.
|
|
303 |
*
|
|
304 |
* <p> The initial/default value of this socket option is {@code TRUE}. An
|
|
305 |
* implementation allows this socket option to be set after the socket is
|
|
306 |
* bound. Whether the socket option can be queried or changed prior to
|
|
307 |
* binding the socket is system dependent.
|
|
308 |
*
|
|
309 |
* @see java.nio.channels.MulticastChannel
|
|
310 |
*/
|
|
311 |
public static final SocketOption<Boolean> IP_MULTICAST_LOOP =
|
|
312 |
new StdSocketOption<Boolean>("IP_MULTICAST_LOOP", Boolean.class);
|
|
313 |
|
|
314 |
|
|
315 |
// -- IPPROTO_TCP --
|
|
316 |
|
|
317 |
/**
|
|
318 |
* Disable the Nagle algorithm.
|
|
319 |
*
|
|
320 |
* <p> The value of this socket option is a {@code Boolean} that represents
|
|
321 |
* whether the option is enabled or disabled. The socket option is specific to
|
|
322 |
* stream-oriented sockets using the TCP/IP protocol. TCP/IP uses an algorithm
|
|
323 |
* known as <em>The Nagle Algorithm</em> to coalesce short segments and
|
|
324 |
* improve network efficiency.
|
|
325 |
*
|
|
326 |
* <p> The default value of this socket option is {@code FALSE}. The
|
|
327 |
* socket option should only be enabled in cases where it is known that the
|
|
328 |
* coalescing impacts performance. The socket option may be enabled at any
|
|
329 |
* time. In other words, the Nagle Algorithm can be disabled. Once the option
|
|
330 |
* is enabled, it is system dependent whether it can be subsequently
|
|
331 |
* disabled. In that case, invoking the {@code setOption} method to disable
|
|
332 |
* the option has no effect.
|
|
333 |
*
|
|
334 |
* @see <a href="http://www.ietf.org/rfc/rfc1122.txt">RFC 1122:
|
|
335 |
* Requirements for Internet Hosts -- Communication Layers</a>
|
|
336 |
*/
|
|
337 |
public static final SocketOption<Boolean> TCP_NODELAY =
|
|
338 |
new StdSocketOption<Boolean>("TCP_NODELAY", Boolean.class);
|
|
339 |
|
|
340 |
|
|
341 |
private static class StdSocketOption<T> implements SocketOption<T> {
|
|
342 |
private final String name;
|
|
343 |
private final Class<T> type;
|
|
344 |
StdSocketOption(String name, Class<T> type) {
|
|
345 |
this.name = name;
|
|
346 |
this.type = type;
|
|
347 |
}
|
|
348 |
@Override public String name() { return name; }
|
|
349 |
@Override public Class<T> type() { return type; }
|
|
350 |
@Override public String toString() { return name; }
|
|
351 |
}
|
|
352 |
}
|