INADDR_ANY for Rust UDP sockets

INADDR_ANY doesn't seem to be defined anywhere in Rust land. In C, to bind a socket in a client in preparation for sending, you'd bind to INADDR_ANY, let it bind to some or all available interfaces, and go from there. INADDR_ANY is just the zero IP address.

So I tried

let mut socket = UdpSocket("0")?;

That yields an "Invalid socket address" error. What's the "Any" address in Rust?

let mut socket = UdpSocket::bind("0.0.0.0:0")?;

Fair enough. There really should be a named constant for that, though.

3 Likes

Yeah, I agree. At least an example in the docs until then.

1 Like

Right. The docs all show talking to 127.0.0.1, which is "localhost" and only useful for talking to yourself.

It does have a name: Ipv4Addr in std::net - Rust

4 Likes

Ah, that's where it is. Thanks. That should be used in the UDP documentation examples.

I've written a PR to make this easier to find in the future: https://github.com/rust-lang/rust/pull/84257

6 Likes

The feedback loop between the Rust users and developers is quite amazing.

3 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.