Most visited

Recently visited

Added in API level 1

DatagramChannel

public abstract class DatagramChannel
extends AbstractSelectableChannel implements ByteChannel, ScatteringByteChannel, GatheringByteChannel, NetworkChannel

java.lang.Object
   ↳ java.nio.channels.spi.AbstractInterruptibleChannel
     ↳ java.nio.channels.SelectableChannel
       ↳ java.nio.channels.spi.AbstractSelectableChannel
         ↳ java.nio.channels.DatagramChannel


面向数据报套接字的可选通道。

通过调用该类的open方法之一来创建数据报通道。 无法为任意的,预先存在的数据报套接字创建一个通道。 新创建的数据报通道已打开但未连接。 数据报通道不需要连接,以便使用sendreceive方法。 数据报通道可以通过调用其connect方法来连接,以避免作为每个发送和接收操作的一部分执行安全检查的开销。 必须连接数据报通道才能使用readwrite方法,因为这些方法不接受或返回套接字地址。

一旦连接,数据报通道将保持连接状态,直到断开连接或关闭。 数据报通道是否连接可以通过调用其isConnected方法来确定。

套接字选项使用setOption方法进行配置。 到Internet协议套接字的数据报通道支持以下选项:

Option Name 描述
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.

数据报通道可安全地用于多个并发线程。 它们支持并发读写,尽管最多只有一个线程可能正在读取,并且在任何给定时间最多只有一个线程可能正在写入。

Summary

Protected constructors

DatagramChannel(SelectorProvider provider)

初始化此类的新实例。

Public methods

abstract DatagramChannel bind(SocketAddress local)

将通道的套接字绑定到本地地址。

abstract DatagramChannel connect(SocketAddress remote)

连接此通道的插座。

abstract DatagramChannel disconnect()

断开此通道的插座。

abstract SocketAddress getRemoteAddress()

返回此通道套接字连接的远程地址。

abstract boolean isConnected()

确定是否连接了此通道的套接字。

static DatagramChannel open()

打开数据报通道。

static DatagramChannel open(ProtocolFamily family)

打开数据报通道。

abstract long read(ByteBuffer[] dsts, int offset, int length)

从此通道读取数据报。

abstract int read(ByteBuffer dst)

从此通道读取数据报。

final long read(ByteBuffer[] dsts)

从此通道读取数据报。

abstract SocketAddress receive(ByteBuffer dst)

通过此通道接收数据报。

abstract int send(ByteBuffer src, SocketAddress target)

通过此通道发送数据报。

abstract <T> DatagramChannel setOption(SocketOption<T> name, T value)

设置套接字选项的值。

abstract DatagramSocket socket()

检索与此通道关联的数据报套接字。

final int validOps()

返回标识此频道支持的操作的操作集。

abstract int write(ByteBuffer src)

将数据报写入此通道。

final long write(ByteBuffer[] srcs)

将数据报写入此通道。

abstract long write(ByteBuffer[] srcs, int offset, int length)

将数据报写入此通道。

Inherited methods

From class java.nio.channels.spi.AbstractSelectableChannel
From class java.nio.channels.SelectableChannel
From class java.nio.channels.spi.AbstractInterruptibleChannel
From class java.lang.Object
From interface java.nio.channels.Channel
From interface java.nio.channels.InterruptibleChannel
From interface java.nio.channels.ScatteringByteChannel
From interface java.nio.channels.GatheringByteChannel
From interface java.nio.channels.NetworkChannel
From interface java.io.Closeable
From interface java.nio.channels.ReadableByteChannel
From interface java.nio.channels.WritableByteChannel
From interface java.lang.AutoCloseable

Protected constructors

DatagramChannel

Added in API level 1
DatagramChannel (SelectorProvider provider)

初始化此类的新实例。

Parameters
provider SelectorProvider

Public methods

bind

Added in API level 24
DatagramChannel bind (SocketAddress local)

将通道的套接字绑定到本地地址。

