Java NIO中的DatagramChannel是一个能收发UDP包的通道。因为UDP是无连接的网络协议,所以不能像其它通道那样读取和写入。它发送和接收的是数据包。

打开 DatagramChannel

下面是 DatagramChannel 的打开方式:

DatagramChannel channel = DatagramChannel.open();
channel.socket().bind(new InetSocketAddress(9999));

这个例子打开的 DatagramChannel可以在UDP端口9999上接收数据包。

接收数据

通过receive()方法从DatagramChannel接收数据,如:

ByteBuffer buf = ByteBuffer.allocate(48);
buf.clear();
channel.receive(buf);

receive()方法会将接收到的数据包内容复制到指定的Buffer. 如果Buffer容不下收到的数据,多出的数据将被丢弃。

发送数据

通过send()方法从DatagramChannel发送数据,如:

String newData = "New String to write to file..." + System.currentTimeMillis();
ByteBuffer buf = ByteBuffer.allocate(48);
buf.clear();
buf.put(newData.getBytes());
buf.flip();
int bytesSent = channel.send(buf, new InetSocketAddress("jenkov.com", 80));

这个例子发送一串字符到”jenkov.com”服务器的UDP端口80。 因为服务端并没有监控这个端口,所以什么也不会发生。也不会通知你发出的数据包是否已收到,因为UDP在数据传送方面没有任何保证。

连接到特定的地址

可以将DatagramChannel“连接”到网络中的特定地址的。由于UDP是无连接的,连接到特定地址并不会像TCP通道那样创建一个真正的连接。而是锁住DatagramChannel ,让其只能从特定地址收发数据。

这里有个例子:

channel.connect(new InetSocketAddress("jenkov.com", 80));

当连接后,也可以使用read()和write()方法,就像在用传统的通道一样。只是在数据传送方面没有任何保证。这里有几个例子:

int bytesRead = channel.read(buf);
int bytesWritten = channel.write(but);

完整实例

服务端: 接收客户端发送消息,收取到消息后,给发送方一个回应

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.DatagramChannel; /**
* 服务端: 接收客户端发送消息,收取到消息后,给发送方一个回应
*/
public class TestDataDramdChannelReceive {
public static void main(String[] args) throws IOException {
// 获取通道
DatagramChannel datagramChannel = DatagramChannel.open();
// 绑定端口
datagramChannel.bind(new InetSocketAddress(8989));
// 分配Buffer
ByteBuffer buffer = ByteBuffer.allocate(1024);
byte b[];
while (true){
// 清空Buffer
buffer.clear();
// 接受客户端发送数据
SocketAddress socketAddress = datagramChannel.receive(buffer);
if (socketAddress != null) {
int position = buffer.position();
b = new byte[position];
buffer.flip();
for(int i=0; i<position; ++i) {
b[i] = buffer.get();
}
System.out.println("receive remote " + socketAddress.toString() + ":" + new String(b, "UTF-8"));
//接收到消息后给发送方回应
sendReback(socketAddress,datagramChannel);
}
}
}
public static void sendReback(SocketAddress socketAddress, DatagramChannel datagramChannel) throws IOException {
String message = "I has receive your message";
ByteBuffer buffer = ByteBuffer.allocate(1024);
buffer.put(message.getBytes("UTF-8"));
buffer.flip();
datagramChannel.send(buffer, socketAddress);
}
}

客户端: 发送控制台输入的内容并接收服务端回应

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.DatagramChannel;
import java.util.Scanner; /**
* 客户端: 发送控制台输入的内容并接收服务端回应
*/
public class TestDataGramdChannelSend {
public static void main(String[] args) throws IOException {
final DatagramChannel channel = DatagramChannel.open();
//接收消息线程
new Thread(new Runnable() {
@Override
public void run() {
ByteBuffer buffer = ByteBuffer.allocate(1024);
byte b[];
while(true) {
buffer.clear();
SocketAddress socketAddress = null;
try {
socketAddress = channel.receive(buffer);
} catch (IOException e) {
e.printStackTrace();
}
if (socketAddress != null) {
int position = buffer.position();
b = new byte[position];
buffer.flip();
for(int i=0; i<position; ++i) {
b[i] = buffer.get();
}
try {
System.out.println("receive remote " + socketAddress.toString() + ":" + new String(b, "UTF-8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
}
}
}).start(); //发送控制台输入消息
while (true) {
Scanner sc = new Scanner(System.in);
String next = sc.next();
try {
sendMessage(channel, next);
} catch (IOException e) {
e.printStackTrace();
}
}
} public static void sendMessage(DatagramChannel channel, String mes) throws IOException {
if (mes == null || mes.isEmpty()) {
return;
}
ByteBuffer buffer = ByteBuffer.allocate(1024);
buffer.clear();
buffer.put(mes.getBytes("UTF-8"));
buffer.flip();
System.out.println("send msg:" + mes);
int send = channel.send(buffer, new InetSocketAddress("localhost",8989));
}
}

运行服务端程序,然后再运行客户端程序,在客户端控制台输入数据,按回车键发送,在服务端控制台可以收到发送的数据。

源码地址:https://github.com/qjm201000/io_nio_datagramchannel.git

转自:http://ifeve.com/datagram-channel/

最新文章

  1. PHP数组的基础知识
  2. 订制DOM选择器
  3. python与C++交互
  4. C++程序设计(一)
  5. 一步一步实现MVC5+EF6+Bootstarp+Autofac+NoSql实现OADemo 之登陆(一) 验证码 Captcha 之大插件小用
  6. python-判断系统平台
  7. hdu-4753-Fishhead’s Little Game-记忆化搜索
  8. 使用java发送邮件sp自动发送邮件方法
  9. 关键字 virtual
  10. leetcode 3Sum Closest python
  11. R语言-离职率分析
  12. Eclipse 安装 AmaterasUML 插件
  13. [Android] Android 用于异步加载 ContentProvider 中的内容的机制 -- Loader 机制 (LoaderManager + CursorLoader + LoaderManager.LoaderCallbacks)
  14. 219. 存在重复元素 II
  15. jquery选择器筛选器
  16. HDU 4725 The Shortest Path in Nya Graph (最短路)
  17. Linux 增量系统备份和部分还原策略
  18. 15年-ICPC长春-网络赛
  19. C语言数据结构之哈夫曼树及哈夫曼编码的实现
  20. Hadoop权威指南文摘

热门文章

  1. .net reactor 学习系列(二)---.net reactor界面各功能说明
  2. cocos2d-x 源代码分析 : Ref (CCObject) 源代码分析 cocos2d-x内存管理策略
  3. ng-alain 复用标签相关设置
  4. 自动备份Mysql数据库脚本
  5. .NET Core 中使用 Humanizer 显示友好时间格式
  6. 高启全:长江存储自主3D NAND,DRAM研发欢迎美光一起加入(千秋大业,慢慢做)
  7. HTTP通信过程原理
  8. QT中的各种对话框
  9. Qt 5.6.2 静态编译(VS2013 x86 target xp openssl icu webkit)
  10. visual studio添加docker支持简记