149 * {@link #getLocalPort getLocalPort}.
150 * <p>
151 * The maximum queue length for incoming connection indications (a
152 * request to connect) is set to the {@code backlog} parameter. If
153 * a connection indication arrives when the queue is full, the
154 * connection is refused.
155 * <p>
156 * If the application has specified a server socket factory, that
157 * factory's {@code createSocketImpl} method is called to create
158 * the actual socket implementation. Otherwise a "plain" socket is created.
159 * <p>
160 * If there is a security manager,
161 * its {@code checkListen} method is called
162 * with the {@code port} argument
163 * as its argument to ensure the operation is allowed.
164 * This could result in a SecurityException.
165 *
166 * The {@code backlog} argument is the requested maximum number of
167 * pending connections on the socket. Its exact semantics are implementation
168 * specific. In particular, an implementation may impose a maximum length
169 * or may choose to ignore the parameter altogther. The value provided
170 * should be greater than {@code 0}. If it is less than or equal to
171 * {@code 0}, then an implementation specific default will be used.
172 *
173 * @param port the port number, or {@code 0} to use a port
174 * number that is automatically allocated.
175 * @param backlog requested maximum length of the queue of incoming
176 * connections.
177 *
178 * @exception IOException if an I/O error occurs when opening the socket.
179 * @exception SecurityException
180 * if a security manager exists and its {@code checkListen}
181 * method doesn't allow the operation.
182 * @exception IllegalArgumentException if the port parameter is outside
183 * the specified range of valid port values, which is between
184 * 0 and 65535, inclusive.
185 *
186 * @see java.net.SocketImpl
187 * @see java.net.SocketImplFactory#createSocketImpl()
188 * @see java.net.ServerSocket#setSocketFactory(java.net.SocketImplFactory)
189 * @see SecurityManager#checkListen
197 * local IP address to bind to. The <i>bindAddr</i> argument
198 * can be used on a multi-homed host for a ServerSocket that
199 * will only accept connect requests to one of its addresses.
200 * If <i>bindAddr</i> is null, it will default accepting
201 * connections on any/all local addresses.
202 * The port must be between 0 and 65535, inclusive.
203 * A port number of {@code 0} means that the port number is
204 * automatically allocated, typically from an ephemeral port range.
205 * This port number can then be retrieved by calling
206 * {@link #getLocalPort getLocalPort}.
207 *
208 * <P>If there is a security manager, this method
209 * calls its {@code checkListen} method
210 * with the {@code port} argument
211 * as its argument to ensure the operation is allowed.
212 * This could result in a SecurityException.
213 *
214 * The {@code backlog} argument is the requested maximum number of
215 * pending connections on the socket. Its exact semantics are implementation
216 * specific. In particular, an implementation may impose a maximum length
217 * or may choose to ignore the parameter altogther. The value provided
218 * should be greater than {@code 0}. If it is less than or equal to
219 * {@code 0}, then an implementation specific default will be used.
220 *
221 * @param port the port number, or {@code 0} to use a port
222 * number that is automatically allocated.
223 * @param backlog requested maximum length of the queue of incoming
224 * connections.
225 * @param bindAddr the local InetAddress the server will bind to
226 *
227 * @throws SecurityException if a security manager exists and
228 * its {@code checkListen} method doesn't allow the operation.
229 *
230 * @throws IOException if an I/O error occurs when opening the socket.
231 * @exception IllegalArgumentException if the port parameter is outside
232 * the specified range of valid port values, which is between
233 * 0 and 65535, inclusive.
234 *
235 * @see SocketOptions
236 * @see SocketImpl
237 * @see SecurityManager#checkListen
334 * its {@code checkListen} method doesn't allow the operation.
335 * @throws IllegalArgumentException if endpoint is a
336 * SocketAddress subclass not supported by this socket
337 * @since 1.4
338 */
339 public void bind(SocketAddress endpoint) throws IOException {
340 bind(endpoint, 50);
341 }
342
343 /**
344 *
345 * Binds the {@code ServerSocket} to a specific address
346 * (IP address and port number).
347 * <p>
348 * If the address is {@code null}, then the system will pick up
349 * an ephemeral port and a valid local address to bind the socket.
350 * <P>
351 * The {@code backlog} argument is the requested maximum number of
352 * pending connections on the socket. Its exact semantics are implementation
353 * specific. In particular, an implementation may impose a maximum length
354 * or may choose to ignore the parameter altogther. The value provided
355 * should be greater than {@code 0}. If it is less than or equal to
356 * {@code 0}, then an implementation specific default will be used.
357 * @param endpoint The IP address and port number to bind to.
358 * @param backlog requested maximum length of the queue of
359 * incoming connections.
360 * @throws IOException if the bind operation fails, or if the socket
361 * is already bound.
362 * @throws SecurityException if a {@code SecurityManager} is present and
363 * its {@code checkListen} method doesn't allow the operation.
364 * @throws IllegalArgumentException if endpoint is a
365 * SocketAddress subclass not supported by this socket
366 * @since 1.4
367 */
368 public void bind(SocketAddress endpoint, int backlog) throws IOException {
369 if (isClosed())
370 throw new SocketException("Socket is closed");
371 if (!oldImpl && isBound())
372 throw new SocketException("Already bound");
373 if (endpoint == null)
374 endpoint = new InetSocketAddress(0);
809 if (factory != null) {
810 throw new SocketException("factory already defined");
811 }
812 SecurityManager security = System.getSecurityManager();
813 if (security != null) {
814 security.checkSetFactory();
815 }
816 factory = fac;
817 }
818
819 /**
820 * Sets a default proposed value for the
821 * {@link SocketOptions#SO_RCVBUF SO_RCVBUF} option for sockets
822 * accepted from this {@code ServerSocket}. The value actually set
823 * in the accepted socket must be determined by calling
824 * {@link Socket#getReceiveBufferSize()} after the socket
825 * is returned by {@link #accept()}.
826 * <p>
827 * The value of {@link SocketOptions#SO_RCVBUF SO_RCVBUF} is used both to
828 * set the size of the internal socket receive buffer, and to set the size
829 * of the TCP receive window that is advertized to the remote peer.
830 * <p>
831 * It is possible to change the value subsequently, by calling
832 * {@link Socket#setReceiveBufferSize(int)}. However, if the application
833 * wishes to allow a receive window larger than 64K bytes, as defined by RFC1323
834 * then the proposed value must be set in the ServerSocket <B>before</B>
835 * it is bound to a local address. This implies, that the ServerSocket must be
836 * created with the no-argument constructor, then setReceiveBufferSize() must
837 * be called and lastly the ServerSocket is bound to an address by calling bind().
838 * <p>
839 * Failure to do this will not cause an error, and the buffer size may be set to the
840 * requested value but the TCP receive window in sockets accepted from
841 * this ServerSocket will be no larger than 64K bytes.
842 *
843 * @exception SocketException if there is an error
844 * in the underlying protocol, such as a TCP error.
845 *
846 * @param size the size to which to set the receive buffer
847 * size. This value must be greater than 0.
848 *
849 * @exception IllegalArgumentException if the
|
149 * {@link #getLocalPort getLocalPort}.
150 * <p>
151 * The maximum queue length for incoming connection indications (a
152 * request to connect) is set to the {@code backlog} parameter. If
153 * a connection indication arrives when the queue is full, the
154 * connection is refused.
155 * <p>
156 * If the application has specified a server socket factory, that
157 * factory's {@code createSocketImpl} method is called to create
158 * the actual socket implementation. Otherwise a "plain" socket is created.
159 * <p>
160 * If there is a security manager,
161 * its {@code checkListen} method is called
162 * with the {@code port} argument
163 * as its argument to ensure the operation is allowed.
164 * This could result in a SecurityException.
165 *
166 * The {@code backlog} argument is the requested maximum number of
167 * pending connections on the socket. Its exact semantics are implementation
168 * specific. In particular, an implementation may impose a maximum length
169 * or may choose to ignore the parameter altogether. The value provided
170 * should be greater than {@code 0}. If it is less than or equal to
171 * {@code 0}, then an implementation specific default will be used.
172 *
173 * @param port the port number, or {@code 0} to use a port
174 * number that is automatically allocated.
175 * @param backlog requested maximum length of the queue of incoming
176 * connections.
177 *
178 * @exception IOException if an I/O error occurs when opening the socket.
179 * @exception SecurityException
180 * if a security manager exists and its {@code checkListen}
181 * method doesn't allow the operation.
182 * @exception IllegalArgumentException if the port parameter is outside
183 * the specified range of valid port values, which is between
184 * 0 and 65535, inclusive.
185 *
186 * @see java.net.SocketImpl
187 * @see java.net.SocketImplFactory#createSocketImpl()
188 * @see java.net.ServerSocket#setSocketFactory(java.net.SocketImplFactory)
189 * @see SecurityManager#checkListen
197 * local IP address to bind to. The <i>bindAddr</i> argument
198 * can be used on a multi-homed host for a ServerSocket that
199 * will only accept connect requests to one of its addresses.
200 * If <i>bindAddr</i> is null, it will default accepting
201 * connections on any/all local addresses.
202 * The port must be between 0 and 65535, inclusive.
203 * A port number of {@code 0} means that the port number is
204 * automatically allocated, typically from an ephemeral port range.
205 * This port number can then be retrieved by calling
206 * {@link #getLocalPort getLocalPort}.
207 *
208 * <P>If there is a security manager, this method
209 * calls its {@code checkListen} method
210 * with the {@code port} argument
211 * as its argument to ensure the operation is allowed.
212 * This could result in a SecurityException.
213 *
214 * The {@code backlog} argument is the requested maximum number of
215 * pending connections on the socket. Its exact semantics are implementation
216 * specific. In particular, an implementation may impose a maximum length
217 * or may choose to ignore the parameter altogether. The value provided
218 * should be greater than {@code 0}. If it is less than or equal to
219 * {@code 0}, then an implementation specific default will be used.
220 *
221 * @param port the port number, or {@code 0} to use a port
222 * number that is automatically allocated.
223 * @param backlog requested maximum length of the queue of incoming
224 * connections.
225 * @param bindAddr the local InetAddress the server will bind to
226 *
227 * @throws SecurityException if a security manager exists and
228 * its {@code checkListen} method doesn't allow the operation.
229 *
230 * @throws IOException if an I/O error occurs when opening the socket.
231 * @exception IllegalArgumentException if the port parameter is outside
232 * the specified range of valid port values, which is between
233 * 0 and 65535, inclusive.
234 *
235 * @see SocketOptions
236 * @see SocketImpl
237 * @see SecurityManager#checkListen
334 * its {@code checkListen} method doesn't allow the operation.
335 * @throws IllegalArgumentException if endpoint is a
336 * SocketAddress subclass not supported by this socket
337 * @since 1.4
338 */
339 public void bind(SocketAddress endpoint) throws IOException {
340 bind(endpoint, 50);
341 }
342
343 /**
344 *
345 * Binds the {@code ServerSocket} to a specific address
346 * (IP address and port number).
347 * <p>
348 * If the address is {@code null}, then the system will pick up
349 * an ephemeral port and a valid local address to bind the socket.
350 * <P>
351 * The {@code backlog} argument is the requested maximum number of
352 * pending connections on the socket. Its exact semantics are implementation
353 * specific. In particular, an implementation may impose a maximum length
354 * or may choose to ignore the parameter altogether. The value provided
355 * should be greater than {@code 0}. If it is less than or equal to
356 * {@code 0}, then an implementation specific default will be used.
357 * @param endpoint The IP address and port number to bind to.
358 * @param backlog requested maximum length of the queue of
359 * incoming connections.
360 * @throws IOException if the bind operation fails, or if the socket
361 * is already bound.
362 * @throws SecurityException if a {@code SecurityManager} is present and
363 * its {@code checkListen} method doesn't allow the operation.
364 * @throws IllegalArgumentException if endpoint is a
365 * SocketAddress subclass not supported by this socket
366 * @since 1.4
367 */
368 public void bind(SocketAddress endpoint, int backlog) throws IOException {
369 if (isClosed())
370 throw new SocketException("Socket is closed");
371 if (!oldImpl && isBound())
372 throw new SocketException("Already bound");
373 if (endpoint == null)
374 endpoint = new InetSocketAddress(0);
809 if (factory != null) {
810 throw new SocketException("factory already defined");
811 }
812 SecurityManager security = System.getSecurityManager();
813 if (security != null) {
814 security.checkSetFactory();
815 }
816 factory = fac;
817 }
818
819 /**
820 * Sets a default proposed value for the
821 * {@link SocketOptions#SO_RCVBUF SO_RCVBUF} option for sockets
822 * accepted from this {@code ServerSocket}. The value actually set
823 * in the accepted socket must be determined by calling
824 * {@link Socket#getReceiveBufferSize()} after the socket
825 * is returned by {@link #accept()}.
826 * <p>
827 * The value of {@link SocketOptions#SO_RCVBUF SO_RCVBUF} is used both to
828 * set the size of the internal socket receive buffer, and to set the size
829 * of the TCP receive window that is advertised to the remote peer.
830 * <p>
831 * It is possible to change the value subsequently, by calling
832 * {@link Socket#setReceiveBufferSize(int)}. However, if the application
833 * wishes to allow a receive window larger than 64K bytes, as defined by RFC1323
834 * then the proposed value must be set in the ServerSocket <B>before</B>
835 * it is bound to a local address. This implies, that the ServerSocket must be
836 * created with the no-argument constructor, then setReceiveBufferSize() must
837 * be called and lastly the ServerSocket is bound to an address by calling bind().
838 * <p>
839 * Failure to do this will not cause an error, and the buffer size may be set to the
840 * requested value but the TCP receive window in sockets accepted from
841 * this ServerSocket will be no larger than 64K bytes.
842 *
843 * @exception SocketException if there is an error
844 * in the underlying protocol, such as a TCP error.
845 *
846 * @param size the size to which to set the receive buffer
847 * size. This value must be greater than 0.
848 *
849 * @exception IllegalArgumentException if the
|