此方法用于在套接字和本地地址之间建立关联。 一旦建立了关联,套接字就会保持绑定状态,直到通道关闭。 如果local参数的值为null则套接字将被绑定到自动分配的地址。

Parameters
local SocketAddress: The address to bind the socket, or null to bind the socket to an automatically assigned socket address
Returns
DatagramChannel This channel
Throws
AlreadyBoundException
UnsupportedAddressTypeException
ClosedChannelException
IOException
SecurityException If a security manager has been installed and its checkListen method denies the operation

connect

Added in API level 1
DatagramChannel connect (SocketAddress remote)

连接此通道的插座。

通道的套接字配置为只接收来自给定远程对等地址的数据报并发送数据报给给定的远程对等地址。 一旦连接,数据报可能不会被接收或发送到任何其他地址。 数据报套接字保持连接状态,直到它明确断开连接或关闭。

此方法执行与DatagramSocket类的connect方法完全相同的安全检查。 也就是说,如果安装了安全管理器,那么这种方法将验证其方法checkAcceptcheckConnect允许分别从给定的远程地址接收和发送数据报。

这个方法可以在任何时候调用。 它对调用时已经进行的读取或写入操作没有任何影响。 如果此通道的套接字未被绑定,则此方法将首先使套接字绑定到自动分配的地址,就像调用参数为nullbind方法null

Parameters
remote SocketAddress: The remote address to which this channel is to be connected
Returns
DatagramChannel This datagram channel
Throws
ClosedChannelException If this channel is closed
AsynchronousCloseException If another thread closes this channel while the connect operation is in progress
ClosedByInterruptException If another thread interrupts the current thread while the connect operation is in progress, thereby closing the channel and setting the current thread's interrupt status
SecurityException If a security manager has been installed and it does not permit access to the given remote address
IOException If some other I/O error occurs

disconnect

Added in API level 1
DatagramChannel disconnect ()

断开此通道的插座。

通道的套接字配置为只要安全管理器(如果已安装)允许它接收数据报并将数据报发送到任何远程地址。

这个方法可以在任何时候调用。 它对调用时已经进行的读取或写入操作没有任何影响。

如果此通道的套接字未连接,或者通道已关闭,则调用此方法不起作用。

Returns
DatagramChannel This datagram channel
Throws
IOException If some other I/O error occurs

getRemoteAddress

Added in API level 24
SocketAddress getRemoteAddress ()

返回此通道套接字连接的远程地址。

Returns
SocketAddress 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

isConnected

Added in API level 1
boolean isConnected ()

确定是否连接了此通道的套接字。

Returns
boolean true if, and only if, this channel's socket is open and connected

open

Added in API level 1
DatagramChannel open ()

打开数据报通道。

新通道是通过调用系统范围的默认SelectorProvider对象的openDatagramChannel方法创建的。 该频道不会连接。

通道套接字的ProtocolFamily是平台(可能是配置)相关的,因此未指定。 open允许在打开数据报通道时选择协议族,并且应该用于打开用于Internet协议多播的数据报通道。

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

open

Added in API level 24
DatagramChannel open (ProtocolFamily family)

打开数据报通道。

family参数用于指定ProtocolFamily 如果数据报通道用于IP多路传输,那么这应该对应于该通道将加入的多播组的地址类型。

新通道是通过调用系统范围的默认SelectorProvider对象的openDatagramChannel方法创建的。 该频道不会连接。

Parameters
family ProtocolFamily: The protocol family
Returns
DatagramChannel 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

read

Added in API level 1
long read (ByteBuffer[] dsts, 
                int offset, 
                int length)

从此通道读取数据报。

只有在此通道的套接字已连接的情况下才可以调用此方法,并且它只接受来自套接字对等方的数据报。 如果数据报中的字节多于保留在给定缓冲区中的字节,那么数据报的其余部分将被静默丢弃。 否则,此方法的行为与ScatteringByteChannel界面中的规定完全相同。

