简体   繁体   English

Java的DatagramChannel.write()的含义

[英]The meaning of Java's DatagramChannel.write()

I recently found code using DatagramChannel.write() . 我最近使用DatagramChannel.write()找到了代码。 The code only asked for host ip and port before it connected and then would allow sending (via DatagramChannel.write() ). 该代码仅在连接之前询问主机ip和端口,然后才允许发送(通过DatagramChannel.write() )。

Being new to network programming, it seemed odd that it wouldn't request the destination IP/port. 对于网络编程来说,它是新手,它不请求目标IP /端口似乎很奇怪。 Why not? 为什么不?

Oracle's documentation states 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. 仅在连接此通道的套接字的情况下才可以调用此方法,在这种情况下,它将直接向套接字的对等方发送数据报。

What does it mean by "the socket's peer"? “套接字的对等方”是什么意思?

Being new to network programming, it seemed odd that it wouldn't request the destination IP/port. 对于网络编程来说,它是新手,它不请求目标IP /端口似乎很奇怪。 Why not? 为什么不?

Because it has already been specified in the prior connect(). 因为它已经在先前的connect().指定了connect().

Oracle's documentation states 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. 仅在连接此通道的套接字的情况下才可以调用此方法,在这种情况下,它将直接向套接字的对等方发送数据报。 What does it mean by "the socket's peer"? “套接字的对等方”是什么意思?

The target IP:port to which you connected the channel. 您将通道连接到的目标IP:端口。

DatagramChannel is part of java.nio package. DatagramChannel是java.nio包的一部分。 It will take a parameter as ByteBuffer to send to the connected channel. 它将参数作为ByteBuffer发送到连接的通道。 Before trying to send you should connect using InetSocketAddress, which takes the host ip and port as parameter. 在尝试发送之前,您应该使用InetSocketAddress进行连接,它以主机ip和端口为参数。 connect(SocketAddress remote) --> connects the channel to the underlying socket. connect(SocketAddress remote)->将通道连接到基础套接字。 you can get the socket from Datagramchannel because channel is non blocking io used for read/write purpose. 您可以从Datagramchannel获取套接字,因为channel不阻塞io,用于读/写目的。 socket peer's is nothing but the remote machine. 套接字对等方不过是远程计算机。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM