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

Is there a way to create an UdpSocket without bind() for just send_to()?

我正在尝试做相当于 this piece of Ruby:

def color=(color)
  @color = color
  any_bar = UDPSocket.new
  any_bar.connect HOSTNAME, @port
  any_bar.send @color, 0
  any_bar.close
end

没有 bind(),我看不到 initialize a UdpSocket from the Rust API documentation 的任何其他方式。

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

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