Parameters
dsts ByteBuffer: The buffers into which bytes are to be transferred
offset int: The offset within the buffer array of the first buffer into which bytes are to be transferred; must be non-negative and no larger than dsts.length
length int: The maximum number of buffers to be accessed; must be non-negative and no larger than dsts.length - offset
Returns
long The number of bytes read, possibly zero, or -1 if the channel has reached end-of-stream
Throws
NotYetConnectedException If this channel's socket is not connected
IOException

read

Added in API level 1
int read (ByteBuffer dst)

从此通道读取数据报。

只有在此通道的套接字已连接的情况下才可以调用此方法,并且它只接受来自套接字对等方的数据报。 如果数据报中有更多的字节比保留在给定的缓冲区中,那么数据报的其余部分将被静默丢弃。 否则,此方法的行为与ReadableByteChannel界面中指定的ReadableByteChannel

Parameters
dst ByteBuffer: The buffer into which bytes are to be transferred
Returns
int The number of bytes read, possibly zero, or -1 if the channel has reached end-of-stream
Throws
NotYetConnectedException If this channel's socket is not connected
IOException

read

Added in API level 1
long read (ByteBuffer[] dsts)

从此通道读取数据报。

只有在此通道的套接字已连接的情况下才可以调用此方法,并且它只接受来自套接字对等方的数据报。 如果数据报中的字节多于保留在给定缓冲区中的字节,那么数据报的其余部分将被静默丢弃。 否则,此方法的行为与ScatteringByteChannel界面中的规定完全相同。

Parameters
dsts ByteBuffer: The buffers into which bytes are to be transferred
Returns
long The number of bytes read, possibly zero, or -1 if the channel has reached end-of-stream
Throws
NotYetConnectedException If this channel's socket is not connected
IOException

receive

Added in API level 1
SocketAddress receive (ByteBuffer dst)

通过此通道接收数据报。

如果数据报立即可用,或者如果该通道处于阻塞模式并且最终变为可用,则将数据报复制到给定字节缓冲区中,并返回其源地址。 如果此通道处于非阻塞模式并且数据报不能立即可用,则此方法立即返回null

数据报从当前位置开始传送到给定的字节缓冲区,就像通过常规的read操作一样。 如果缓冲区中剩余的字节数少于保存数据报所需的字节数,那么数据报的其余部分将被静默丢弃。

此方法执行与DatagramSocket类的receive方法完全相同的安全检查。 也就是说,如果套接字未连接到特定的远程地址和每个数据报接收的该方法验证源的地址和端口号是由安全管理器的所允许的安全管理器已安装然后checkAccept方法。 首先通过connect方法连接套接字,可以避免此安全检查的开销。

这个方法可以在任何时候调用。 然而,如果另一个线程已经在这个通道上启动了一个读操作,那么这个方法的调用将会阻塞,直到第一个操作完成。 如果此通道的套接字未绑定,则此方法将首先使套接字绑定到自动分配的地址,就好像调用参数为nullbind方法null

Parameters
dst ByteBuffer: The buffer into which the datagram is to be transferred
Returns
SocketAddress The datagram's source address, or null if this channel is in non-blocking mode and no datagram was immediately available
Throws
ClosedChannelException If this channel is closed
AsynchronousCloseException If another thread closes this channel while the read operation is in progress
ClosedByInterruptException If another thread interrupts the current thread while the read operation is in progress, thereby closing the channel and setting the current thread's interrupt status
SecurityException If a security manager has been installed and it does not permit datagrams to be accepted from the datagram's sender
IOException If some other I/O error occurs

send

Added in API level 1
int send (ByteBuffer src, 
                SocketAddress target)

通过此通道发送数据报。

如果此通道处于非阻塞模式,并且基础输出缓冲区中有足够的空间,或者此通道处于阻塞模式并且有足够的空间可用,则给定缓冲区中的剩余字节将作为单个数据报传输到给定目标地址。

数据报从字节缓冲区传输,就像通过常规的 write操作一样。

此方法执行与DatagramSocket类的send方法完全相同的安全检查。 也就是说,如果套接字没有连接到特定的远程地址,并且已经安装了安全管理器,那么对于发送的每个数据报,此方法将验证安全管理器的方法checkConnect允许目标地址和端口号。 首先通过connect方法连接套接字,可以避免此安全检查的开销。

