sockets - UdpSocket.send_to fails with "invalid argument" -
sockets - UdpSocket.send_to fails with "invalid argument" -
i came across problem while experimenting bittorrent tracker protocol using rust.
this simple programme supposed send info using udp sockets failing:
use std::collections::hashset; utilize std::io::net::addrinfo; utilize std::io::net::ip::{ipv4addr, socketaddr, ipaddr}; utilize std::io::net::udp::udpsocket; fn main() { allow addr = socketaddr { ip: ipv4addr(127, 0, 0, 1), port: 34254 }; allow mut socket = match udpsocket::bind(addr) { ok(s) => s, err(e) => panic!("couldn't bind socket: {}", e), }; allow host_addr = "tracker.openbittorrent.com"; allow port = "80"; allow host_ips: hashset<ipaddr> = addrinfo::get_host_addresses(host_addr.as_slice()).unwrap().into_iter().collect(); host_ip in host_ips.into_iter() { allow socket_addr = socketaddr { ip: host_ip, port: from_str(port).unwrap(), }; println!("sending {}", socket_addr); allow data: &[u8] = [0x1, 0x2, 0x3]; println!("{}", socket.send_to(data, socket_addr)); } }
output:
sending 31.172.63.252:80 err(invalid argument (invalid argument)) sending 31.172.63.253:80 err(invalid argument (invalid argument))
any ideas what's going wrong?
you binding socket localhost (the loopback interface), , trying communicate through socket address not on interface. if instead bind 0.0.0.0
, succeed. means "all ipv4 interfaces". can bind more specific address if necessary.
sockets udp rust
Comments
Post a Comment