The meaning of Java's DatagramChannel.write(


I recently found code using DatagramChannel.write(). The code only asked for host ip and port before it connected and then would allow sending (via DatagramChannel.write()).

Being new to network programming, it seemed odd that it wouldn't request the destination IP/port. Why not?

Oracle's documentation states

This method may only be invoked if this channel's socket is connected, in which case it sends datagrams directly to the socket's peer.

What does it mean by "the socket's peer"?


DatagramChannel is part of java.nio package. It will take a parameter as ByteBuffer to send to the connected channel. Before trying to send you should connect using InetSocketAddress, which takes the host ip and port as parameter. connect(SocketAddress remote) --> connects the channel to the underlying socket. you can get the socket from Datagramchannel because channel is non blocking io used for read/write purpose. socket peer's is nothing but the remote machine.

By : Shriram

This video can help you solving your question :)
By: admin