Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

rust

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().

like image 740
nelsonjchen Avatar asked Apr 11 '15 22:04

nelsonjchen


People also ask

Is it necessary to bind a UDP socket?

As far as I remember bind is not required for a UDP socket because a bind call is made for you by the stack.

How does a UDP socket work?

UDP socket routines enable simple IP communication using the user datagram protocol (UDP). The User Datagram Protocol (UDP) runs on top of the Internet Protocol (IP) and was developed for applications that do not require reliability, acknowledgment, or flow control features at the transport layer.


1 Answers

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.

like image 166
haavee Avatar answered Oct 19 '22 07:10

haavee