Network Programming with Rust
Abhishek Chanda
- English
- ePUB (mobile friendly)
- Available on iOS & Android
Network Programming with Rust
Abhishek Chanda
About This Book
Learn to write servers and network clients using Rust's low-level socket classes with this guideAbout This Book⢠Build a solid foundation in Rust while also mastering important network programming details⢠Leverage the power of a number of available libraries to perform network operations in Rust⢠Develop a fully functional web server to gain the skills you need, fastWho This Book Is ForThis book is for software developers who want to write networking software with Rust. A basic familiarity with networking concepts is assumed. Beginner-level knowledge of Rust will help but is not necessary.What You Will Learn⢠Appreciate why networking is important in implementing distributed systems⢠Write a non-asynchronous echo server over TCP that talks to a client over a network⢠Parse JSON and binary data using parser combinators such as nom⢠Write an HTTP client that talks to the server using reqwest⢠Modify an existing Rust HTTTP server and add SSL to it⢠Master asynchronous programming support in Rust⢠Use external packages in a Rust projectIn DetailRust is low-level enough to provide fine-grained control over memory while providing safety through compile-time validation. This makes it uniquely suitable for writing low-level networking applications.This book is divided into three main parts that will take you on an exciting journey of building a fully functional web server. The book starts with a solid introduction to Rust and essential networking concepts. This will lay a foundation for, and set the tone of, the entire book. In the second part, we will take an in-depth look at using Rust for networking software. From client-server networking using sockets to IPv4/v6, DNS, TCP, UDP, you will also learn about serializing and deserializing data using serde. The book shows how to communicate with REST servers over HTTP. The final part of the book discusses asynchronous network programming using the Tokio stack. Given the importance of security for modern systems, you will see how Rust supports common primitives such as TLS and public-key cryptography.After reading this book, you will be more than confident enough to use Rust to build effective networking softwareStyle and approachThis book will get you started with building networking software in Rust by taking you through all the essential concepts.
Frequently asked questions
Information
Asynchronous Network Programming Using Tokio
- Futures abstraction in Rust
- Asynchronous programming using the tokio stack
Looking into the Future
trait Future {
type Item;
type Error;
fn poll(&mut self) -> Poll<Self::Item, Self::Error>;
...
}
type Poll<T, E> = Result<Async<T>, E>;
pub enum Async<T> { Ready(T), NotReady, }
- Ok(Async::Ready(result)) when the operation has completed successfully and the result is in the inner variable called result.
- Ok(Async::NotReady) when the operation has not completed yet and a result is not available. Note that this does not indicate an error condition.
- Err(e) when the operation ran into an error. No result is available in this case.
$ cargo new --bin futures-example
[package]
name = "futures-example"
version = "0.1.0"
authors = ["Foo<[email protected]>"]
[dependencies]
futures = "0.1.17"
futures-cpupool = "0.1.7"
// ch7/futures-example/src/main.rs
#![feature(conservative_impl_trait)]
extern crate futures;
extern crate futures_cpupool;
use std::io;
use futures::Future;
use futures_cpupool::CpuPool;
// This implementation returns a boxed future
fn check_prime_boxed(n: u64) -> Box<Future<Item = bool, Error = io::Error>> {
for i in 2..n {
if n % i == 0 { return Box::new(futures::future::ok(false)); }
}
Box::new(futures::future::ok(true))
}
// This returns a future using impl trait
fn check_prime_impl_trait(n: u64) -> impl Future<Item = bool, Error = io::Error> {
for i in 2..n {
if n % i == 0 { return futures::future::ok(false); }
}
futures::future::ok(true)
}
// This does not return a future
fn check_prime(n: u64) -> bool {
for i in 2..n {
if n % i == 0 { return false }
}
true
}
fn main() {
let input: u64 = 58466453;
println!("Right before first call");
let res_one = check_prime_boxed(input);
println!("Called check_prime_boxed");
let res_two = check_prime_impl_trait(input);
println!("Called check_prime_impl_trait");
println!("Results are {} and {}", res_one.wait().unwrap(),
res_two.wait().unwrap());
let thread_pool = CpuPool::new(4);
let res_three = thread_pool.spawn_fn(move || {
let temp = check_prime(input);
let result: Result<bool, ()> = Ok(temp);
result
});
println!("Called check_prime in another thread");
println!("Result from the last call: {}", res_three.wait().unwrap());
}