NIO2 API

org.classpath.icedtea.java.nio.channels
Class DatagramChannel

java.lang.Object
  extended by java.nio.channels.spi.AbstractInterruptibleChannel
      extended by java.nio.channels.SelectableChannel
          extended by java.nio.channels.spi.AbstractSelectableChannel
              extended by java.nio.channels.DatagramChannel
                  extended by org.classpath.icedtea.java.nio.channels.DatagramChannel
All Implemented Interfaces:
Closeable, ByteChannel, Channel, GatheringByteChannel, InterruptibleChannel, ReadableByteChannel, ScatteringByteChannel, WritableByteChannel, MulticastChannel, NetworkChannel

public abstract class DatagramChannel
extends DatagramChannel
implements MulticastChannel

A selectable channel for datagram-oriented sockets.

A datagram channel is created by invoking one of the open methods of this class. It is not possible to create a channel for an arbitrary, pre-existing datagram socket. A newly-created datagram channel is open but not connected. A datagram channel need not be connected in order for the send and receive methods to be used. A datagram channel may be connected, by invoking its connect method, in order to avoid the overhead of the security checks are otherwise performed as part of every send and receive operation. A datagram channel must be connected in order to use the read and write methods, since those methods do not accept or return socket addresses.

Once connected, a datagram channel remains connected until it is disconnected or closed. Whether or not a datagram channel is connected may be determined by invoking its isConnected method.

Socket options are configured using the setOption method. A datagram channel to an Internet Protocol socket supports the following options:

Option Name Description
SO_SNDBUF The size of the socket send buffer
SO_RCVBUF The size of the socket receive buffer
SO_REUSEADDR Re-use address
SO_BROADCAST Allow transmission of broadcast datagrams
IP_TOS The Type of Service (ToS) octet in the Internet Protocol (IP) header
IP_MULTICAST_IF The network interface for Internet Protocol (IP) multicast datagrams
IP_MULTICAST_TTL The time-to-live for Internet Protocol (IP) multicast datagrams
IP_MULTICAST_LOOP Loopback for Internet Protocol (IP) multicast datagrams
Additional (implementation specific) options may also be supported.

Datagram channels are safe for use by multiple concurrent threads. They support concurrent reading and writing, though at most one thread may be reading and at most one thread may be writing at any given time.

Since:
1.4

Constructor Summary
protected DatagramChannel(SelectorProvider provider)
          Initializes a new instance of this class.
 
Method Summary
abstract  DatagramChannel bind(SocketAddress local)
          Binds the channel's socket to a local address.
abstract  SocketAddress getRemoteAddress()
           Returns the remote address to which this channel's socket is connected.
static DatagramChannel open()
          Opens a datagram channel.
static DatagramChannel open(ProtocolFamily family)
          Opens a datagram channel.
abstract
<T> DatagramChannel
setOption(SocketOption<T> name, T value)
          Sets the value of a socket option.
 
Methods inherited from class java.nio.channels.DatagramChannel
connect, disconnect, isConnected, read, read, read, receive, send, socket, validOps, write, write, write
 
Methods inherited from class java.nio.channels.spi.AbstractSelectableChannel
blockingLock, configureBlocking, implCloseChannel, implCloseSelectableChannel, implConfigureBlocking, isBlocking, isRegistered, keyFor, provider, register
 
Methods inherited from class java.nio.channels.SelectableChannel
register
 
Methods inherited from class java.nio.channels.spi.AbstractInterruptibleChannel
begin, close, end, isOpen
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface org.classpath.icedtea.java.nio.channels.MulticastChannel
join, join
 
Methods inherited from interface org.classpath.icedtea.java.nio.channels.NetworkChannel
getLocalAddress, getOption, supportedOptions
 
Methods inherited from interface java.nio.channels.Channel
close, isOpen
 

Constructor Detail

DatagramChannel

protected DatagramChannel(SelectorProvider provider)
Initializes a new instance of this class.

Method Detail

open

public static DatagramChannel open()
                            throws IOException
Opens a datagram channel.

The new channel is created by invoking the openDatagramChannel method of the system-wide default SelectorProvider object. The channel will not be connected.

Returns:
A new datagram channel
Throws:
IOException - If an I/O error occurs

open

public static DatagramChannel open(ProtocolFamily family)
                            throws IOException
Opens a datagram channel.

The family parameter is used to specify the ProtocolFamily. If the datagram channel is to be used for IP multicasing then this should correspond to the address type of the multicast groups that this channel will join.

The new channel is created by invoking the openDatagramChannel method of the system-wide default SelectorProvider object. The channel will not be connected.

Parameters:
family - The protocol family
Returns:
A new datagram channel
Throws:
UnsupportedOperationException - If the specified protocol family is not supported. For example, suppose the parameter is specified as StandardProtocolFamily.INET6 but IPv6 is not enabled on the platform.
IOException - If an I/O error occurs
Since:
1.7

bind

public abstract DatagramChannel bind(SocketAddress local)
                              throws IOException
Description copied from interface: NetworkChannel
Binds the channel's socket to a local address.

This method is used to establish an association between the socket and a local address. Once an association is established then the socket remains bound until the channel is closed. If the local parameter has the value null then the socket will be bound to an address that is assigned automatically.

Specified by:
bind in interface NetworkChannel
Parameters:
local - The address to bind the socket, or null to bind the socket to an automatically assigned socket address
Returns:
This channel
Throws:
AlreadyBoundException - If the socket is already bound
UnsupportedAddressTypeException - If the type of the given address is not supported
ClosedChannelException - If the channel is closed
IOException - If some other I/O error occurs
SecurityException - If a security manager has been installed and its checkListen method denies the operation
Since:
1.7
See Also:
NetworkChannel.getLocalAddress()

setOption

public abstract <T> DatagramChannel setOption(SocketOption<T> name,
                                              T value)
                                   throws IOException
Description copied from interface: NetworkChannel
Sets the value of a socket option.

Specified by:
setOption in interface NetworkChannel
Parameters:
name - The socket option
value - The value of the socket option. A value of null may be a valid value for some socket options.
Returns:
This channel
Throws:
IllegalArgumentException - If the value is not a valid value for this socket option
ClosedChannelException - If this channel is closed
IOException - If an I/O error occurs
Since:
1.7
See Also:
StandardSocketOption

getRemoteAddress

public abstract SocketAddress getRemoteAddress()
                                        throws IOException
Returns the remote address to which this channel's socket is connected.

Returns:
The remote address; null if the channel's socket is not connected
Throws:
ClosedChannelException - If the channel is closed
IOException - If an I/O error occurs
Since:
1.7

NIO2 API

Copyright © 2007, 2011, Oracle and/or its affiliates. All rights reserved.