Rust std::net::UdpSocket没有名为recv_的方法
我正在编写一个用于创建udp客户端/服务器应用程序的程序。但当尝试使用send_to或recv_from方法时,我会出现以下错误: [E0599]在当前作用域中找不到类型Rust std::net::UdpSocket没有名为recv_的方法,rust,udp,Rust,Udp,我正在编写一个用于创建udp客户端/服务器应用程序的程序。但当尝试使用send_to或recv_from方法时,我会出现以下错误: [E0599]在当前作用域中找不到类型std::result::result的名为send_to的方法 我没有以任何方式修改cargo.toml文件,但是我没有想到我会这样做,因为我使用的是rust版本1.35.0的标准库 客户端和服务器都是使用cargo new[filename]--bin创建的,代码在main.rs中 客户 use std::net::{Ipv
std::result::result
的名为send_to
的方法
我没有以任何方式修改cargo.toml文件,但是我没有想到我会这样做,因为我使用的是rust版本1.35.0的标准库
客户端和服务器都是使用cargo new[filename]--bin创建的,代码在main.rs中
客户
use std::net::{Ipv4Addr, SocketAddrV4, UdpSocket};
use std::io;
fn snd() -> Result<(), io::Error> {
// Define the local connection (to send the data from)
let ip = Ipv4Addr::new(127, 0, 0, 1);
let connection = SocketAddrV4::new(ip, 9992);
// Bind the socket
// let socket = try!(UdpSocket::bind(connection));
let socket = UdpSocket::bind(connection);
// Define the remote connection (to send the data to)
let connection2 = SocketAddrV4::new(ip, 9991);
// Send data via the socket
let buf = &[0x01, 0x02, 0x03];
socket.send_to(buf, &connection2);
println!("{:?}", buf);
Ok(())
}
fn main() {
match snd() {
Ok(()) => println!("All snd-ing went well"),
Err(err) => println!("Error: {:?}", err),
}
}
使用std::net:{Ipv4Addr,SocketAddrV4,UdpSocket};
使用std::io;
fn snd()->结果{
//定义本地连接(从中发送数据)
让ip=Ipv4Addr::new(127,0,0,1);
let connection=SocketAddrV4::new(ip,9992);
//捆绑插座
//让socket=try!(UdpSocket::bind(connection));
让socket=UdpSocket::bind(连接);
//定义远程连接(将数据发送到)
让connection2=SocketAddrV4::new(ip,9991);
//通过套接字发送数据
设buf=&[0x01,0x02,0x03];
套接字。发送到(buf和连接2);
println!(“{:?}”,buf);
好(())
}
fn main(){
匹配snd(){
好的(())=>println!(“一切进展顺利”),
Err(Err)=>println!(“错误:{:?}”,Err),
}
}
服务器
use std::net::{Ipv4Addr, SocketAddrV4, UdpSocket};
use std::io;
fn recv() -> Result<(), io::Error> {
// Define the local connection information
let ip = Ipv4Addr::new(127, 0, 0, 1);
let connection = SocketAddrV4::new(ip, 9991);
// Bind the socket
// let socket = try!(UdpSocket::bind(connection));
let socket = UdpSocket::bind(connection);
// Read from the socket
let mut buf = [0; 10];
// let (amt, src) = try!(socket.recv_from(&mut buf));
let (amt, src) = socket.recv_from(&mut buf).expect("Didn't recieve data");
// Print only the valid data (slice)
println!("{:?}", &buf[0 .. amt]);
Ok(())
}
fn main() {
match recv() {
Ok(()) => println!("All recv-ing went well"),
Err(err) => println!("Error: {:?}", err),
}
}
使用std::net:{Ipv4Addr,SocketAddrV4,UdpSocket};
使用std::io;
fn recv()->结果{
//定义本地连接信息
让ip=Ipv4Addr::new(127,0,0,1);
let connection=SocketAddrV4::new(ip,9991);
//捆绑插座
//让socket=try!(UdpSocket::bind(connection));
让socket=UdpSocket::bind(连接);
//从插座读取数据
设mut buf=[0;10];
//let(amt,src)=try!(socket.recv_from(&mut buf));
let(amt,src)=socket.recv_from(&mut buf).expect(“未收到数据”);
//仅打印有效数据(切片)
println!(“{:?},&buf[0..amt]);
好(())
}
fn main(){
匹配recv(){
好的(())=>println!(“一切顺利”),
Err(Err)=>println!(“错误:{:?}”,Err),
}
}
在使用cargo build时,服务器端也会出现以下错误
[E0599]对于当前范围中的类型std::result::result
,未找到名为recv_from
的方法
编辑:
这似乎是由于忘记处理UdpSocket::bind返回中的错误处理。这一点在文章中有更详细的讨论
如下文所述
这个问题基本上保持不变,因为我没有看到很多问题或示例明确地处理net::UdpSocket
let socket = UdpSocket::bind(connection);
此调用返回可能表示成功值或错误值的结果<代码>展开()。如果出现错误(值为Err
),它也会恐慌
或者您也可以使用expect,它会在恐慌之前打印消息
let socket = UdpSocket::bind(connection).expect("failed to bind host socket");
这是非常基本的东西,我真的建议阅读,特别是关于可能重复的章节,我已经阅读了这本书,但是我对这些概念还是很陌生(比如处理带有错误部分的返回),所以我在这里忽略了它。
let socket = UdpSocket::bind(connection).expect("failed to bind host socket");