有没有办法只为send_to()创建一个没有bind()的UdpSocket?

cra*_*sim 7 rust

我正在尝试相当于这件Ruby:

def color=(color)
  @color = color
  any_bar = UDPSocket.new
  any_bar.connect HOSTNAME, @port
  any_bar.send @color, 0
  any_bar.close
end
Run Code Online (Sandbox Code Playgroud)

我看不到任何其他的方式来初始化UdpSocket从锈病API文档没有bind().

haa*_*vee 10

我会尝试::bind("0.0.0.0:0")- 这应该让O/S为您选择一个IP /端口.这可能足以让瞬态套接字发送简单的数据报.

注意:这也是在未绑定的UDP套接字上使用send_to时也会发生的情况,例如使用从socket()系统调用返回的fd而不调用bind() - O/S分配一个IP /端口来发送数据报.