天天看点

Java NIO DatagramChannelJava NIO DatagramChannel

Java Nio 

1

<a target="_blank" href="http://tutorials.jenkov.com/java-nio/index.html">Java NIO Tutorial</a>

2

<a target="_blank" href="http://tutorials.jenkov.com/java-nio/overview.html">Java NIO Overview</a>

3

<a target="_blank" href="http://tutorials.jenkov.com/java-nio/channels.html">Java NIO Channel</a>

4

<a target="_blank" href="http://tutorials.jenkov.com/java-nio/buffers.html">Java NIO Buffer</a>

5

<a target="_blank" href="http://tutorials.jenkov.com/java-nio/scatter-gather.html">Java NIO Scatter / Gather</a>

6

<a target="_blank" href="http://tutorials.jenkov.com/java-nio/channel-to-channel-transfers.html">Java NIO Channel to Channel Transfers</a>

7

<a target="_blank" href="http://tutorials.jenkov.com/java-nio/selectors.html">Java NIO Selector</a>

8

<a target="_blank" href="http://tutorials.jenkov.com/java-nio/file-channel.html">Java NIO FileChannel</a>

9

<a target="_blank" href="http://tutorials.jenkov.com/java-nio/socketchannel.html">Java NIO SocketChannel</a>

10

<a target="_blank" href="http://tutorials.jenkov.com/java-nio/server-socket-channel.html">Java NIO ServerSocketChannel</a>

11

<a target="_blank" href="http://tutorials.jenkov.com/java-nio/datagram-channel.html">Java NIO DatagramChannel</a>

12

<a target="_blank" href="http://tutorials.jenkov.com/java-nio/pipe.html">Java NIO Pipe</a>

13

<a target="_blank" href="http://tutorials.jenkov.com/java-nio/nio-vs-io.html">Java NIO vs. IO</a>

Java NIO DatagramChannelJava NIO DatagramChannel

Rate article:

Share article:

<a target="_blank" href="https://twitter.com/share">Tweet</a>

Table of Contents

<a target="_blank" href="http://tutorials.jenkov.com/java-nio/datagram-channel.html#opening-a-datagramchannel">Opening a DatagramChannel</a>

<a target="_blank" href="http://tutorials.jenkov.com/java-nio/datagram-channel.html#receiving-data">Receiving Data</a>

<a target="_blank" href="http://tutorials.jenkov.com/java-nio/datagram-channel.html#sending-data">Sending Data</a>

<a target="_blank" href="http://tutorials.jenkov.com/java-nio/datagram-channel.html#connecting-to-a-specific-address">Connecting to a Specific Address</a>

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 <code>DatagramChannel</code> like you do from other channels. Instead you send and receive packets of data.

<a target="_blank"></a>

Here is how you open a <code>DatagramChannel</code>:

This example opens a <code>DatagramChannel</code> which can receive packets on UDP port 9999.

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

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

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

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.

It is possible to "connect" a <code>DatagramChannel</code> 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<code>DatagramChannel</code> so you can only send and receive data packets from one specific address.

Here is an example:

When connected you can also use the <code>read()</code> and <code>write()</code> 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:

<a target="_blank" href="http://tutorials.jenkov.com/java-nio/pipe.html">Next:   Java NIO Pipe</a>