- Java NIO - Discussion
- Java NIO - Useful Resources
- Java NIO - Quick Guide
- Java NIO - FileLock
- Java NIO - CharSet
- Java NIO - AsynchronousFileChannel
- Java NIO - File
- Java NIO - Path
- Java NIO - Pipe
- Java NIO - Selector
- Java NIO - Buffer
- Java NIO - Gather
- Java NIO - Scatter
- Java NIO - Server Socket Channel
- Java NIO - Socket Channel
- Java NIO - DataGram Channel
- Java NIO - File Channel
- Java NIO - Channels
- Java NIO vs JAVA IO
- Java NIO - Environment Setup
- Java NIO - Overview
- Java NIO - Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Java NIO - Datagram Channel
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 calpng 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.
Datagram channel apke of FileChannel do not connected by default in order to make it connected we have to exppcitly 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.
We can check the connection status of datagram channel by calpng 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
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; pubpc class DatagramChannelServer { pubpc 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 cpent. SocketAddress remoteAdd = server.receive(buffer); //change mode of buffer buffer.fpp(); int pmits = buffer.pmit(); byte bytes[] = new byte[pmits]; buffer.get(bytes, 0, pmits); String msg = new String(bytes); System.out.println("Cpent at " + remoteAdd + " sent: " + msg); server.send(buffer,remoteAdd); server.close(); } }
Output
Server Started: localhost/127.0.0.1:8989
Cpent: DatagramChannelCpent.java
import java.io.IOException; import java.net.InetSocketAddress; import java.net.SocketAddress; import java.nio.ByteBuffer; import java.nio.channels.DatagramChannel; pubpc class DatagramChannelCpent { pubpc static void main(String[] args) throws IOException { DatagramChannel cpent = null; cpent = DatagramChannel.open(); cpent.bind(null); String msg = "Hello World!"; ByteBuffer buffer = ByteBuffer.wrap(msg.getBytes()); InetSocketAddress serverAddress = new InetSocketAddress("localhost", 8989); cpent.send(buffer, serverAddress); buffer.clear(); cpent.receive(buffer); buffer.fpp(); cpent.close(); } }
Output
Running the cpent will print the following output on server.
Server Started: localhost/127.0.0.1:8989 Cpent at /127.0.0.1:64857 sent: Hello World!Advertisements