Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/java/net/ServerSocket.java
38829 views
/*1* Copyright (c) 1995, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package java.net;2627import java.io.FileDescriptor;28import java.io.IOException;29import java.nio.channels.ServerSocketChannel;30import java.security.AccessController;31import java.security.PrivilegedExceptionAction;3233import sun.security.util.SecurityConstants;3435/**36* This class implements server sockets. A server socket waits for37* requests to come in over the network. It performs some operation38* based on that request, and then possibly returns a result to the requester.39* <p>40* The actual work of the server socket is performed by an instance41* of the {@code SocketImpl} class. An application can42* change the socket factory that creates the socket43* implementation to configure itself to create sockets44* appropriate to the local firewall.45*46* @author unascribed47* @see java.net.SocketImpl48* @see java.net.ServerSocket#setSocketFactory(java.net.SocketImplFactory)49* @see java.nio.channels.ServerSocketChannel50* @since JDK1.051*/52public53class ServerSocket implements java.io.Closeable {54/**55* Various states of this socket.56*/57private boolean created = false;58private boolean bound = false;59private boolean closed = false;60private Object closeLock = new Object();6162/**63* The implementation of this Socket.64*/65private SocketImpl impl;6667/**68* Are we using an older SocketImpl?69*/70private boolean oldImpl = false;7172/**73* Package-private constructor to create a ServerSocket associated with74* the given SocketImpl.75*76* @throws SecurityException if a security manager is set and77* its {@code checkPermission} method doesn't allow78* {@code NetPermission("setSocketImpl")}.79*/80ServerSocket(SocketImpl impl) {81checkPermission();82this.impl = impl;83impl.setServerSocket(this);84}8586private static Void checkPermission() {87SecurityManager sm = System.getSecurityManager();88if (sm != null) {89sm.checkPermission(SecurityConstants.SET_SOCKETIMPL_PERMISSION);90}91return null;92}9394/**95* Creates an unbound server socket.96*97* @exception IOException IO error when opening the socket.98* @revised 1.499*/100public ServerSocket() throws IOException {101setImpl();102}103104/**105* Creates a server socket, bound to the specified port. A port number106* of {@code 0} means that the port number is automatically107* allocated, typically from an ephemeral port range. This port108* number can then be retrieved by calling {@link #getLocalPort getLocalPort}.109* <p>110* The maximum queue length for incoming connection indications (a111* request to connect) is set to {@code 50}. If a connection112* indication arrives when the queue is full, the connection is refused.113* <p>114* If the application has specified a server socket factory, that115* factory's {@code createSocketImpl} method is called to create116* the actual socket implementation. Otherwise a "plain" socket is created.117* <p>118* If there is a security manager,119* its {@code checkListen} method is called120* with the {@code port} argument121* as its argument to ensure the operation is allowed.122* This could result in a SecurityException.123*124*125* @param port the port number, or {@code 0} to use a port126* number that is automatically allocated.127*128* @exception IOException if an I/O error occurs when opening the socket.129* @exception SecurityException130* if a security manager exists and its {@code checkListen}131* method doesn't allow the operation.132* @exception IllegalArgumentException if the port parameter is outside133* the specified range of valid port values, which is between134* 0 and 65535, inclusive.135*136* @see java.net.SocketImpl137* @see java.net.SocketImplFactory#createSocketImpl()138* @see java.net.ServerSocket#setSocketFactory(java.net.SocketImplFactory)139* @see SecurityManager#checkListen140*/141public ServerSocket(int port) throws IOException {142this(port, 50, null);143}144145/**146* Creates a server socket and binds it to the specified local port147* number, with the specified backlog.148* A port number of {@code 0} means that the port number is149* automatically allocated, typically from an ephemeral port range.150* This port number can then be retrieved by calling151* {@link #getLocalPort getLocalPort}.152* <p>153* The maximum queue length for incoming connection indications (a154* request to connect) is set to the {@code backlog} parameter. If155* a connection indication arrives when the queue is full, the156* connection is refused.157* <p>158* If the application has specified a server socket factory, that159* factory's {@code createSocketImpl} method is called to create160* the actual socket implementation. Otherwise a "plain" socket is created.161* <p>162* If there is a security manager,163* its {@code checkListen} method is called164* with the {@code port} argument165* as its argument to ensure the operation is allowed.166* This could result in a SecurityException.167*168* The {@code backlog} argument is the requested maximum number of169* pending connections on the socket. Its exact semantics are implementation170* specific. In particular, an implementation may impose a maximum length171* or may choose to ignore the parameter altogther. The value provided172* should be greater than {@code 0}. If it is less than or equal to173* {@code 0}, then an implementation specific default will be used.174* <P>175*176* @param port the port number, or {@code 0} to use a port177* number that is automatically allocated.178* @param backlog requested maximum length of the queue of incoming179* connections.180*181* @exception IOException if an I/O error occurs when opening the socket.182* @exception SecurityException183* if a security manager exists and its {@code checkListen}184* method doesn't allow the operation.185* @exception IllegalArgumentException if the port parameter is outside186* the specified range of valid port values, which is between187* 0 and 65535, inclusive.188*189* @see java.net.SocketImpl190* @see java.net.SocketImplFactory#createSocketImpl()191* @see java.net.ServerSocket#setSocketFactory(java.net.SocketImplFactory)192* @see SecurityManager#checkListen193*/194public ServerSocket(int port, int backlog) throws IOException {195this(port, backlog, null);196}197198/**199* Create a server with the specified port, listen backlog, and200* local IP address to bind to. The <i>bindAddr</i> argument201* can be used on a multi-homed host for a ServerSocket that202* will only accept connect requests to one of its addresses.203* If <i>bindAddr</i> is null, it will default accepting204* connections on any/all local addresses.205* The port must be between 0 and 65535, inclusive.206* A port number of {@code 0} means that the port number is207* automatically allocated, typically from an ephemeral port range.208* This port number can then be retrieved by calling209* {@link #getLocalPort getLocalPort}.210*211* <P>If there is a security manager, this method212* calls its {@code checkListen} method213* with the {@code port} argument214* as its argument to ensure the operation is allowed.215* This could result in a SecurityException.216*217* The {@code backlog} argument is the requested maximum number of218* pending connections on the socket. Its exact semantics are implementation219* specific. In particular, an implementation may impose a maximum length220* or may choose to ignore the parameter altogther. The value provided221* should be greater than {@code 0}. If it is less than or equal to222* {@code 0}, then an implementation specific default will be used.223* <P>224* @param port the port number, or {@code 0} to use a port225* number that is automatically allocated.226* @param backlog requested maximum length of the queue of incoming227* connections.228* @param bindAddr the local InetAddress the server will bind to229*230* @throws SecurityException if a security manager exists and231* its {@code checkListen} method doesn't allow the operation.232*233* @throws IOException if an I/O error occurs when opening the socket.234* @exception IllegalArgumentException if the port parameter is outside235* the specified range of valid port values, which is between236* 0 and 65535, inclusive.237*238* @see SocketOptions239* @see SocketImpl240* @see SecurityManager#checkListen241* @since JDK1.1242*/243public ServerSocket(int port, int backlog, InetAddress bindAddr) throws IOException {244setImpl();245if (port < 0 || port > 0xFFFF)246throw new IllegalArgumentException(247"Port value out of range: " + port);248if (backlog < 1)249backlog = 50;250try {251bind(new InetSocketAddress(bindAddr, port), backlog);252} catch(SecurityException e) {253close();254throw e;255} catch(IOException e) {256close();257throw e;258}259}260261/**262* Get the {@code SocketImpl} attached to this socket, creating263* it if necessary.264*265* @return the {@code SocketImpl} attached to that ServerSocket.266* @throws SocketException if creation fails.267* @since 1.4268*/269SocketImpl getImpl() throws SocketException {270if (!created)271createImpl();272return impl;273}274275private void checkOldImpl() {276if (impl == null)277return;278// SocketImpl.connect() is a protected method, therefore we need to use279// getDeclaredMethod, therefore we need permission to access the member280try {281AccessController.doPrivileged(282new PrivilegedExceptionAction<Void>() {283public Void run() throws NoSuchMethodException {284impl.getClass().getDeclaredMethod("connect",285SocketAddress.class,286int.class);287return null;288}289});290} catch (java.security.PrivilegedActionException e) {291oldImpl = true;292}293}294295private void setImpl() {296if (factory != null) {297impl = factory.createSocketImpl();298checkOldImpl();299} else {300// No need to do a checkOldImpl() here, we know it's an up to date301// SocketImpl!302impl = new SocksSocketImpl();303}304if (impl != null)305impl.setServerSocket(this);306}307308/**309* Creates the socket implementation.310*311* @throws IOException if creation fails312* @since 1.4313*/314void createImpl() throws SocketException {315if (impl == null)316setImpl();317try {318impl.create(true);319created = true;320} catch (IOException e) {321throw new SocketException(e.getMessage());322}323}324325/**326*327* Binds the {@code ServerSocket} to a specific address328* (IP address and port number).329* <p>330* If the address is {@code null}, then the system will pick up331* an ephemeral port and a valid local address to bind the socket.332* <p>333* @param endpoint The IP address and port number to bind to.334* @throws IOException if the bind operation fails, or if the socket335* is already bound.336* @throws SecurityException if a {@code SecurityManager} is present and337* its {@code checkListen} method doesn't allow the operation.338* @throws IllegalArgumentException if endpoint is a339* SocketAddress subclass not supported by this socket340* @since 1.4341*/342public void bind(SocketAddress endpoint) throws IOException {343bind(endpoint, 50);344}345346/**347*348* Binds the {@code ServerSocket} to a specific address349* (IP address and port number).350* <p>351* If the address is {@code null}, then the system will pick up352* an ephemeral port and a valid local address to bind the socket.353* <P>354* The {@code backlog} argument is the requested maximum number of355* pending connections on the socket. Its exact semantics are implementation356* specific. In particular, an implementation may impose a maximum length357* or may choose to ignore the parameter altogther. The value provided358* should be greater than {@code 0}. If it is less than or equal to359* {@code 0}, then an implementation specific default will be used.360* @param endpoint The IP address and port number to bind to.361* @param backlog requested maximum length of the queue of362* incoming connections.363* @throws IOException if the bind operation fails, or if the socket364* is already bound.365* @throws SecurityException if a {@code SecurityManager} is present and366* its {@code checkListen} method doesn't allow the operation.367* @throws IllegalArgumentException if endpoint is a368* SocketAddress subclass not supported by this socket369* @since 1.4370*/371public void bind(SocketAddress endpoint, int backlog) throws IOException {372if (isClosed())373throw new SocketException("Socket is closed");374if (!oldImpl && isBound())375throw new SocketException("Already bound");376if (endpoint == null)377endpoint = new InetSocketAddress(0);378if (!(endpoint instanceof InetSocketAddress))379throw new IllegalArgumentException("Unsupported address type");380InetSocketAddress epoint = (InetSocketAddress) endpoint;381if (epoint.isUnresolved())382throw new SocketException("Unresolved address");383if (backlog < 1)384backlog = 50;385try {386SecurityManager security = System.getSecurityManager();387if (security != null)388security.checkListen(epoint.getPort());389getImpl().bind(epoint.getAddress(), epoint.getPort());390getImpl().listen(backlog);391bound = true;392} catch(SecurityException e) {393bound = false;394throw e;395} catch(IOException e) {396bound = false;397throw e;398}399}400401/**402* Returns the local address of this server socket.403* <p>404* If the socket was bound prior to being {@link #close closed},405* then this method will continue to return the local address406* after the socket is closed.407* <p>408* If there is a security manager set, its {@code checkConnect} method is409* called with the local address and {@code -1} as its arguments to see410* if the operation is allowed. If the operation is not allowed,411* the {@link InetAddress#getLoopbackAddress loopback} address is returned.412*413* @return the address to which this socket is bound,414* or the loopback address if denied by the security manager,415* or {@code null} if the socket is unbound.416*417* @see SecurityManager#checkConnect418*/419public InetAddress getInetAddress() {420if (!isBound())421return null;422try {423InetAddress in = getImpl().getInetAddress();424SecurityManager sm = System.getSecurityManager();425if (sm != null)426sm.checkConnect(in.getHostAddress(), -1);427return in;428} catch (SecurityException e) {429return InetAddress.getLoopbackAddress();430} catch (SocketException e) {431// nothing432// If we're bound, the impl has been created433// so we shouldn't get here434}435return null;436}437438/**439* Returns the port number on which this socket is listening.440* <p>441* If the socket was bound prior to being {@link #close closed},442* then this method will continue to return the port number443* after the socket is closed.444*445* @return the port number to which this socket is listening or446* -1 if the socket is not bound yet.447*/448public int getLocalPort() {449if (!isBound())450return -1;451try {452return getImpl().getLocalPort();453} catch (SocketException e) {454// nothing455// If we're bound, the impl has been created456// so we shouldn't get here457}458return -1;459}460461/**462* Returns the address of the endpoint this socket is bound to.463* <p>464* If the socket was bound prior to being {@link #close closed},465* then this method will continue to return the address of the endpoint466* after the socket is closed.467* <p>468* If there is a security manager set, its {@code checkConnect} method is469* called with the local address and {@code -1} as its arguments to see470* if the operation is allowed. If the operation is not allowed,471* a {@code SocketAddress} representing the472* {@link InetAddress#getLoopbackAddress loopback} address and the local473* port to which the socket is bound is returned.474*475* @return a {@code SocketAddress} representing the local endpoint of476* this socket, or a {@code SocketAddress} representing the477* loopback address if denied by the security manager,478* or {@code null} if the socket is not bound yet.479*480* @see #getInetAddress()481* @see #getLocalPort()482* @see #bind(SocketAddress)483* @see SecurityManager#checkConnect484* @since 1.4485*/486487public SocketAddress getLocalSocketAddress() {488if (!isBound())489return null;490return new InetSocketAddress(getInetAddress(), getLocalPort());491}492493/**494* Listens for a connection to be made to this socket and accepts495* it. The method blocks until a connection is made.496*497* <p>A new Socket {@code s} is created and, if there498* is a security manager,499* the security manager's {@code checkAccept} method is called500* with {@code s.getInetAddress().getHostAddress()} and501* {@code s.getPort()}502* as its arguments to ensure the operation is allowed.503* This could result in a SecurityException.504*505* @exception IOException if an I/O error occurs when waiting for a506* connection.507* @exception SecurityException if a security manager exists and its508* {@code checkAccept} method doesn't allow the operation.509* @exception SocketTimeoutException if a timeout was previously set with setSoTimeout and510* the timeout has been reached.511* @exception java.nio.channels.IllegalBlockingModeException512* if this socket has an associated channel, the channel is in513* non-blocking mode, and there is no connection ready to be514* accepted515*516* @return the new Socket517* @see SecurityManager#checkAccept518* @revised 1.4519* @spec JSR-51520*/521public Socket accept() throws IOException {522if (isClosed())523throw new SocketException("Socket is closed");524if (!isBound())525throw new SocketException("Socket is not bound yet");526Socket s = new Socket((SocketImpl) null);527implAccept(s);528return s;529}530531/**532* Subclasses of ServerSocket use this method to override accept()533* to return their own subclass of socket. So a FooServerSocket534* will typically hand this method an <i>empty</i> FooSocket. On535* return from implAccept the FooSocket will be connected to a client.536*537* @param s the Socket538* @throws java.nio.channels.IllegalBlockingModeException539* if this socket has an associated channel,540* and the channel is in non-blocking mode541* @throws IOException if an I/O error occurs when waiting542* for a connection.543* @since JDK1.1544* @revised 1.4545* @spec JSR-51546*/547protected final void implAccept(Socket s) throws IOException {548SocketImpl si = null;549try {550if (s.impl == null)551s.setImpl();552else {553s.impl.reset();554}555si = s.impl;556s.impl = null;557si.address = new InetAddress();558si.fd = new FileDescriptor();559getImpl().accept(si);560561SecurityManager security = System.getSecurityManager();562if (security != null) {563security.checkAccept(si.getInetAddress().getHostAddress(),564si.getPort());565}566} catch (IOException e) {567if (si != null)568si.reset();569s.impl = si;570throw e;571} catch (SecurityException e) {572if (si != null)573si.reset();574s.impl = si;575throw e;576}577s.impl = si;578s.postAccept();579}580581/**582* Closes this socket.583*584* Any thread currently blocked in {@link #accept()} will throw585* a {@link SocketException}.586*587* <p> If this socket has an associated channel then the channel is closed588* as well.589*590* @exception IOException if an I/O error occurs when closing the socket.591* @revised 1.4592* @spec JSR-51593*/594public void close() throws IOException {595synchronized(closeLock) {596if (isClosed())597return;598if (created)599impl.close();600closed = true;601}602}603604/**605* Returns the unique {@link java.nio.channels.ServerSocketChannel} object606* associated with this socket, if any.607*608* <p> A server socket will have a channel if, and only if, the channel609* itself was created via the {@link610* java.nio.channels.ServerSocketChannel#open ServerSocketChannel.open}611* method.612*613* @return the server-socket channel associated with this socket,614* or {@code null} if this socket was not created615* for a channel616*617* @since 1.4618* @spec JSR-51619*/620public ServerSocketChannel getChannel() {621return null;622}623624/**625* Returns the binding state of the ServerSocket.626*627* @return true if the ServerSocket successfully bound to an address628* @since 1.4629*/630public boolean isBound() {631// Before 1.3 ServerSockets were always bound during creation632return bound || oldImpl;633}634635/**636* Returns the closed state of the ServerSocket.637*638* @return true if the socket has been closed639* @since 1.4640*/641public boolean isClosed() {642synchronized(closeLock) {643return closed;644}645}646647/**648* Enable/disable {@link SocketOptions#SO_TIMEOUT SO_TIMEOUT} with the649* specified timeout, in milliseconds. With this option set to a non-zero650* timeout, a call to accept() for this ServerSocket651* will block for only this amount of time. If the timeout expires,652* a <B>java.net.SocketTimeoutException</B> is raised, though the653* ServerSocket is still valid. The option <B>must</B> be enabled654* prior to entering the blocking operation to have effect. The655* timeout must be {@code > 0}.656* A timeout of zero is interpreted as an infinite timeout.657* @param timeout the specified timeout, in milliseconds658* @exception SocketException if there is an error in659* the underlying protocol, such as a TCP error.660* @since JDK1.1661* @see #getSoTimeout()662*/663public synchronized void setSoTimeout(int timeout) throws SocketException {664if (isClosed())665throw new SocketException("Socket is closed");666getImpl().setOption(SocketOptions.SO_TIMEOUT, new Integer(timeout));667}668669/**670* Retrieve setting for {@link SocketOptions#SO_TIMEOUT SO_TIMEOUT}.671* 0 returns implies that the option is disabled (i.e., timeout of infinity).672* @return the {@link SocketOptions#SO_TIMEOUT SO_TIMEOUT} value673* @exception IOException if an I/O error occurs674* @since JDK1.1675* @see #setSoTimeout(int)676*/677public synchronized int getSoTimeout() throws IOException {678if (isClosed())679throw new SocketException("Socket is closed");680Object o = getImpl().getOption(SocketOptions.SO_TIMEOUT);681/* extra type safety */682if (o instanceof Integer) {683return ((Integer) o).intValue();684} else {685return 0;686}687}688689/**690* Enable/disable the {@link SocketOptions#SO_REUSEADDR SO_REUSEADDR}691* socket option.692* <p>693* When a TCP connection is closed the connection may remain694* in a timeout state for a period of time after the connection695* is closed (typically known as the {@code TIME_WAIT} state696* or {@code 2MSL} wait state).697* For applications using a well known socket address or port698* it may not be possible to bind a socket to the required699* {@code SocketAddress} if there is a connection in the700* timeout state involving the socket address or port.701* <p>702* Enabling {@link SocketOptions#SO_REUSEADDR SO_REUSEADDR} prior to703* binding the socket using {@link #bind(SocketAddress)} allows the socket704* to be bound even though a previous connection is in a timeout state.705* <p>706* When a {@code ServerSocket} is created the initial setting707* of {@link SocketOptions#SO_REUSEADDR SO_REUSEADDR} is not defined.708* Applications can use {@link #getReuseAddress()} to determine the initial709* setting of {@link SocketOptions#SO_REUSEADDR SO_REUSEADDR}.710* <p>711* The behaviour when {@link SocketOptions#SO_REUSEADDR SO_REUSEADDR} is712* enabled or disabled after a socket is bound (See {@link #isBound()})713* is not defined.714*715* @param on whether to enable or disable the socket option716* @exception SocketException if an error occurs enabling or717* disabling the {@link SocketOptions#SO_REUSEADDR SO_REUSEADDR}718* socket option, or the socket is closed.719* @since 1.4720* @see #getReuseAddress()721* @see #bind(SocketAddress)722* @see #isBound()723* @see #isClosed()724*/725public void setReuseAddress(boolean on) throws SocketException {726if (isClosed())727throw new SocketException("Socket is closed");728getImpl().setOption(SocketOptions.SO_REUSEADDR, Boolean.valueOf(on));729}730731/**732* Tests if {@link SocketOptions#SO_REUSEADDR SO_REUSEADDR} is enabled.733*734* @return a {@code boolean} indicating whether or not735* {@link SocketOptions#SO_REUSEADDR SO_REUSEADDR} is enabled.736* @exception SocketException if there is an error737* in the underlying protocol, such as a TCP error.738* @since 1.4739* @see #setReuseAddress(boolean)740*/741public boolean getReuseAddress() throws SocketException {742if (isClosed())743throw new SocketException("Socket is closed");744return ((Boolean) (getImpl().getOption(SocketOptions.SO_REUSEADDR))).booleanValue();745}746747/**748* Returns the implementation address and implementation port of749* this socket as a {@code String}.750* <p>751* If there is a security manager set, its {@code checkConnect} method is752* called with the local address and {@code -1} as its arguments to see753* if the operation is allowed. If the operation is not allowed,754* an {@code InetAddress} representing the755* {@link InetAddress#getLoopbackAddress loopback} address is returned as756* the implementation address.757*758* @return a string representation of this socket.759*/760public String toString() {761if (!isBound())762return "ServerSocket[unbound]";763InetAddress in;764if (System.getSecurityManager() != null)765in = InetAddress.getLoopbackAddress();766else767in = impl.getInetAddress();768return "ServerSocket[addr=" + in +769",localport=" + impl.getLocalPort() + "]";770}771772void setBound() {773bound = true;774}775776void setCreated() {777created = true;778}779780/**781* The factory for all server sockets.782*/783private static SocketImplFactory factory = null;784785/**786* Sets the server socket implementation factory for the787* application. The factory can be specified only once.788* <p>789* When an application creates a new server socket, the socket790* implementation factory's {@code createSocketImpl} method is791* called to create the actual socket implementation.792* <p>793* Passing {@code null} to the method is a no-op unless the factory794* was already set.795* <p>796* If there is a security manager, this method first calls797* the security manager's {@code checkSetFactory} method798* to ensure the operation is allowed.799* This could result in a SecurityException.800*801* @param fac the desired factory.802* @exception IOException if an I/O error occurs when setting the803* socket factory.804* @exception SocketException if the factory has already been defined.805* @exception SecurityException if a security manager exists and its806* {@code checkSetFactory} method doesn't allow the operation.807* @see java.net.SocketImplFactory#createSocketImpl()808* @see SecurityManager#checkSetFactory809*/810public static synchronized void setSocketFactory(SocketImplFactory fac) throws IOException {811if (factory != null) {812throw new SocketException("factory already defined");813}814SecurityManager security = System.getSecurityManager();815if (security != null) {816security.checkSetFactory();817}818factory = fac;819}820821/**822* Sets a default proposed value for the823* {@link SocketOptions#SO_RCVBUF SO_RCVBUF} option for sockets824* accepted from this {@code ServerSocket}. The value actually set825* in the accepted socket must be determined by calling826* {@link Socket#getReceiveBufferSize()} after the socket827* is returned by {@link #accept()}.828* <p>829* The value of {@link SocketOptions#SO_RCVBUF SO_RCVBUF} is used both to830* set the size of the internal socket receive buffer, and to set the size831* of the TCP receive window that is advertized to the remote peer.832* <p>833* It is possible to change the value subsequently, by calling834* {@link Socket#setReceiveBufferSize(int)}. However, if the application835* wishes to allow a receive window larger than 64K bytes, as defined by RFC1323836* then the proposed value must be set in the ServerSocket <B>before</B>837* it is bound to a local address. This implies, that the ServerSocket must be838* created with the no-argument constructor, then setReceiveBufferSize() must839* be called and lastly the ServerSocket is bound to an address by calling bind().840* <p>841* Failure to do this will not cause an error, and the buffer size may be set to the842* requested value but the TCP receive window in sockets accepted from843* this ServerSocket will be no larger than 64K bytes.844*845* @exception SocketException if there is an error846* in the underlying protocol, such as a TCP error.847*848* @param size the size to which to set the receive buffer849* size. This value must be greater than 0.850*851* @exception IllegalArgumentException if the852* value is 0 or is negative.853*854* @since 1.4855* @see #getReceiveBufferSize856*/857public synchronized void setReceiveBufferSize (int size) throws SocketException {858if (!(size > 0)) {859throw new IllegalArgumentException("negative receive size");860}861if (isClosed())862throw new SocketException("Socket is closed");863getImpl().setOption(SocketOptions.SO_RCVBUF, new Integer(size));864}865866/**867* Gets the value of the {@link SocketOptions#SO_RCVBUF SO_RCVBUF} option868* for this {@code ServerSocket}, that is the proposed buffer size that869* will be used for Sockets accepted from this {@code ServerSocket}.870*871* <p>Note, the value actually set in the accepted socket is determined by872* calling {@link Socket#getReceiveBufferSize()}.873* @return the value of the {@link SocketOptions#SO_RCVBUF SO_RCVBUF}874* option for this {@code Socket}.875* @exception SocketException if there is an error876* in the underlying protocol, such as a TCP error.877* @see #setReceiveBufferSize(int)878* @since 1.4879*/880public synchronized int getReceiveBufferSize()881throws SocketException{882if (isClosed())883throw new SocketException("Socket is closed");884int result = 0;885Object o = getImpl().getOption(SocketOptions.SO_RCVBUF);886if (o instanceof Integer) {887result = ((Integer)o).intValue();888}889return result;890}891892/**893* Sets performance preferences for this ServerSocket.894*895* <p> Sockets use the TCP/IP protocol by default. Some implementations896* may offer alternative protocols which have different performance897* characteristics than TCP/IP. This method allows the application to898* express its own preferences as to how these tradeoffs should be made899* when the implementation chooses from the available protocols.900*901* <p> Performance preferences are described by three integers902* whose values indicate the relative importance of short connection time,903* low latency, and high bandwidth. The absolute values of the integers904* are irrelevant; in order to choose a protocol the values are simply905* compared, with larger values indicating stronger preferences. If the906* application prefers short connection time over both low latency and high907* bandwidth, for example, then it could invoke this method with the values908* {@code (1, 0, 0)}. If the application prefers high bandwidth above low909* latency, and low latency above short connection time, then it could910* invoke this method with the values {@code (0, 1, 2)}.911*912* <p> Invoking this method after this socket has been bound913* will have no effect. This implies that in order to use this capability914* requires the socket to be created with the no-argument constructor.915*916* @param connectionTime917* An {@code int} expressing the relative importance of a short918* connection time919*920* @param latency921* An {@code int} expressing the relative importance of low922* latency923*924* @param bandwidth925* An {@code int} expressing the relative importance of high926* bandwidth927*928* @since 1.5929*/930public void setPerformancePreferences(int connectionTime,931int latency,932int bandwidth)933{934/* Not implemented yet */935}936937}938939940