1
我在使用Rust實驗BitTorrent跟蹤器協議時遇到了此問題。UdpSocket.send_to失敗,出現「無效參數」
這是應該發送使用UDP套接字的一些數據失敗簡單的程序:
use std::collections::HashSet;
use std::io::net::addrinfo;
use std::io::net::ip::{Ipv4Addr, SocketAddr, IpAddr};
use std::io::net::udp::UdpSocket;
fn main() {
let addr = SocketAddr { ip: Ipv4Addr(127, 0, 0, 1), port: 34254 };
let mut socket = match UdpSocket::bind(addr) {
Ok(s) => s,
Err(e) => panic!("couldn't bind socket: {}", e),
};
let host_addr = "tracker.openbittorrent.com";
let port = "80";
let host_ips: HashSet<IpAddr> =
addrinfo::get_host_addresses(host_addr.as_slice()).unwrap().into_iter().collect();
for host_ip in host_ips.into_iter() {
let socket_addr = SocketAddr {
ip: host_ip,
port: from_str(port).unwrap(),
};
println!("Sending to {}", socket_addr);
let data: &[u8] = [0x1, 0x2, 0x3];
println!("{}", socket.send_to(data, socket_addr));
}
}
輸出:
Sending to 31.172.63.252:80
Err(invalid argument (Invalid argument))
Sending to 31.172.63.253:80
Err(invalid argument (Invalid argument))
這是怎麼回事不對任何想法?
這看起來類似於http://stackoverflow.com/questions/7505315/invalid-argument-when-sending-udp-packet-but-它,它有幫助嗎? – 2014-11-04 12:23:08