KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > nio > channels > ServerSocketChannel


1 /*
2  * @(#)ServerSocketChannel.java 1.24 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package java.nio.channels;
9
10 import java.io.IOException JavaDoc;
11 import java.net.ServerSocket JavaDoc;
12 import java.net.SocketAddress JavaDoc;
13 import java.nio.channels.spi.*;
14
15
16 /**
17  * A selectable channel for stream-oriented listening sockets.
18  *
19  * <p> Server-socket channels are not a complete abstraction of listening
20  * network sockets. Binding and the manipulation of socket options must be
21  * done through an associated {@link java.net.ServerSocket} object obtained by
22  * invoking the {@link #socket() socket} method. It is not possible to create
23  * a channel for an arbitrary, pre-existing server socket, nor is it possible
24  * to specify the {@link java.net.SocketImpl} object to be used by a server
25  * socket associated with a server-socket channel.
26  *
27  * <p> A server-socket channel is created by invoking the {@link #open() open}
28  * method of this class. A newly-created server-socket channel is open but not
29  * yet bound. An attempt to invoke the {@link #accept() accept} method of an
30  * unbound server-socket channel will cause a {@link NotYetBoundException} to
31  * be thrown. A server-socket channel can be bound by invoking one of the
32  * {@link java.net.ServerSocket#bind(java.net.SocketAddress,int) bind} methods
33  * of an associated server socket.
34  *
35  * <p> Server-socket channels are safe for use by multiple concurrent threads.
36  * </p>
37  *
38  *
39  * @author Mark Reinhold
40  * @author JSR-51 Expert Group
41  * @version 1.24, 03/12/19
42  * @since 1.4
43  */

44
45 public abstract class ServerSocketChannel
46     extends AbstractSelectableChannel
47 {
48
49     /**
50      * Initializes a new instance of this class.
51      */

52     protected ServerSocketChannel(SelectorProvider provider) {
53     super(provider);
54     }
55
56     /**
57      * Opens a server-socket channel.
58      *
59      * <p> The new channel is created by invoking the {@link
60      * java.nio.channels.spi.SelectorProvider#openServerSocketChannel
61      * openServerSocketChannel} method of the system-wide default {@link
62      * java.nio.channels.spi.SelectorProvider} object.
63      *
64      * <p> The new channel's socket is initially unbound; it must be bound to a
65      * specific address via one of its socket's {@link
66      * java.net.ServerSocket#bind(SocketAddress) bind} methods before
67      * connections can be accepted. </p>
68      *
69      * @return A new socket channel
70      *
71      * @throws IOException
72      * If an I/O error occurs
73      */

74     public static ServerSocketChannel JavaDoc open() throws IOException JavaDoc {
75     return SelectorProvider.provider().openServerSocketChannel();
76     }
77
78     /**
79      * Returns an operation set identifying this channel's supported
80      * operations.
81      *
82      * <p> Server-socket channels only support the accepting of new
83      * connections, so this method returns {@link SelectionKey#OP_ACCEPT}.
84      * </p>
85      *
86      * @return The valid-operation set
87      */

88     public final int validOps() {
89     return SelectionKey.OP_ACCEPT;
90     }
91
92
93     // -- ServerSocket-specific operations --
94

95     /**
96      * Retrieves a server socket associated with this channel.
97      *
98      * <p> The returned object will not declare any public methods that are not
99      * declared in the {@link java.net.ServerSocket} class. </p>
100      *
101      * @return A server socket associated with this channel
102      */

103     public abstract ServerSocket JavaDoc socket();
104
105     /**
106      * Accepts a connection made to this channel's socket.
107      *
108      * <p> If this channel is in non-blocking mode then this method will
109      * immediately return <tt>null</tt> if there are no pending connections.
110      * Otherwise it will block indefinitely until a new connection is available
111      * or an I/O error occurs.
112      *
113      * <p> The socket channel returned by this method, if any, will be in
114      * blocking mode regardless of the blocking mode of this channel.
115      *
116      * <p> This method performs exactly the same security checks as the {@link
117      * java.net.ServerSocket#accept accept} method of the {@link
118      * java.net.ServerSocket} class. That is, if a security manager has been
119      * installed then for each new connection this method verifies that the
120      * address and port number of the connection's remote endpoint are
121      * permitted by the security manager's {@link
122      * java.lang.SecurityManager#checkAccept checkAccept} method. </p>
123      *
124      * @return The socket channel for the new connection,
125      * or <tt>null</tt> if this channel is in non-blocking mode
126      * and no connection is available to be accepted
127      *
128      * @throws ClosedChannelException
129      * If this channel is closed
130      *
131      * @throws AsynchronousCloseException
132      * If another thread closes this channel
133      * while the accept operation is in progress
134      *
135      * @throws ClosedByInterruptException
136      * If another thread interrupts the current thread
137      * while the accept operation is in progress, thereby
138      * closing the channel and setting the current thread's
139      * interrupt status
140      *
141      * @throws NotYetBoundException
142      * If this channel's socket has not yet been bound
143      *
144      * @throws SecurityException
145      * If a security manager has been installed
146      * and it does not permit access to the remote endpoint
147      * of the new connection
148      *
149      * @throws IOException
150      * If some other I/O error occurs
151      */

152     public abstract SocketChannel JavaDoc accept() throws IOException JavaDoc;
153
154 }
155
Popular Tags