博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java NIO DatagramChannel
阅读量:7255 次
发布时间:2019-06-29

本文共 2250 字,大约阅读时间需要 7 分钟。

A Java NIO DatagramChannel is a channel that can send and receive UDP packets. Since UDP is a connection-less network protocol, you cannot just by default read and write to a DatagramChannel like you do from other channels. Instead you send and receive packets of data.

Opening a DatagramChannel

Here is how you open a DatagramChannel:

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

This example opens a DatagramChannel which can receive packets on UDP port 9999.

Receiving Data

You receive data from a DatagramChannel by calling its receive() method, like this:

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

The receive() method will copy the content of a received packet of data into the given Buffer. If the received packet contains more data than the Buffer can contain, the remaining data is discarded silently.

Sending Data

You can send data via a DatagramChannel by calling its send() method, like this:

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));

This example sends the string to the "jenkov.com" server on UDP port 80. Nothing is listening on that port though, so nothing will happen. You will not be notified of whether the send packet was received or not, since UDP does not make any guarantees about delivery of data.

Connecting to a Specific Address

It is possible to "connect" a DatagramChannel to a specific address on the network. Since UDP is connection-less, this way of connecting to an address does not create a real connection, like with a TCP channel. Rather, it locks your DatagramChannel so you can only send and receive data packets from one specific address.

Here is an example:

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

When connected you can also use the read() and write() method, as if you were using a traditional channel. You just don't have any guarantees about delivery of the sent data. Here are a few examples:

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

 Ref:

转载地址:http://orzdm.baihongyu.com/

你可能感兴趣的文章
PHP实现执行定时任务的几种思路详解
查看>>
几种机器学习框架的对比和选择
查看>>
graphql-yoga interface && union 使用
查看>>
32.QT-制作最强电压电阻表盘,可以自定义阴影效果,渐变颜色,图标,文字标签等-附带demo程序...
查看>>
jquery tmpl 详解
查看>>
Linux iptables 命令
查看>>
bootstrap课程9 bootstrap如何实现动画加载进度条的效果
查看>>
Laravel 5.3 用户验证源码探究 (一) 路由与注册
查看>>
程序员考证之信息系统项目管理师
查看>>
scikit-learn学习笔记
查看>>
mybatis 传入多个参数
查看>>
opencv给图片添加文字水印<转>
查看>>
mysql查询表的数据大小
查看>>
初识Continuation
查看>>
smooth l1
查看>>
ET–异步协程使用–TimerComponent篇
查看>>
Linux LVM学习总结——Insufficient Free Extents for a Logical Volume
查看>>
智课雅思词汇---二十一、名词性后缀acity是什么意思
查看>>
JavaWeb 返回json数据的两种方式
查看>>
(转)Java 详解 JVM 工作原理和流程
查看>>