Java Reference
In-Depth Information
ing to set an option the channel doesn't support throws an UnsupportedOperationEx
ception .
Example 11-7 is a simple program to list all supported socket options for the different
types of network channels.
Example 11-7. Listing supported options
import java.io.* ;
import java.net.* ;
import java.nio.channels.* ;
public class OptionSupport {
public static void main ( String [] args ) throws IOException {
printOptions ( SocketChannel . open ());
printOptions ( ServerSocketChannel . open ());
printOptions ( AsynchronousSocketChannel . open ());
printOptions ( AsynchronousServerSocketChannel . open ());
printOptions ( DatagramChannel . open ());
}
private static void printOptions ( NetworkChannel channel ) throws IOException {
System . out . println ( channel . getClass (). getSimpleName () + " supports:" );
for ( SocketOption <?> option : channel . supportedOptions ()) {
System . out . println ( option . name () + ": " + channel . getOption ( option ));
}
System . out . println ();
channel . close ();
}
}
Here's the output showing which options are supported by which types of channels, and
what the default values are:
SocketChannelImpl supports:
SO_OOBINLINE: false
SO_REUSEADDR: false
SO_LINGER: -1
SO_KEEPALIVE: false
IP_TOS: 0
SO_SNDBUF: 131072
SO_RCVBUF: 131072
TCP_NODELAY: false
ServerSocketChannelImpl supports:
SO_REUSEADDR: true
SO_RCVBUF: 131072
UnixAsynchronousSocketChannelImpl supports:
SO_KEEPALIVE: false
Search WWH ::




Custom Search