3

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

Reginald Marr
  • 387
  • 2
  • 16
  • This is pretty basic stuff, I really suggest reading [the book](https://doc.rust-lang.org/book/) and especially the chapter about [error handling](https://doc.rust-lang.org/book/ch09-02-recoverable-errors-with-result.html) – Svetlin Zarev Jul 11 '19 at 04:34
  • 1
    Possible duplicate of [How to do error handling in Rust and what are the common pitfalls?](https://stackoverflow.com/questions/30505639/how-to-do-error-handling-in-rust-and-what-are-the-common-pitfalls) – Svetlin Zarev Jul 11 '19 at 04:37
  • I have been going through the book however I'm still new to the concepts (like handling returns with error portion) so I overlooked it here. – Reginald Marr Jul 11 '19 at 13:20

1 Answers1

1
let socket = UdpSocket::bind(connection);

This call returns Result which could represent success value or error value. unwrap() forces it to yield success value (content of Ok). It also panics in case there is error (value is Err).

let socket = UdpSocket::bind(connection).unwrap();

OR you can use expect as well which will print the message before panicking.

let socket = UdpSocket::bind(connection).expect("failed to bind host socket");
ravi
  • 10,994
  • 1
  • 18
  • 36