This code fails at runtime. Only on MacOS.
It would be nice to have a clearer error rather than "Invalid Argument"
in this double colon IPv6 address case.
Source: bevy_simple_networking/simple_client.rs at main · bacongobbler/bevy_simple_networking · GitHub
use std::net::{SocketAddr, UdpSocket};
fn main() {
// Because of this
let bind_addr: SocketAddr = "[::]:8000".parse().expect("could not parse addr");
let remote_addr: SocketAddr = "0.0.0.0:8001".parse().expect("could not parse addr");
dbg!(remote_addr, bind_addr);
let socket = dbg!(UdpSocket::bind(bind_addr).expect("could not bind socket"));
// Panics there
socket
.connect(remote_addr)
.expect("could not connect to server");
dbg!(socket);
}
$ rustc main.rs
$ ./main
[main.rs:13] remote_addr = 0.0.0.0:8001
[main.rs:13] bind_addr = [::]:8000
[main.rs:15] UdpSocket::bind(bind_addr).expect("could not bind socket") = UdpSocket {
addr: [::]:8000,
fd: 3,
}
thread 'main' panicked at 'could not connect to server: Os { code: 22, kind: InvalidInput, message: "Invalid argument" }', main.rs:20:10
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Solution:
It seems that on MacOS you can't connect to IPv4 address if you are bound to IPv6 and vice versa.
"[::]:8000"
is IPv6, but "0.0.0.0:8001"
is IPv4. So please use this check from tokio: