java.nio.channels

Class DatagramChannel

    • 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.

        The ProtocolFamily of the channel's socket is platform (and possibly configuration) dependent and therefore unspecified. The 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.

        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
      • socket
        public abstract DatagramSocket socket()
        Retrieves a datagram socket associated with this channel.

        The returned object will not declare any public methods that are not declared in the DatagramSocket class.

        Returns:
        A datagram socket associated with this channel
      • isConnected
        public abstract boolean isConnected()
        Tells whether or not this channel's socket is connected.
        Returns:
        true if, and only if, this channel's socket is open and connected
      • connect
        public abstract DatagramChannel connect(SocketAddress remote)
                                         throws IOException
        Connects this channel's socket.

        The channel's socket is configured so that it only receives datagrams from, and sends datagrams to, the given remote peer 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.

        This method performs exactly the same security checks as the connect method of the DatagramSocket class. That is, if a security manager has been installed then this method verifies that its checkAccept and checkConnect methods permit datagrams to be received from and sent to, respectively, the given remote address.

        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 bind method with a parameter of null.

        Parameters:
        remote - The remote address to which this channel is to be connected
        Returns:
        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
        public abstract DatagramChannel disconnect()
                                            throws IOException
        Disconnects this channel's 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.

        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 connected, or if the channel is closed, then invoking this method has no effect.

        Returns:
        This datagram channel
        Throws:
        IOException - If some other I/O error occurs
      • 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
      • receive
        public abstract SocketAddress receive(ByteBuffer dst)
                                       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 null.

        The datagram is transferred into the given byte buffer starting at its current position, as if by a regular 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.

        This method performs exactly the same security checks as the receive method of the 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 checkAccept method. The overhead of this security check can be avoided by first connecting the socket via the connect method.

        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 bind method with a parameter of null.

        Parameters:
        dst - The buffer into which the datagram is to be transferred
        Returns:
        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
        public abstract int send(ByteBuffer src,
               SocketAddress target)
                          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.

        The datagram is transferred from the byte buffer as if by a regular write operation.

        This method performs exactly the same security checks as the send method of the 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 checkConnect method. The overhead of this security check can be avoided by first connecting the socket via the connect method.

        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 bind method with a parameter of null.

        Parameters:
        src - The buffer containing the datagram to be sent
        target - The address to which the datagram is to be sent
        Returns:
        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
      • read
        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 buffer then the remainder of the datagram is silently discarded. Otherwise this method behaves exactly as specified in the ReadableByteChannel interface.

        Specified by:
        read in interface ReadableByteChannel
        Parameters:
        dst - The buffer into which bytes are to be transferred
        Returns:
        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
        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
        IOException - If some other I/O error occurs
      • read
        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 ScatteringByteChannel interface.

        Specified by:
        read in interface ScatteringByteChannel
        Parameters:
        dsts - The buffers into which bytes are to be transferred
        offset - 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 - The maximum number of buffers to be accessed; must be non-negative and no larger than dsts.length - offset
        Returns:
        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
        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
        IOException - If some other I/O error occurs
      • read
        public final long read(ByteBuffer[] dsts)
                        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 ScatteringByteChannel interface.

        Specified by:
        read in interface ScatteringByteChannel
        Parameters:
        dsts - The buffers into which bytes are to be transferred
        Returns:
        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
        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
        IOException - If some other I/O error occurs
      • write
        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 WritableByteChannel interface.

        Specified by:
        write in interface WritableByteChannel
        Parameters:
        src - The buffer from which bytes are to be retrieved
        Returns:
        The number of bytes written, possibly zero
        Throws:
        NotYetConnectedException - If this channel's socket is not connected
        ClosedChannelException - If this channel is closed
        AsynchronousCloseException - If another thread closes this channel while the write operation is in progress
        ClosedByInterruptException - If another thread interrupts the current thread while the write operation is in progress, thereby closing the channel and setting the current thread's interrupt status
        IOException - If some other I/O error occurs
      • write
        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 GatheringByteChannel interface.

        Specified by:
        write in interface GatheringByteChannel
        Parameters:
        srcs - The buffers from which bytes are to be retrieved
        offset - 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 - The maximum number of buffers to be accessed; must be non-negative and no larger than srcs.length - offset
        Returns:
        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
        ClosedChannelException - If this channel is closed
        AsynchronousCloseException - If another thread closes this channel while the write operation is in progress
        ClosedByInterruptException - If another thread interrupts the current thread while the write operation is in progress, thereby closing the channel and setting the current thread's interrupt status
        IOException - If some other I/O error occurs
      • write
        public final long write(ByteBuffer[] srcs)
                         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 GatheringByteChannel interface.

        Specified by:
        write in interface GatheringByteChannel
        Parameters:
        srcs - The buffers from which bytes are to be retrieved
        Returns:
        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
        ClosedChannelException - If this channel is closed
        AsynchronousCloseException - If another thread closes this channel while the write operation is in progress
        ClosedByInterruptException - If another thread interrupts the current thread while the write operation is in progress, thereby closing the channel and setting the current thread's interrupt status
        IOException - If some other I/O error occurs

Document created the 11/06/2005, last modified the 04/03/2020
Source of the printed document:https://www.gaudry.be/en/java-api-rf-java/nio/channels/DatagramChannel.html

The infobrol is a personal site whose content is my sole responsibility. The text is available under CreativeCommons license (BY-NC-SA). More info on the terms of use and the author.

References

  1. View the html document Language of the document:fr Manuel PHP : https://docs.oracle.com

These references and links indicate documents consulted during the writing of this page, or which may provide additional information, but the authors of these sources can not be held responsible for the content of this page.
The author This site is solely responsible for the way in which the various concepts, and the freedoms that are taken with the reference works, are presented here. Remember that you must cross multiple source information to reduce the risk of errors.

Contents Haut