Reputation: 699
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"?
Upvotes: 2
Views: 452
Reputation: 311054
Being new to network programming, it seemed odd that it wouldn't request the destination IP/port. Why not?
Because it has already been specified in the prior connect().
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"?
The target IP:port to which you connected the channel.
Upvotes: 2
Reputation: 4421
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.
Upvotes: 1