UdpSocket weird binding behaviour

I'm trying to use the below code, but the connect(remote_addr) fail (error message below).
But it work fine when I use UdpSocket::bind("0.0.0.0:0").

0.0.0.0:0 and [::]:0 aren't supposed to be the same thing ?

I don't know if it's a rust-specific issue or just how network work, but I would appreciate an explanation :sweat_smile:.

    let remote_addr: SocketAddr = "127.0.0.1:4567".parse().expect("could not parse addr");
    let socket = UdpSocket::bind("[::]:0").expect("could not bind socket");
    socket
        .connect(remote_addr)
        .expect("could not connect to server");
thread 'main' panicked at 'could not connect to server: Os { code: 10014, kind: Uncategorized, message: "Le système a détecté une adresse de pointeur non valide en essayant d’utiliser un argument pointeur dans un appel." }'

You know, the one is a IPv4 address and the other is a IPv6 address. :slight_smile:

This distinction matter. 0.0.0.0 is a IPv4 unspecified address. Binding the socket on it means to listen every IPv4 network interfaces - yes, IPv4 interfaces. IPv4 and IPv6 do not share network interfaces on its own. To connect to the IPv6 loopback interface, use [::1]:4567 instead which is a IPv6 locahost address.

2 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.