24 */ |
24 */ |
25 |
25 |
26 package java.nio.channels; |
26 package java.nio.channels; |
27 |
27 |
28 import java.io.IOException; |
28 import java.io.IOException; |
|
29 import java.net.ProtocolFamily; |
|
30 import java.net.StandardProtocolFamily; |
29 import java.net.ServerSocket; |
31 import java.net.ServerSocket; |
30 import java.net.SocketOption; |
32 import java.net.SocketOption; |
31 import java.net.SocketAddress; |
33 import java.net.SocketAddress; |
32 import java.nio.channels.spi.AbstractSelectableChannel; |
34 import java.nio.channels.spi.AbstractSelectableChannel; |
33 import java.nio.channels.spi.SelectorProvider; |
35 import java.nio.channels.spi.SelectorProvider; |
34 |
36 |
35 /** |
37 /** |
36 * A selectable channel for stream-oriented listening sockets. |
38 * A selectable channel for stream-oriented listening sockets. |
37 * |
39 * |
38 * <p> A server-socket channel is created by invoking the {@link #open() open} |
40 * <p> A server-socket channel is created by invoking the open methods of this class. |
39 * method of this class. It is not possible to create a channel for an arbitrary, |
41 * It is not possible to create a channel for an arbitrary, |
40 * pre-existing {@link ServerSocket}. A newly-created server-socket channel is |
42 * pre-existing {@link ServerSocket}. A newly-created server-socket channel is |
41 * open but not yet bound. An attempt to invoke the {@link #accept() accept} |
43 * open but not yet bound. An attempt to invoke the {@link #accept() accept} |
42 * method of an unbound server-socket channel will cause a {@link NotYetBoundException} |
44 * method of an unbound server-socket channel will cause a {@link NotYetBoundException} |
43 * to be thrown. A server-socket channel can be bound by invoking one of the |
45 * to be thrown. A server-socket channel can be bound by invoking one of the |
44 * {@link #bind(java.net.SocketAddress,int) bind} methods defined by this class. |
46 * {@link #bind(java.net.SocketAddress,int) bind} methods defined by this class. |
45 * |
47 * |
|
48 * <p>Two kinds of server-socket channel are supported: <i>IP</i> (Internet Protocol) |
|
49 * and <i>unix domain</i> depending on which open method is used to create them and which subtype of |
|
50 * {@link SocketAddress} that they support for local and remote addresses. |
|
51 * <i>IP</i> channels are created using {@link #open()}. They use {@link |
|
52 * InetSocketAddress} addresses and support both IPv4 and IPv6 TCP/IP. |
|
53 * <i>unix domain</i> channels are created using {@link #open(ProtocolFamily)} |
|
54 * with the family parameter set to {@link StandardProtocolFamily#UNIX}. |
|
55 * They use {@link UnixDomainSocketAddress}es and also |
|
56 * do not support the {@link #socket()} method. |
|
57 * |
46 * <p> Socket options are configured using the {@link #setOption(SocketOption,Object) |
58 * <p> Socket options are configured using the {@link #setOption(SocketOption,Object) |
47 * setOption} method. Server-socket channels support the following options: |
59 * setOption} method. <i>IP</i> server-socket channels support the following options: |
48 * <blockquote> |
60 * <blockquote> |
49 * <table class="striped"> |
61 * <table class="striped"> |
50 * <caption style="display:none">Socket options</caption> |
62 * <caption style="display:none">Socket options</caption> |
51 * <thead> |
63 * <thead> |
52 * <tr> |
64 * <tr> |
90 protected ServerSocketChannel(SelectorProvider provider) { |
105 protected ServerSocketChannel(SelectorProvider provider) { |
91 super(provider); |
106 super(provider); |
92 } |
107 } |
93 |
108 |
94 /** |
109 /** |
95 * Opens a server-socket channel. |
110 * Opens an <i>IP</i> server-socket channel. |
96 * |
111 * |
97 * <p> The new channel is created by invoking the {@link |
112 * <p> The new channel is created by invoking the {@link |
98 * java.nio.channels.spi.SelectorProvider#openServerSocketChannel |
113 * java.nio.channels.spi.SelectorProvider#openServerSocketChannel |
99 * openServerSocketChannel} method of the system-wide default {@link |
114 * openServerSocketChannel} method of the system-wide default {@link |
100 * java.nio.channels.spi.SelectorProvider} object. |
115 * java.nio.channels.spi.SelectorProvider} object. |
109 * @throws IOException |
124 * @throws IOException |
110 * If an I/O error occurs |
125 * If an I/O error occurs |
111 */ |
126 */ |
112 public static ServerSocketChannel open() throws IOException { |
127 public static ServerSocketChannel open() throws IOException { |
113 return SelectorProvider.provider().openServerSocketChannel(); |
128 return SelectorProvider.provider().openServerSocketChannel(); |
|
129 } |
|
130 |
|
131 /** |
|
132 * Returns a {@link ServerSocketChannel} of the given protocol family. |
|
133 * Where family is equal to {@link StandardProtocolFamily#INET} or {@link |
|
134 * StandardProtocolFamily#INET6} the returned channel must be bound to an |
|
135 * {@link InetSocketAddress}. If family is {@link StandardProtocolFamily#UNIX} |
|
136 * the returned channel must be bound to a {@link jdk.net.UnixDomainSocketAddress} |
|
137 * |
|
138 * @param family the protocol family |
|
139 * |
|
140 * @return a ServerSocketChannel |
|
141 * |
|
142 * @throws IOException if an I/O error occurs |
|
143 * @throws UnsupportedAddressTypeException if the protocol family is not supported |
|
144 * @since 14 |
|
145 */ |
|
146 public static ServerSocketChannel open(ProtocolFamily family) throws IOException { |
|
147 return SelectorProvider.provider().openServerSocketChannel(family); |
114 } |
148 } |
115 |
149 |
116 /** |
150 /** |
117 * Returns an operation set identifying this channel's supported |
151 * Returns an operation set identifying this channel's supported |
118 * operations. |
152 * operations. |
148 * @throws AlreadyBoundException {@inheritDoc} |
182 * @throws AlreadyBoundException {@inheritDoc} |
149 * @throws UnsupportedAddressTypeException {@inheritDoc} |
183 * @throws UnsupportedAddressTypeException {@inheritDoc} |
150 * @throws ClosedChannelException {@inheritDoc} |
184 * @throws ClosedChannelException {@inheritDoc} |
151 * @throws IOException {@inheritDoc} |
185 * @throws IOException {@inheritDoc} |
152 * @throws SecurityException |
186 * @throws SecurityException |
153 * If a security manager has been installed and its {@link |
187 * If a security manager has been installed and its |
154 * SecurityManager#checkListen checkListen} method denies the |
188 * {@link SecurityManager#checkListen checkListen} method denies |
155 * operation |
189 * the operation for <i>IP</i> channels or for <i>unix domain</i> |
|
190 * channels, if the security manager denies "read" or "write" |
|
191 * {@link FilePermission} for the local path. |
156 * |
192 * |
157 * @since 1.7 |
193 * @since 1.7 |
158 */ |
194 */ |
159 public final ServerSocketChannel bind(SocketAddress local) |
195 public final ServerSocketChannel bind(SocketAddress local) |
160 throws IOException |
196 throws IOException |
174 * connections on the socket. Its exact semantics are implementation specific. |
210 * connections on the socket. Its exact semantics are implementation specific. |
175 * In particular, an implementation may impose a maximum length or may choose |
211 * In particular, an implementation may impose a maximum length or may choose |
176 * to ignore the parameter altogther. If the {@code backlog} parameter has |
212 * to ignore the parameter altogther. If the {@code backlog} parameter has |
177 * the value {@code 0}, or a negative value, then an implementation specific |
213 * the value {@code 0}, or a negative value, then an implementation specific |
178 * default is used. |
214 * default is used. |
|
215 * |
|
216 * <p> Note, for <i>Unix domain</i> channels, a file is created in the file-system |
|
217 * with the same name as this channel's bound address. This file persists after |
|
218 * the channel is closed, and must be removed before another channel can bind |
|
219 * to the same name. |
179 * |
220 * |
180 * @param local |
221 * @param local |
181 * The address to bind the socket, or {@code null} to bind to an |
222 * The address to bind the socket, or {@code null} to bind to an |
182 * automatically assigned socket address |
223 * automatically assigned socket address |
183 * @param backlog |
224 * @param backlog |
192 * @throws ClosedChannelException |
233 * @throws ClosedChannelException |
193 * If this channel is closed |
234 * If this channel is closed |
194 * @throws IOException |
235 * @throws IOException |
195 * If some other I/O error occurs |
236 * If some other I/O error occurs |
196 * @throws SecurityException |
237 * @throws SecurityException |
197 * If a security manager has been installed and its {@link |
238 * If a security manager has been installed and its |
198 * SecurityManager#checkListen checkListen} method denies the |
239 * {@link SecurityManager#checkListen checkListen} method denies |
199 * operation |
240 * the operation for <i>IP</i> channels or for <i>unix domain</i> |
|
241 * channels, if the security manager denies "read" or "write" |
|
242 * {@link FilePermission} for the local path. |
200 * |
243 * |
201 * @since 1.7 |
244 * @since 1.7 |
202 */ |
245 */ |
203 public abstract ServerSocketChannel bind(SocketAddress local, int backlog) |
246 public abstract ServerSocketChannel bind(SocketAddress local, int backlog) |
204 throws IOException; |
247 throws IOException; |
213 */ |
256 */ |
214 public abstract <T> ServerSocketChannel setOption(SocketOption<T> name, T value) |
257 public abstract <T> ServerSocketChannel setOption(SocketOption<T> name, T value) |
215 throws IOException; |
258 throws IOException; |
216 |
259 |
217 /** |
260 /** |
218 * Retrieves a server socket associated with this channel. |
261 * Retrieves a server socket associated with this channel if it is an <i>IP</i> |
|
262 * channel. The operation is not supported for <i>unix domain</i> channels. |
219 * |
263 * |
220 * <p> The returned object will not declare any public methods that are not |
264 * <p> The returned object will not declare any public methods that are not |
221 * declared in the {@link java.net.ServerSocket} class. </p> |
265 * declared in the {@link java.net.ServerSocket} class. </p> |
222 * |
266 * |
223 * @return A server socket associated with this channel |
267 * @return A server socket associated with this channel |
|
268 * @throws UnsupportedOperationException is this is a Unix domain channel |
224 */ |
269 */ |
225 public abstract ServerSocket socket(); |
270 public abstract ServerSocket socket(); |
226 |
271 |
227 /** |
272 /** |
228 * Accepts a connection made to this channel's socket. |
273 * Accepts a connection made to this channel's socket. |
233 * or an I/O error occurs. |
278 * or an I/O error occurs. |
234 * |
279 * |
235 * <p> The socket channel returned by this method, if any, will be in |
280 * <p> The socket channel returned by this method, if any, will be in |
236 * blocking mode regardless of the blocking mode of this channel. |
281 * blocking mode regardless of the blocking mode of this channel. |
237 * |
282 * |
238 * <p> This method performs exactly the same security checks as the {@link |
283 * <p> For <i>IP</i> channels, this method performs exactly the same security checks as the {@link |
239 * java.net.ServerSocket#accept accept} method of the {@link |
284 * java.net.ServerSocket#accept accept} method of the {@link |
240 * java.net.ServerSocket} class. That is, if a security manager has been |
285 * java.net.ServerSocket} class. That is, if a security manager has been |
241 * installed then for each new connection this method verifies that the |
286 * installed then for each new connection this method verifies that the |
242 * address and port number of the connection's remote endpoint are |
287 * address and port number of the connection's remote endpoint are |
243 * permitted by the security manager's {@link |
288 * permitted by the security manager's {@link |
244 * java.lang.SecurityManager#checkAccept checkAccept} method. </p> |
289 * java.lang.SecurityManager#checkAccept checkAccept} method. </p> |
245 * |
290 * |
|
291 * <p> For <i>unix domain</i> channels, this method performs a security |
|
292 * manager {@link SecurityManager#checkPermission(Permission)} using |
|
293 * a {@link java.io.FilePermission} constructed with the path from the |
|
294 * remote address and "read,write" as the actions. |
|
295 * |
246 * @return The socket channel for the new connection, |
296 * @return The socket channel for the new connection, |
247 * or {@code null} if this channel is in non-blocking mode |
297 * or {@code null} if this channel is in non-blocking mode |
248 * and no connection is available to be accepted |
298 * and no connection is available to be accepted |
249 * |
299 * |
250 * @throws ClosedChannelException |
300 * @throws ClosedChannelException |
274 public abstract SocketChannel accept() throws IOException; |
324 public abstract SocketChannel accept() throws IOException; |
275 |
325 |
276 /** |
326 /** |
277 * {@inheritDoc} |
327 * {@inheritDoc} |
278 * <p> |
328 * <p> |
279 * If there is a security manager set, its {@code checkConnect} method is |
329 * If there is a security manager set and this is an <i>IP</i> channel, |
|
330 * {@code checkConnect} method is |
280 * called with the local address and {@code -1} as its arguments to see |
331 * called with the local address and {@code -1} as its arguments to see |
281 * if the operation is allowed. If the operation is not allowed, |
332 * if the operation is allowed. If the operation is not allowed, |
282 * a {@code SocketAddress} representing the |
333 * a {@code SocketAddress} representing the |
283 * {@link java.net.InetAddress#getLoopbackAddress loopback} address and the |
334 * {@link java.net.InetAddress#getLoopbackAddress loopback} address and the |
284 * local port of the channel's socket is returned. |
335 * local port of the channel's socket is returned. |
|
336 * <p> |
|
337 * If there is a security manager set and this is an <i>unix domain</i> channel, |
|
338 * then this returns a {@link UnixDomainSocketAddress} corresponding to the |
|
339 * bound address. |
285 * |
340 * |
286 * @return The {@code SocketAddress} that the socket is bound to, or the |
341 * @return The {@code SocketAddress} that the socket is bound to, or the |
287 * {@code SocketAddress} representing the loopback address if |
342 * {@code SocketAddress} representing the loopback address if |
288 * denied by the security manager, or {@code null} if the |
343 * denied by the security manager, or {@code null} if the |
289 * channel's socket is not bound |
344 * channel's socket is not bound |