Java Nio 简明教程
Java NIO - Datagram Channel
Java NIO Datagram 用作在无连接协议上发送和接收 UDP 数据包的通道。默认情况下,数据报通道在阻塞,虽然它可以在非阻塞模式下使用。为了使它非阻塞,我们可以使用 configureBlocking(false) 方法。数据报通道可以通过调用其名为 open() 的静态方法之一来打开,该方法还可以使用 IP 地址作为参数,以便可以用于多播。
Java NIO Datagram is used as channel which can send and receive UDP packets over a connection less protocol.By default datagram channel is blocking while it can be use in non blocking mode.In order to make it non-blocking we can use the configureBlocking(false) method.DataGram channel can be open by calling its one of the static method named as open() which can also take IP address as parameter so that it can be used for multi casting.
数据报通道类似于 FileChannel,默认情况下不进行连接,因此为了实现连接,我们必须显式调用其 connect() 方法。然而,数据报通道不需要连接才能使用 send 和 receive 方法,而它必须连接才能使用 read 和 write 方法,因为那些方法不接受或不返回套接字地址。
Datagram channel alike of FileChannel do not connected by default in order to make it connected we have to explicitly call its connect() method.However datagram channel need not be connected in order for the send and receive methods to be used while it must be connected in order to use the read and write methods, since those methods do not accept or return socket addresses.
我们可以通过调用其 isConnected() 方法来检查数据报通道的连接状态。一旦连接,数据报通道将一直保持连接状态,直到它断开或关闭。数据报通道是线程安全的,并且同时支持多线程和并发。
We can check the connection status of datagram channel by calling its isConnected() method.Once connected, a datagram channel remains connected until it is disconnected or closed.Datagram channels are thread safe and supports multi-threading and concurrency simultaneously.
Important methods of datagram channel
-
bind(SocketAddress local) − This method is used to bind the datagram channel’s socket to the local address which is provided as the parameter to this method.
-
connect(SocketAddress remote) − This method is used to connect the socket to the remote address.
-
disconnect() − This method is used to disconnect the socket to the remote address.
-
getRemoteAddress() − This method return the address of remote location to which the channel’s socket is connected.
-
isConnected() − As already mentioned this method returns the status of connection of datagram channel i.e whether it is connected or not.
-
open() and open(ProtocolFamily family) − Open method is used open a datagram channel for single address while parametrized open method open channel for multiple addresses represented as protocol family.
-
read(ByteBuffer dst) − This method is used to read data from the given buffer through datagram channel.
-
receive(ByteBuffer dst) − This method is used to receive datagram via this channel.
-
send(ByteBuffer src, SocketAddress target) − This method is used to send datagram via this channel.
Example
以下示例显示如何通过 Java NIO DataGramChannel 发送数据。
The following example shows the how to send data from Java NIO DataGramChannel.
Server: DatagramChannelServer.java
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.DatagramChannel;
public class DatagramChannelServer {
public static void main(String[] args) throws IOException {
DatagramChannel server = DatagramChannel.open();
InetSocketAddress iAdd = new InetSocketAddress("localhost", 8989);
server.bind(iAdd);
System.out.println("Server Started: " + iAdd);
ByteBuffer buffer = ByteBuffer.allocate(1024);
//receive buffer from client.
SocketAddress remoteAdd = server.receive(buffer);
//change mode of buffer
buffer.flip();
int limits = buffer.limit();
byte bytes[] = new byte[limits];
buffer.get(bytes, 0, limits);
String msg = new String(bytes);
System.out.println("Client at " + remoteAdd + " sent: " + msg);
server.send(buffer,remoteAdd);
server.close();
}
}
Client: DatagramChannelClient.java
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.DatagramChannel;
public class DatagramChannelClient {
public static void main(String[] args) throws IOException {
DatagramChannel client = null;
client = DatagramChannel.open();
client.bind(null);
String msg = "Hello World!";
ByteBuffer buffer = ByteBuffer.wrap(msg.getBytes());
InetSocketAddress serverAddress = new InetSocketAddress("localhost",
8989);
client.send(buffer, serverAddress);
buffer.clear();
client.receive(buffer);
buffer.flip();
client.close();
}
}