`
Donald_Draper
  • 浏览: 979811 次
社区版块
存档分类
最新评论

DatagramChannel定义

    博客分类:
  • NIO
nio 
阅读更多
Channel接口定义:http://donald-draper.iteye.com/blog/2369111
AbstractInterruptibleChannel接口定义:http://donald-draper.iteye.com/blog/2369238
SelectableChannel接口定义:http://donald-draper.iteye.com/blog/2369317
SelectionKey定义:http://donald-draper.iteye.com/blog/2369499
SelectorProvider定义:http://donald-draper.iteye.com/blog/2369615
AbstractSelectableChannel定义:http://donald-draper.iteye.com/blog/2369742
NetworkChannel接口定义:http://donald-draper.iteye.com/blog/2369773
Selector定义:http://donald-draper.iteye.com/blog/2370015
AbstractSelector定义:http://donald-draper.iteye.com/blog/2370138
SelectorImpl分析 :http://donald-draper.iteye.com/blog/2370519
WindowsSelectorImpl解析一(FdMap,PollArrayWrapper):
http://donald-draper.iteye.com/blog/2370811
WindowsSelectorImpl解析二(选择操作,通道注册,通道反注册,选择器关闭等):
http://donald-draper.iteye.com/blog/2370862
ByteChannel,分散聚集通道接口的定义(SocketChannel):
http://donald-draper.iteye.com/blog/2371065
MembershipKey定义:http://donald-draper.iteye.com/blog/2372947
MulticastChanne接口定义:http://donald-draper.iteye.com/blog/2373009

package java.nio.channels;
import java.io.IOException;
import java.net.ProtocolFamily;
import java.net.DatagramSocket;
import java.net.SocketOption;
import java.net.SocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.spi.AbstractSelectableChannel;
import java.nio.channels.spi.SelectorProvider;

/**
 * A selectable channel for datagram-oriented sockets.
 *DatagramChannel是一个面向报文socket的可选择通道。
 *  A datagram channel is created by invoking one of the {@link #open 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 {@link #send
 * send} and {@link #receive receive} methods to be used.  A datagram channel may be
 * connected, by invoking its {@link #connect 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 {@link #read(java.nio.ByteBuffer) read} and {@link
 * #write(java.nio.ByteBuffer) write} methods, since those methods do not
 * accept or return socket addresses.
 *DatagramChannel通过#open创建。如果报文通道关联的DatagramSocket已经存在,则
 将不能与已经存在的DatagramSocket关联的DatagramChannel。新创建的报文通道是无连接的。
使用#send和#receive发送和接受报文是不需要建立连接的。如果想要在调用#send和#receive不进行
安全检查,可以先调用#connect,建立一个处于连接状态的通道。如果报文通道使用#read和#write方法
接受和发送报文,通道必须建立连接,因为这些方法不能接受和返回socket地址。
 * <p> 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 {@link #isConnected isConnected} method.
 *一旦报文通道连接建立,在断开连接和通道关闭前,报文通道都是可连接。我们可以通过
#isConnected判断一个报文通道是否处于连接状态。
 * <p> Socket options are configured using the {@link #setOption(SocketOption,Object)
 * setOption} method. A datagram channel to an Internet Protocol socket supports
 * the following options:
 Socket选项配置使用#setOption方法,报文通道支持如下选项:
 * <blockquote>
 * <table border>
 *   <tr>
 *     <th>Option Name</th>
 *     <th>Description</th>
 *   </tr>
 *   <tr>发送缓冲区
 *     <td> {@link java.net.StandardSocketOptions#SO_SNDBUF SO_SNDBUF} </td>
 *     <td> The size of the socket send buffer </td>
 *   </tr>
 *   <tr>接受缓存区
 *     <td> {@link java.net.StandardSocketOptions#SO_RCVBUF SO_RCVBUF} </td>
 *     <td> The size of the socket receive buffer </td>
 *   </tr>
 *   <tr>地址重用
 *     <td> {@link java.net.StandardSocketOptions#SO_REUSEADDR SO_REUSEADDR} </td>
 *     <td> Re-use address </td>
 *   </tr>
 *   <tr>是否支持报文广播传输
 *     <td> {@link java.net.StandardSocketOptions#SO_BROADCAST SO_BROADCAST} </td>
 *     <td> Allow transmission of broadcast datagrams </td>
 *   </tr>
 *   <tr>网络协议服务类型
 *     <td> {@link java.net.StandardSocketOptions#IP_TOS IP_TOS} </td>
 *     <td> The Type of Service (ToS) octet in the Internet Protocol (IP) header </td>
 *   </tr>
 *   <tr>多播网卡接口
 *     <td> {@link java.net.StandardSocketOptions#IP_MULTICAST_IF IP_MULTICAST_IF} </td>
 *     <td> The network interface for Internet Protocol (IP) multicast datagrams </td>
 *   </tr>
 *   <tr>
 *     <td> {@link java.net.StandardSocketOptions#IP_MULTICAST_TTL
 *       IP_MULTICAST_TTL} </td>多播报文存活时间
 *     <td> The [i]time-to-live[/i] for Internet Protocol (IP) multicast
 *       datagrams </td>
 *   </tr>
 *   <tr>
 *     <td> {@link java.net.StandardSocketOptions#IP_MULTICAST_LOOP
 *       IP_MULTICAST_LOOP} </td>是否支持多播回路地址
 *     <td> Loopback for Internet Protocol (IP) multicast datagrams </td>
 *   </tr>
 * </table>
 * </blockquote>
 * Additional (implementation specific) options may also be supported.
 *
 * <p> 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.  

 *DatagramChannel并发访问时,是线程安全的。尽管最多只支持一个线程进行读写操作,
 但DatagramChannel支持并发读写操作。
 * @author Mark Reinhold
 * @author JSR-51 Expert Group
 * @since 1.4
 */

public abstract class DatagramChannel
    extends AbstractSelectableChannel
    implements ByteChannel, ScatteringByteChannel, GatheringByteChannel, MulticastChannel
{
 /**
     * Initializes a new instance of this class.
     */
    protected DatagramChannel(SelectorProvider provider) {
        super(provider);
    }

    /**
     * Opens a datagram channel.
     *打开一个报文通道
     *  The new channel is created by invoking the {@link
     * java.nio.channels.spi.SelectorProvider#openDatagramChannel()
     * openDatagramChannel} method of the system-wide default {@link
     * java.nio.channels.spi.SelectorProvider} object.  The channel will not be
     * connected.
     *报文通道通过系统默认的SelectorProvider实现创建,新创建的通道是未连接。
     * <p> The {@link ProtocolFamily ProtocolFamily} of the channel's socket
     * is platform (and possibly configuration) dependent and therefore unspecified.
     * The {@link #open(ProtocolFamily) open} allows the protocol family to be
     * selected when opening a datagram channel, and should be used to open
     * datagram channels that are intended for Internet Protocol multicasting.
     *通道socket的协议ProtocolFamily依赖于平台,因此是不确定的。#open(ProtocolFamily方法,
     打开一个具体协议的通道socket,可以用于多播网络协议。
     * @return  A new datagram channel
     *
     * @throws  IOException
     *          If an I/O error occurs
     */
    public static DatagramChannel open() throws IOException {
        return SelectorProvider.provider().openDatagramChannel();
    }

    /**
     * Opens a datagram channel.
     *
     * <p> The {@code family} parameter is used to specify the {@link
     * 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.
     *如果报文通道用于IP多播,协议ProtocolFamily参数family为通道加入的多播组地址类型。
     * <p> The new channel is created by invoking the {@link
     * java.nio.channels.spi.SelectorProvider#openDatagramChannel(ProtocolFamily)
     * openDatagramChannel} method of the system-wide default {@link
     * java.nio.channels.spi.SelectorProvider} object.  The channel will not be
     * connected.
     *报文通道通过系统默认的SelectorProvider实现创建,新创建的通道是未连接。
     * @param   family
     *          The protocol family
     *
     * @return  A new datagram channel
     *
     * @throws  UnsupportedOperationException,系统不支持具体的协议
     *          If the specified protocol family is not supported. For example,
     *          suppose the parameter is specified as {@link
     *          java.net.StandardProtocolFamily#INET6 StandardProtocolFamily.INET6}
     *          but IPv6 is not enabled on the platform.
     * @throws  IOException
     *          If an I/O error occurs
     *
     * @since   1.7
     */
    public static DatagramChannel open(ProtocolFamily family) throws IOException {
        return SelectorProvider.provider().openDatagramChannel(family);
    }

    /**
     * Returns an operation set identifying this channel's supported
     * operations.
     *返回通道支持的操作事件集,默认支持读写操作事件
     * <p> Datagram channels support reading and writing, so this method
     * returns <tt>(</tt>{@link SelectionKey#OP_READ} <tt>|</tt> {@link
     * SelectionKey#OP_WRITE}<tt>)</tt>.  

     *
     * @return  The valid-operation set
     */
    public final int validOps() {
        return (SelectionKey.OP_READ
                | SelectionKey.OP_WRITE);
    }


    // -- Socket-specific operations --

    /**
     * @throws  AlreadyBoundException               {@inheritDoc}
     * @throws  UnsupportedAddressTypeException     {@inheritDoc}
     * @throws  ClosedChannelException              {@inheritDoc}
     * @throws  IOException                         {@inheritDoc}
     * @throws  SecurityException
     *          If a security manager has been installed and its {@link
     *          SecurityManager#checkListen checkListen} method denies the
     *          operation
     *绑定SocketAddress地址
     * @since 1.7
     */
    public abstract DatagramChannel bind(SocketAddress local)
        throws IOException;

    /**
     * @throws  UnsupportedOperationException           {@inheritDoc}
     * @throws  IllegalArgumentException                {@inheritDoc}
     * @throws  ClosedChannelException                  {@inheritDoc}
     * @throws  IOException                             {@inheritDoc}
     *配置通道选项
     * @since 1.7
     */
    public abstract <T> DatagramChannel setOption(SocketOption<T> name, T value)
        throws IOException;

    /**
     * Retrieves a datagram socket associated with this channel.
     *获取与通道关联的DatagramSocket
     *  The returned object will not declare any public methods that are not
     * declared in the {@link java.net.DatagramSocket} class.  

     *返回的DatagramSocket不能声明任何在java.net.DatagramSocket中没有声明的public方法
     * @return  A datagram socket associated with this channel
     */
    public abstract DatagramSocket socket();

    /**
     * Tells whether or not this channel's socket is connected.
     *判断一个通道是否连接
     * @return  {@code true} if, and only if, this channel's socket
     *          is {@link #isOpen open} and connected
     */
    public abstract boolean isConnected();

    /**
     * Connects this channel's socket.
     *连接通道socket
     *  The channel's socket is configured so that it only receives
     * datagrams from, and sends datagrams to, the given remote <i>peer</i>
     * address.  Once connected, datagrams may not be received from or sent to
     * any other address.  A datagram socket remains connected until it is
     * explicitly disconnected or until it is closed.
     *配置通道的socket,以便可以从给定的远程peer接受报文,或发送报文给peer。
     一旦连接,通道不能从其他地址接受报文,或者发送报文给其他地址。在通道显示
disconnected或关闭之前,DatagramSocket仍处于连接状态。
     * <p> This method performs exactly the same security checks as the {@link
     * java.net.DatagramSocket#connect connect} method of the {@link
     * java.net.DatagramSocket} class.  That is, if a security manager has been
     * installed then this method verifies that its {@link
     * java.lang.SecurityManager#checkAccept checkAccept} and {@link
     * java.lang.SecurityManager#checkConnect checkConnect} methods permit
     * datagrams to be received from and sent to, respectively, the given
     * remote address.
     *此方法执行与DatagramSocket#connect相同的安全检查。如果安全管理器配置,
     则此方将通过SecurityManager#checkAccept和#checkConnect检查是否允许从远端地址
     接受报文,或者发送报文到远端地址。
     * <p> This method may be invoked at any time.  It will not have any effect
     * on read or write operations that are already in progress at the moment
     * that it is invoked. If this channel's socket is not bound then this method
     * will first cause the socket to be bound to an address that is assigned
     * automatically, as if invoking the {@link #bind bind} method with a
     * parameter of {@code null}. 

     *此方可以在任何时候调用。在此方法调用的过程中,对读写IO操作没有任何影响。
     如果通道的socket没有绑定,此方将自动绑定一个地址,仿佛调用#bind(null)方法。
     * @param  remote
     *         The remote address to which this channel is to be connected
     *
     * @return  This datagram channel
     *
     * @throws  ClosedChannelException
     *          If this channel is closed
     *
     * @throws  AsynchronousCloseException
     *          If another thread closes this channel
     *          while the connect operation is in progress
     *
     * @throws  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
     *
     * @throws  SecurityException
     *          If a security manager has been installed
     *          and it does not permit access to the given remote address
     *
     * @throws  IOException
     *          If some other I/O error occurs
     */
    public abstract DatagramChannel connect(SocketAddress remote)
        throws IOException;

    /**
     * Disconnects this channel's socket.
     *断开通道的socket连接。
     *  The channel's socket is configured so that it can receive datagrams
     * from, and sends datagrams to, any remote address so long as the security
     * manager, if installed, permits it.
     *配置socket通道,以便可以从任何远端地址接受报文,或发送报文到任何远端地址,
     只要安全管理器,安装并允许报文通道接受连接和连接,即接受、发送报文权限。
     * <p> This method may be invoked at any time.  It will not have any effect
     * on read or write operations that are already in progress at the moment
     * that it is invoked.
     *此方法可以在任何时候调用。在此方法调用的过程中,对读写IO操作没有任何影响。
     * <p> If this channel's socket is not connected, or if the channel is
     * closed, then invoking this method has no effect.  

     *如果通道没有连接,或通道已经关闭,调用此方法没有任何影响
     * @return  This datagram channel
     *
     * @throws  IOException
     *          If some other I/O error occurs
     */
    public abstract DatagramChannel disconnect() throws IOException;

    /**
     * Returns the remote address to which this channel's socket is connected.
     *获取通道连接的远端地址,没有连接,则返回null
     * @return  The remote address; {@code null} if the channel's socket is not
     *          connected
     *
     * @throws  ClosedChannelException
     *          If the channel is closed
     * @throws  IOException
     *          If an I/O error occurs
     *
     * @since 1.7
     */
    public abstract SocketAddress getRemoteAddress() throws IOException;

    /**
     * Receives a datagram via this channel.
     *从通道接受一个报文
     *  If a datagram is immediately available, or if this channel is in
     * blocking mode and one eventually becomes available, then the datagram is
     * copied into the given byte buffer and its source address is returned.
     * If this channel is in non-blocking mode and a datagram is not
     * immediately available then this method immediately returns
     * <tt>null</tt>.
     *如果报文立即可用,或通道为阻塞模式,只要有报文空可用,则将拷贝报文到指定
     的buffer,并返回报文的源地址。如果通道为非阻塞模式,报文当前不可用,则
     此方立即返回null。
     * <p> The datagram is transferred into the given byte buffer starting at
     * its current position, as if by a regular {@link
     * ReadableByteChannel#read(java.nio.ByteBuffer) read} operation.  If there
     * are fewer bytes remaining in the buffer than are required to hold the
     * datagram then the remainder of the datagram is silently discarded.
     *如果报文的长度大于buffer的可用空间,则读取buffer的可用空间remaining个字节后,
     剩余的报文字节将会抛弃,就像执行ReadableByteChannel#read(java.nio.ByteBuffer) 操作。
     * <p> This method performs exactly the same security checks as the {@link
     * java.net.DatagramSocket#receive receive} method of the {@link
     * java.net.DatagramSocket} class.  That is, if the socket is not connected
     * to a specific remote address and a security manager has been installed
     * then for each datagram received this method verifies that the source's
     * address and port number are permitted by the security manager's {@link
     * java.lang.SecurityManager#checkAccept checkAccept} method.  The overhead
     * of this security check can be avoided by first connecting the socket via
     * the {@link #connect connect} method.
     *此方将会执行与DatagramSocket#receive一样的安全检查。如果DatagramSocket还没有连接,
     在接受报文时,执行安全检查,将会使用SecurityManager#checkAccept方法,
     检查是否允许接受源地址和端口连接请求。此安全检查可以通过#connect方法,连接socket避免。
     * <p> This method may be invoked at any time.  If another thread has
     * already initiated a read operation upon this channel, however, then an
     * invocation of this method will block until the first operation is
     * complete. If this channel's socket is not bound then this method will
     * first cause the socket to be bound to an address that is assigned
     * automatically, as if invoking the {@link #bind bind} method with a
     * parameter of {@code null}. 

     *此方可以在任何时候调用,如果在此方法调用之前,已经有线程正在执行读IO操作,
     则当前线程将阻塞到正在执行读IO操作的线程完毕。如果通道的socket没有绑定地址,
     此方将自动绑定一个地址,仿佛调用#bind(null)方法。
     * @param  dst
     *         The buffer into which the datagram is to be transferred
     *
     * @return  The datagram's source address,
     *          or <tt>null</tt> if this channel is in non-blocking mode
     *          and no datagram was immediately available
     *
     * @throws  ClosedChannelException
     *          If this channel is closed
     *
     * @throws  AsynchronousCloseException
     *          If another thread closes this channel
     *          while the read operation is in progress
     *
     * @throws  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
     *
     * @throws  SecurityException
     *          If a security manager has been installed
     *          and it does not permit datagrams to be accepted
     *          from the datagram's sender
     *
     * @throws  IOException
     *          If some other I/O error occurs
     */
    public abstract SocketAddress receive(ByteBuffer dst) throws IOException;

    /**
     * Sends a datagram via this channel.
     *通过通道发送一个报文
     *  If this channel is in non-blocking mode and there is sufficient room
     * in the underlying output buffer, or if this channel is in blocking mode
     * and sufficient room becomes available, then the remaining bytes in the
     * given buffer are transmitted as a single datagram to the given target
     * address.
     *如果通道为非阻塞模式,底层有足够的发送buffer空间,或通道为阻塞模式,有足够的空间
     可用,则buffer中数据将作为一个报文发送到远端地址。
     * <p> The datagram is transferred from the byte buffer as if by a regular
     * {@link WritableByteChannel#write(java.nio.ByteBuffer) write} operation.
     *转换buffer数据为报文,就像执行WritableByteChannel#write(java.nio.ByteBuffer)操作。
     * <p> This method performs exactly the same security checks as the {@link
     * java.net.DatagramSocket#send send} method of the {@link
     * java.net.DatagramSocket} class.  That is, if the socket is not connected
     * to a specific remote address and a security manager has been installed
     * then for each datagram sent this method verifies that the target address
     * and port number are permitted by the security manager's {@link
     * java.lang.SecurityManager#checkConnect checkConnect} method.  The
     * overhead of this security check can be avoided by first connecting the
     * socket via the {@link #connect connect} method.
     *方法中执行与DatagramSocket#send相同的安全检查。如果socket没有连接到远端地址,
     在每次报文发送时,将会通过SecurityManager#checkConnect检查socket到远端地址和端口
     的连接权限。此安全检查可以通过#connect方法,连接socket避免。
     * <p> This method may be invoked at any time.  If another thread has
     * already initiated a write operation upon this channel, however, then an
     * invocation of this method will block until the first operation is
     * complete. If this channel's socket is not bound then this method will
     * first cause the socket to be bound to an address that is assigned
     * automatically, as if by invoking the {@link #bind bind} method with a
     * parameter of {@code null}. 

     *此方法可以在任何时候调用。如果在此方法调用之前,已经有线程正在执行写IO操作,
     则当前线程将阻塞到正在执行写IO操作的线程完毕。如果通道的socket没有绑定地址,
     此方将自动绑定一个地址,仿佛调用#bind(null)方法。
     * @param  src
     *         The buffer containing the datagram to be sent
     *
     * @param  target
     *         The address to which the datagram is to be sent
     *
     * @return   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
     *此方将返回发送的报文字节数,为发送buffer的remaining空间数据,如果通道为
     非阻塞模式,底层没有足够的空间存放报文,有可能返回0
     * @throws  ClosedChannelException
     *          If this channel is closed
     *
     * @throws  AsynchronousCloseException
     *          If another thread closes this channel
     *          while the read operation is in progress
     *
     * @throws  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
     *
     * @throws  SecurityException
     *          If a security manager has been installed
     *          and it does not permit datagrams to be sent
     *          to the given address
     *
     * @throws  IOException
     *          If some other I/O error occurs
     */
    public abstract int send(ByteBuffer src, SocketAddress target)
        throws IOException;


    // -- ByteChannel operations --

    /**
     * Reads a datagram from this channel.
     *从通道读取一个报文
     *  This method may only be invoked if this channel's socket is
     * connected, and it only accepts datagrams from the socket's peer.  If
     * there are more bytes in the datagram than remain in the given buffer
     * then the remainder of the datagram is silently discarded.  Otherwise
     * this method behaves exactly as specified in the {@link
     * ReadableByteChannel} interface.  

     *此方法当且仅当通道socket建立连接后,方可调用,并且只接受从socket的peer发来的
     报文。如果报文的长度大于buffer的可用空间,多余的报文字节将会默认丢弃。读操作在其他方面
     与ReadableByteChannel的基本相同
     * @throws  NotYetConnectedException
     *          If this channel's socket is not connected
     */
    public abstract int read(ByteBuffer dst) throws IOException;

    /**
     * Reads a datagram from this channel.
     *从通道读取一个报文
     *  This method may only be invoked if this channel's socket is
     * connected, and it only accepts datagrams from the socket's peer.  If
     * there are more bytes in the datagram than remain in the given buffers
     * then the remainder of the datagram is silently discarded.  Otherwise
     * this method behaves exactly as specified in the {@link
     * ScatteringByteChannel} interface.  

     *方法当且仅当通道socket建立连接后,方可调用,并且只接受从socket的peer发来的
     报文。如果报文的长度大于buffer组的可用空间,多余的报文字节将会默认丢弃。读操作在其他方面
     与ScatteringByteChannel相同。
     * @throws  NotYetConnectedException
     *          If this channel's socket is not connected
     */
    public abstract long read(ByteBuffer[] dsts, int offset, int length)
        throws IOException;

    /**
     * Reads a datagram from this channel.
     *从通道读取一个报文
     *  This method may only be invoked if this channel's socket is
     * connected, and it only accepts datagrams from the socket's peer.  If
     * there are more bytes in the datagram than remain in the given buffers
     * then the remainder of the datagram is silently discarded.  Otherwise
     * this method behaves exactly as specified in the {@link
     * ScatteringByteChannel} interface.  

     *方法当且仅当通道socket建立连接后,方可调用,并且只接受从socket的peer发来的
     报文。如果报文的长度大于buffer组的可用空间,多余的报文字节将会默认丢弃。读操作在其他方面
     与ScatteringByteChannel相同。
     * @throws  NotYetConnectedException
     *          If this channel's socket is not connected
     */
    public final long read(ByteBuffer[] dsts) throws IOException {
        return read(dsts, 0, dsts.length);
    }

    /**
     * Writes a datagram to this channel.
     *写一个报文到通道。
     *  This method may only be invoked if this channel's socket is
     * connected, in which case it sends datagrams directly to the socket's
     * peer.  Otherwise it behaves exactly as specified in the {@link
     * WritableByteChannel} interface.  

     *方法当且仅当通道socket建立连接后,方可调用,并且只能直接发送报文到socket的peer。
     在其他方面与WritableByteChannel相同。
     * @throws  NotYetConnectedException
     *          If this channel's socket is not connected
     */
    public abstract int write(ByteBuffer src) throws IOException;

    /**
     * Writes a datagram to this channel.
     *写一个报文到通道。
     *  This method may only be invoked if this channel's socket is
     * connected, in which case it sends datagrams directly to the socket's
     * peer.  Otherwise it behaves exactly as specified in the {@link
     * GatheringByteChannel} interface.  

     *方法当且仅当通道socket建立连接后,方可调用,并且只能直接发送报文到socket的peer。
     在其他方面与GatheringByteChannel相同。
     * @return   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
     */
    public abstract long write(ByteBuffer[] srcs, int offset, int length)
        throws IOException;

    /**
     * Writes a datagram to this channel.
     *写一个报文到通道。
     *  This method may only be invoked if this channel's socket is
     * connected, in which case it sends datagrams directly to the socket's
     * peer.  Otherwise it behaves exactly as specified in the {@link
     * GatheringByteChannel} interface.  

     *方法当且仅当通道socket建立连接后,方可调用,并且只能直接发送报文到socket的peer。
     在其他方面与GatheringByteChannel相同。
     * @return   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
     *此方将返回发送的报文字节数,为发送buffer组的remaining空间数据之和,如果通道为
     非阻塞模式,底层没有足够的空间存放报文,有可能返回0
     * @throws  NotYetConnectedException
     *          If this channel's socket is not connected
     */
    public final long write(ByteBuffer[] srcs) throws IOException {
        return write(srcs, 0, srcs.length);
    }

}
0
1
分享到:
评论

相关推荐

    boot-example-netty-udp-2.0.5

    我们定义一个`UdpServerHandler`处理接收到的UDP数据包,并将其转换为业务逻辑可以理解的格式。 ```java public class UdpServerHandler extends ChannelInboundHandlerAdapter { @Override public void ...

    Java NIO系列教程

    - **DatagramChannel**:用于 UDP 数据包的发送和接收。 - **SocketChannel**:用于 TCP 数据的发送和接收。 - **ServerSocketChannel**:用于创建服务端的 TCP 连接。 #### Java NIO Buffers - **定义**:缓冲...

    Java NIO的介绍及工作原理

    - **类型**:Java NIO中提供了多种类型的Channel,包括`FileChannel`、`DatagramChannel`、`SocketChannel`和`ServerSocketChannel`等。 - **作用**:Channel是数据流动的路径,用于在Buffer和I/O设备之间传输数据...

    netty搭建TCP、UDP服务

    在具体实现过程中,首先需要定义ChannelInitializer,它会在新建立的Channel中添加所需的ChannelHandler。然后,根据TCP或UDP的需求,配置Bootstrap并绑定监听地址。对于TCP服务,还需要处理连接建立、数据接收和...

    Java中channel用法总结

    本文将深入讲解Java中`Channel`的定义、常见类型、Scatter/Gather操作、文件锁定、内存映射文件以及`DatagramChannel`的相关特性。 1. **Channel接口的定义**: `Channel`接口是所有通道类的基础,它提供了打开、...

    JavaNIONIO概述Java开发Java经验技巧共4页

    Java NIO定义了多个Buffer类,如ByteBuffer、CharBuffer、IntBuffer等,分别对应不同数据类型。 - 在进行I/O操作时,数据先会被读入到缓冲区,然后从缓冲区中读取或写入到通道。缓冲区提供了对数据的高效管理和控制...

    1392492818-videoLiveUdpServer-master (1)_视频聊天服务器代码_基于udp协议_

    2. **定义DatagramPacketHandler**:Netty中的DatagramChannel接收的是DatagramPacket对象,因此我们需要自定义一个处理类,重写其channelRead方法,处理接收到的DatagramPacket,例如解码视频数据、解析控制命令等...

    java笔试题大集合及答案(另附各大公司笔试题).rar

    - **通道**:FileChannel、SocketChannel、DatagramChannel的理解。 - **选择器**:Selector的选择与注册操作。 12. **网络编程**: - **Socket编程**:TCP和UDP通信的基本原理和实现。 - **HTTP协议**:了解...

    JAVA IO-NIO 详解

    - **分类**: 包括FileChannel、DatagramChannel、SocketChannel等。 - **特点**: 支持非阻塞模式,可以通过Selector监控多个Channel的状态。 **2. Buffer** - **概念**: 数据的容器,用于在Channel和程序之间进行...

    最新JDK教程(CHM版)

    - **类型**:包括`FileChannel`、`SocketChannel`、`DatagramChannel`等。 - **功能**:可以读取、写入、映射和锁住内存区域,以及提供非阻塞I/O操作的能力。 ##### 1.2 新I/O在文件操作中的应用 - **文件读写**...

    Java_NIO_API详解

    - **DatagramChannel**: 支持UDP数据报的发送和接收。 - **ServerSocketChannel**: 用于接受客户端连接的服务器端通道。 - **SocketChannel**: 用于与客户端通信的客户端通道。 #### 五、Selector 为了支持非阻塞I...

    Netty主要使用类讲解.docx

    4. **常用Channel类** - NioDatagramChannel 用于UDP通信,NioSctpChannel 用于SCTP(流控制传输协议)客户端,NioServerSocketChannel 用于TCP服务端,NioSocketChannel 用于TCP客户端。EmbeddedChannel 则用于单元...

    netty之UDP协议开发

    2. 创建`Bootstrap`实例并进行配置,包括指定`DatagramChannel`类型(用于UDP通信)和设置`ChannelInitializer`。 3. 设置数据报的发送和接收处理器,通常会有专门的`ChannelHandlerContext`来处理发送和接收事件。 ...

    java 网络编程 代码集合

    SMTP协议定义了电子邮件的传输规则,JavaMail API则提供了与SMTP服务器交互的接口,用于创建、发送和接收电子邮件。 4. **Producer-Consumer**:02-Producer-Consumer示例涉及并发编程中的生产者-消费者问题。这个...

    spring-netty.zip

    在Netty中实现UDP通信,需要创建一个`DatagramChannel`,并通过`DatagramPacket`发送和接收数据。 **Spring Bean与Netty Handler** 在Netty服务器中,我们通常会创建自定义的`ChannelInboundHandler`或`...

    Netty简介 Netty线程模型和EventLoop Codec编码与解码 ByteBuf容器

    Netty提供了一系列预定义的编解码器,如LineBasedFrameDecoder用于按行解析数据,LengthFieldBasedFrameDecoder用于解析带有长度字段的数据帧。用户也可以自定义编解码器,实现特定协议的解析。编码器将应用层对象...

    java nio入门学习,两个pdf

    Java NIO提供了多种类型的通道,如FileChannel、SocketChannel、DatagramChannel等,分别对应于文件、套接字和UDP通信。 2. **缓冲区(Buffers)**:缓冲区是存储数据的主要对象,所有的I/O操作都是通过缓冲区进行的...

Global site tag (gtag.js) - Google Analytics