I am working off a simple example for creating a rust udp client/server application. But when trying to use either the send_to or recv_from methods I get the following error:
[E0599] no method named send_to
found for type std::result::Result<std::net::UdpSocket, std::io::Error>
in the current scope.
I havent altered the cargo.toml file in any way however I did not expect I would have to as I am using the standard library with rust version 1.35.0.
Both the client and server were created using cargo new [filename] --bin and the code is in main.rs
Client
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),
}
}
Server
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),
}
}
I also get the following error on the sever side when using cargo build.
[E0599] no method named recv_from
found for type std::result::Result<std::net::UdpSocket, std::io::Error>
in the current scope.
Edit: It seems this was a result of forgetting to handle the error handling in the return from UdpSocket::bind. This is talked about in more detail in the post How to do error handling in Rust and what are the common pitfalls? as mentioned below.
Leaving this question mostly intact as I have not seen many questions or examples explicitly dealing with net::UdpSocket