PacktPublishing/Network-Programming-with-Rust

Chapter3 - lookup_dns

tomus85 opened this issue · 1 comments

https://github.com/PacktPublishing/Network-Programming-with-Rust/blob/master/Chapter03/lookup-host.rs

Could not compile the example from the book. (Also link above) Keep saying it cannot find lookup_dns. I'm on rust nightly.

However, I have managed to find a solution, so I thought I'll share it here.

First create a cargo project

cargo new lookup_dns

In the Cargo.toml, add the following to the dependencies

dns-lookup = "1.0.2"

Them add the following code:

use std::env;
use std::net::IpAddr;
use dns_lookup::lookup_host;

fn main() {
    let args: Vec<_> = env::args().collect();

    if args.len() != 2 {
        eprintln!("Please provide only one host name");
        std::process::exit(1);
    }
    else {
        let addresses: Vec<IpAddr> = lookup_host(&args[1]).unwrap();

        for address in addresses {
            println!("{}", address);
        }
    }
}

Build and execute the project

cargo build
cargo run google.co.uk

Thanks for posting your solution! I've been looking everywhere to try and resolve this 😁