Java Reference
In-Depth Information
SO_REUSEADDR: false
SO_SNDBUF: 131072
TCP_NODELAY: false
SO_RCVBUF: 131072
UnixAsynchronousServerSocketChannelImpl supports:
SO_REUSEADDR: true
SO_RCVBUF: 131072
DatagramChannelImpl supports:
IP_MULTICAST_TTL: 1
SO_BROADCAST: false
SO_REUSEADDR: false
IP_MULTICAST_IF: null
IP_TOS: 0
IP_MULTICAST_LOOP: true
SO_SNDBUF: 9216
SO_RCVBUF: 196724
Readiness Selection
For network programming, the second part of the new I/O APIs is readiness selection,
the ability to choose a socket that will not block when read or written. This is primarily
of interest to servers, although clients running multiple simultaneous connections with
several windows open—such as a web spider or a browser—can take advantage of it as
well.
In order to perform readiness selection, different channels are registered with a
Selec
tor
object. Each channel is assigned a
SelectionKey
. The program can then ask the
Selector
object for the set of keys to the channels that are ready to perform the operation
you want to perform without blocking.
The Selector Class
The only constructor in
Selector
is protected. Normally, a new selector is created by
invoking the static factory method
Selector.open()
:
public
static
Selector
open
()
throws
IOException
The next step is to add channels to the selector. There are no methods in the
Selec
tor
class to add a channel. The
register()
method is declared in the
SelectableChan
nel
class. Not all channels are selectable—in particular,
FileChannel
s aren't selectable
—but all network channels are. Thus, the channel is registered with a selector by passing
the selector to one of the channel's register methods:
public
final
SelectionKey
register
(
Selector
sel
,
int
ops
)
throws
ClosedChannelException
public
final
SelectionKey
register
(
Selector
sel
,
int
ops
,
Object
att
)
throws
ClosedChannelException