Java的DatagramChannel.write()的含义
The meaning of Java's DatagramChannel.write()
我最近找到了使用 DatagramChannel.write()
的代码。该代码在连接之前只询问主机 ip 和端口,然后允许发送(通过 DatagramChannel.write()
)。
作为网络编程的新手,它不请求目的地似乎很奇怪 IP/port。为什么不呢?
Oracle 的文档说明
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.
"the socket's peer"是什么意思?
DatagramChannel 是 java.nio 包的一部分。它将一个参数作为 ByteBuffer 发送到连接的通道。
在尝试发送之前,您应该使用 InetSocketAddress 进行连接,它将主机 ip 和端口作为参数。
connect(SocketAddress remote) --> 将通道连接到底层套接字。您可以从 Datagramchannel 获取套接字,因为通道是用于 read/write 目的的非阻塞 io。 socket peer 只是远程机器。
Being new to network programming, it seemed odd that it wouldn't request the destination IP/port. Why not?
因为前面已经指定了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"?
您将频道连接到的目标 IP:port。
我最近找到了使用 DatagramChannel.write()
的代码。该代码在连接之前只询问主机 ip 和端口,然后允许发送(通过 DatagramChannel.write()
)。
作为网络编程的新手,它不请求目的地似乎很奇怪 IP/port。为什么不呢?
Oracle 的文档说明
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.
"the socket's peer"是什么意思?
DatagramChannel 是 java.nio 包的一部分。它将一个参数作为 ByteBuffer 发送到连接的通道。 在尝试发送之前,您应该使用 InetSocketAddress 进行连接,它将主机 ip 和端口作为参数。 connect(SocketAddress remote) --> 将通道连接到底层套接字。您可以从 Datagramchannel 获取套接字,因为通道是用于 read/write 目的的非阻塞 io。 socket peer 只是远程机器。
Being new to network programming, it seemed odd that it wouldn't request the destination IP/port. Why not?
因为前面已经指定了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"?
您将频道连接到的目标 IP:port。