Java Reference
In-Depth Information
SO_REUSEADDR
The SO_REUSEADDR option for server sockets is very similar to the same option for
client sockets, discussed in the previous chapter. It determines whether a new socket
will be allowed to bind to a previously used port while there might still be data traversing
the network addressed to the old socket. As you probably expect, there are two methods
to get and set this option:
public boolean getReuseAddress () throws SocketException
public void setReuseAddress ( boolean on ) throws SocketException
The default value is platform dependent. This code fragment determines the default
value by creating a new ServerSocket and then calling getReuseAddress() :
ServerSocket ss = new ServerSocket ( 10240 );
System . out . println ( "Reusable: " + ss . getReuseAddress ());
On the Linux and Mac OS X boxes where I tested this code, server sockets were reusable
by default.
SO_RCVBUF
The SO_RCVBUF option sets the default receive buffer size for client sockets accepted
by the server socket. It's read and written by these two methods:
public int getReceiveBufferSize () throws SocketException
public void setReceiveBufferSize ( int size ) throws SocketException
Setting SO_RCVBUF on a server socket is like calling setReceiveBufferSize() on
each individual socket returned by accept() (except that you can't change the receive
buffer size after the socket has been accepted). Recall from the previous chapter that
this option suggests a value for the size of the individual IP packets in the stream. Faster
connections will want to use larger buffers, although most of the time the default value
is fine.
You can set this option before or after the server socket is bound, unless you want to set
a receive buffer size larger than 64K. In that case, you must set the option on an unbound
ServerSocket before binding it. For example:
ServerSocket ss = new ServerSocket ();
int receiveBufferSize = ss . getReceiveBufferSize ();
if ( receiveBufferSize < 131072 ) {
ss . setReceiveBufferSize ( 131072 );
}
ss . bind ( new InetSocketAddress ( 8000 ));
//...
Search WWH ::




Custom Search