Java in a Nutshell, 5th Edition [Electronic resources]

نسخه متنی -صفحه : 1191/ 433
نمايش فراداده

DatagramChanneljava.nio.channels

Java 1.4closeable

This class implements a communication channel based on network datagrams. Obtain a DatagramChannel by calling the static open( ) method. Call socket( ) to obtain the java.net.DatagramSocket object on which the channel is based if you need to set any socket options to control low-level networking details.

The send( ) method sends the remaining bytes of the specified ByteBuffer to the host and port specified in the java.net.SocketAddress in the form of a datagram. receive( ) does the opposite: it receives a datagram, stores its content into the specified buffer (discarding any bytes that do not fit) and then returns a SocketAddress that specifies the sender of the datagram (or returns null if the channel was in nonblocking mode and no datagram was waiting).

The send( ) and receive( ) methods typically perform security checks on each invocation to see if the application has permissions to communicate with the remote host. If your application will use a DatagramChannel to exchange datagrams with a single remote host and port, use the connect( ) method to connect to a specified SocketAddress. The connect( ) method performs the required security checks once and allows future communication with the specified address without the overhead. Once a DatagramChannel is connected, you can use the standard read( ) and write( ) methods defined by the ReadableByteChannel, WritableByteChannel, GatheringByteChannel and ScatteringByteChannel interfaces. Like the receive( ) method, the read( ) methods silently discard any received bytes that do not fit in the specified ByteBuffer. The read( ) and write( ) methods throw a NotYetConnected exception if connect( ) has not been called.

DatagramChannel is a SelectableChannel; its validOps( ) method specifies that read and write operations may be selected. DatagramChannel objects are thread-safe. Read and write operations may proceed concurrently, but the class ensures that only one thread may read and one thread write at a time.

Figure 13-2. java.nio.channels.DatagramChannel

public abstract class

DatagramChannel extends java.nio.channels.spi. AbstractSelectableChannel implements ByteChannel, GatheringByteChannel, ScatteringByteChannel { // Protected Constructors protected

DatagramChannel (java.nio.channels.spi.SelectorProvider

provider ); // Public Class Methods public static DatagramChannel

open ( ) throws java.io.IOException; // Public Instance Methods public abstract DatagramChannel

connect (java.net.SocketAddress

remote ) throws java.io.IOException; public abstract DatagramChannel

disconnect ( ) throws java.io.IOException; public abstract boolean

isConnected ( ); public abstract java.net.SocketAddress

receive (java.nio.ByteBuffer

dst ) throws java.io.IOException; public abstract int

send (java.nio.ByteBuffer

src , java.net.SocketAddress

target ) throws java.io.IOException; public abstract java.net.DatagramSocket

socket ( ); // Methods Implementing GatheringByteChannel public final long

write (java.nio.ByteBuffer[ ]

srcs ) throws java.io.IOException; public abstract long

write (java.nio.ByteBuffer[ ]

srcs , int

offset , int

length ) throws java.io.IOException; // Methods Implementing ReadableByteChannel public abstract int

read (java.nio.ByteBuffer

dst ) throws java.io.IOException; // Methods Implementing ScatteringByteChannel public final long

read (java.nio.ByteBuffer[ ]

dsts ) throws java.io.IOException; public abstract long

read (java.nio.ByteBuffer[ ]

dsts , int

offset , int

length ) throws java.io.IOException; // Methods Implementing WritableByteChannel public abstract int

write (java.nio.ByteBuffer

src ) throws java.io.IOException; // Public Methods Overriding SelectableChannel public final int

validOps ( ); constant }

Returned By

java.net.DatagramSocket.getChannel( ), java.nio.channels.spi.SelectorProvider.openDatagramChannel( )