这个方法可以在任何时候调用。 但是,如果另一个线程已经在此通道上启动了写操作,则此方法的调用将会阻塞,直到第一个操作完成。 如果此通道的套接字未绑定,则此方法将首先使套接字绑定到自动分配的地址,就像调用bind方法的参数为null

Parameters
src ByteBuffer: The buffer containing the datagram to be sent
target SocketAddress: The address to which the datagram is to be sent
Returns
int The number of bytes sent, which will be either the number of bytes that were remaining in the source buffer when this method was invoked or, if this channel is non-blocking, may be zero if there was insufficient room for the datagram in the underlying output buffer
Throws
ClosedChannelException If this channel is closed
AsynchronousCloseException If another thread closes this channel while the read operation is in progress
ClosedByInterruptException If another thread interrupts the current thread while the read operation is in progress, thereby closing the channel and setting the current thread's interrupt status
SecurityException If a security manager has been installed and it does not permit datagrams to be sent to the given address
IOException If some other I/O error occurs

setOption

Added in API level 24
DatagramChannel setOption (SocketOption<T> name, 
                T value)

设置套接字选项的值。

Parameters
name SocketOption: The socket option
value T: The value of the socket option. A value of null may be a valid value for some socket options.
Returns
DatagramChannel This channel
Throws
UnsupportedOperationException
IllegalArgumentException
ClosedChannelException
IOException

socket

Added in API level 1
DatagramSocket socket ()

检索与此通道关联的数据报套接字。

返回的对象不会声明任何未在 DatagramSocket类中声明的公共方法。

Returns
DatagramSocket A datagram socket associated with this channel

validOps

Added in API level 1
int validOps ()

返回标识此频道支持的操作的操作集。

数据报通道支持读取和写入,因此此方法返回 ( OP_READ | OP_WRITE )

Returns
int The valid-operation set

write

Added in API level 1
int write (ByteBuffer src)

将数据报写入此通道。

如果此通道的套接字已连接,则只能调用此方法,在这种情况下,它将数据报直接发送到套接字的对等方。 否则,其行为与WritableByteChannel界面中的指定完全相同。

Parameters
src ByteBuffer: The buffer from which bytes are to be retrieved
Returns
int The number of bytes written, possibly zero
Throws
NotYetConnectedException If this channel's socket is not connected
IOException

write

Added in API level 1
long write (ByteBuffer[] srcs)

将数据报写入此通道。

如果此通道的套接字已连接,则只能调用此方法,在这种情况下,它将数据报直接发送到套接字的对等方。 否则,其行为与GatheringByteChannel界面中的规定完全相同。

Parameters
srcs ByteBuffer: The buffers from which bytes are to be retrieved
Returns
long The number of bytes sent, which will be either the number of bytes that were remaining in the source buffer when this method was invoked or, if this channel is non-blocking, may be zero if there was insufficient room for the datagram in the underlying output buffer
Throws
NotYetConnectedException If this channel's socket is not connected
IOException

write

Added in API level 1
long write (ByteBuffer[] srcs, 
                int offset, 
                int length)

将数据报写入此通道。

如果此通道的套接字已连接,则只能调用此方法,在这种情况下,它将数据报直接发送到套接字的对等方。 否则,其行为与GatheringByteChannel界面中的指定完全相同。

Parameters
srcs ByteBuffer: The buffers from which bytes are to be retrieved
offset int: The offset within the buffer array of the first buffer from which bytes are to be retrieved; must be non-negative and no larger than srcs.length
length int: The maximum number of buffers to be accessed; must be non-negative and no larger than srcs.length - offset
Returns
long The number of bytes sent, which will be either the number of bytes that were remaining in the source buffer when this method was invoked or, if this channel is non-blocking, may be zero if there was insufficient room for the datagram in the underlying output buffer
Throws
NotYetConnectedException If this channel's socket is not connected
IOException

Hooray!