Java NIO Series Tutorials (10) Java NiO Datagramchannel

Source: Internet
Author: User

The Datagramchannel in Java NiO is a channel that can send and receive UDP packets. Because UDP is a non-connected network protocol, it cannot be read and written like other channels. It sends and receives a packet.

Open Datagramchannel

Here's how Datagramchannel is opened:

1 DatagramChannel channel = DatagramChannel.open();
2 channel.socket().bind(newInetSocketAddress(9999));

This example opens the Datagramchannel that can receive packets on UDP port 9999.

Receive data

Receive data from Datagramchannel through the receive () method, such as:

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

The receive () method copies the received packet contents to the specified buffer. If buffer does not tolerate the data received, the extra data will be discarded.

Send data

Send data from Datagramchannel using the Send () method, such as:

1 String newData = "New String to write to file..."+ System.currentTimeMillis();
2
3 ByteBuffer buf = ByteBuffer.allocate(48);
4 buf.clear();
5 buf.put(newData.getBytes());
6 buf.flip();
7
8 intbytesSent = channel.send(buf, newInetSocketAddress("jenkov.com"80));

This example sends a string of characters to UDP port 80 of the "jenkov.com" server. Because the server does not monitor this port, nothing happens. You will not be notified if the packets you sent have been received, because UDP has no guarantee of data transfer.

Connect to a specific address

Datagramchannel can be "connected" to a specific address in the network. Because UDP is not connected, connecting to a specific address does not create a true connection like a TCP channel. Instead, lock the datagramchannel so that it can only send and receive data from a specific address.

Here's an example:

1 channel.connect(newInetSocketAddress("jenkov.com"80));

When connected, you can also use the read () and write () methods, just as you would with a traditional channel. There is no guarantee of data transfer. Here are a few examples:

View Source code Printing Help
1 intbytesRead = channel.read(buf);
2 intbytesWritten = channel.write(but);

Java NIO Series Tutorials (10) Java NiO Datagramchannel

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.