nelsonjchen
nelsonjchen

Reputation: 395

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

I'm trying to do the equivalent of 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

I can't see any other way to initialize a UdpSocket from the Rust API documentation without bind().

Upvotes: 17

Views: 4882

Answers (1)

emvee
emvee

Reputation: 4449

I would try ::bind("0.0.0.0:0") - this should let the O/S choose an IP/port for you. This might be good enough for a transient socket to send a simple datagram with.

Note: this is what happens too when using sendto() on an unbound UDP socket too, e.g. using the fd returned from the socket() system call without calling bind() - the O/S allocates an IP/port to send your datagram from.

Upvotes: 17

Related Questions