< 返回博客

rdigua 发表于 2019-04-12 23:12

Tags:life,tyro,learning

Learning the rust book

Network Programming with Rust

Who this book is for

This book's target audience is a software engineer who is interested in writing networking software using Rust.

What this book covers

What this book covers

Chapter 1,

Introduction to Client/Server Networking, starts the book with a gentle introduction to computer networking from the ground up. This includes IP addressing, TCP/UDP, and DNS. This forms the basis of our discussions in later chapters.

Chapter 2,

Introduction to Rust and its Ecosystem, contains an introduction to Rust. This is an overall introduction that should be good enough to get the reader started. We do assume some familiarity with programming.

Getting started with Rust

The Rust toolchain installer is available at: https://www.rustup.rs/. The following commands will install all three versions of the toolchain on a system. For the examples in this book, we will use a Linux machine running Ubuntu 16.04. While most of Rust should not depend on the OS, there can be minor differences.

We will point out any strict dependencies on the OS:

$ curl https://sh.rustup.rs -sSf | sh

source $HOME/.cargo/env

  • rustup install nightly beta We will need to put Cargo's bin directory to our PATH by editing .bashrc. Run the following to do that:

$ echo "export PATH=$HOME/.cargo/bin:$PATH" >> ~/.bashrc A Rust installation comes with a lot of documentation built in; they can be accessed by running the following command. This should open up the documentation in a browser window:

  • rustup doc The next step is to set up a Rust project and run it, all using Cargo:

  • cargo new --bin hello-rust This tells Cargo to set up a new project called hello-rust in the current directory. Cargo will create a directory of that name and set up the basic structure. Since the type of this project is set to be a binary, Cargo will generate a file called main.rs which will have an empty main function, the entry point for the application. The other (default) option here is that of a library, in this case, a file named lib.rs will be generated. The file named Cargo.toml has a bunch of metadata for the current project and is used by Cargo. All source code is located in the src directory:

$ cargo run

Introduction to the borrow checker

// chapter2/ownership-heap-fixed.rs

fn main() {
    let s = String::from("Test");
    heap_example(s);
}

fn heap_example(input: String) {
    let mystr = input;
    let _otherstr = mystr.clone(); // if ... let _otherstr = mystr; note: move occurs because `mystr` has type `std::string::String`, which does not implement the `Copy` trait
    println!("{}", mystr);
}

$ rustc ownership-heap-fixed.rs && ./ownership-heap-fixed Test

// chapter2/lifetime.rs

fn main() {
    let v1 = vec![1, 2, 3, 4, 5];
    let v2 = vec![1, 2];

    println!("{:?}", longer_vector(&v1, &v2));
}

fn longer_vector(x: &[i32], y: &[i32]) -> &[i32] {       //errors help: this function's return type contains a borrowed value, but the signature does not say whether it is borrowed from `x` or `y`

    if x.len() > y.len() { x } else { y }
}


fn longer_vector<'a>(x: &'a[i32], y: &'a[i32]) -> &'a[i32] {  //right
    if x.len() > y.len() { x } else { y }
}

  • mark: **p96 **

Chapter 3,

TCP and UDP Using Rust, dives into using Rust for networking. We start with basic socket programming using the standard library. We then look at some crates from the ecosystem that can be used for network programming.

Chapter 4,

Data Serialization, Deserialization, and Parsing, explains that an important aspect of networked computing is handling data. This chapter is an introduction to serializing and deserializing data using Serde. We also look at parsing using nom and other frameworks.

Chapter 5,

Application Layer Protocols, moves up a layer to look at protocols that operate above TCP/IP. We look at a few crates to work with, such as RPC, SMTP, FTP, and TFTP.

Chapter 6,

Talking HTTP in the Internet, explains that arguably the most common application of the internet is HTTP. We look at crates such as Hyper and Rocket which are used for writing HTTP servers and clients.

Chapter 7,

Asynchronous Network Programming Using Tokio, looks at the Tokio stack for asynchronous programming using futures, streams, and event loops.

Chapter 8, Security,

delves into securing the services we have described so far. This is using certificates and secret keys.

Chapter 9, Appendix,

discusses a number of crates have appeared that propose alternate ways of doing things already covered in this book. This includes the async/await syntax, parsing using Pest, and so on. We will discuss some of these in the appendix.

code

The code bundle for the book is also hosted on GitHub at https://github.com/PacktPublishing/Network-Programming-with-Rust. We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!

words:

related the sake of simplicity revolutionized prominent protocols Eventually silos proximity hierarchy imperative brutally engagements architectural Heap memory

1977 In networking, a Request For Comment (RFC) is a document that describes how a proposed system should work. These are the first steps towards standardizing a protocol or a system. The term internet was first used in RFC 675, which proposed a standard for TCP.

Physical layer: It defines how data is transmitted in the physical medium in terms of its electrical and physical characteristics. This can either be by wire, fiber optic, or a wireless medium. Data link layer: It defines how data is transmitted between two nodes connected by a physical medium. This layer deals with prioritization between multiple parties trying to access the wire simultaneously. Another important function of this layer is to include some redundancy in the transmitted bits to minimize errors during transmission. This is referred to as coding. Network layer: It defines how packets (made up of multiple units of data) are transmitted between networks. Thus, this layer needs to define how to identify hosts and networks uniquely. Transport layer: It defines mechanisms to reliably deliver variable length messages to hosts (in the same or different networks). This layer defines a stream of packets that the receiver can then listen to. Session layer: It defines how applications running on hosts should communicate. This layer needs to differentiate between applications running on the same host and deliver packets to them. Presentation layer: It defines common formats for data representation so that different applications can interlink seamlessly. In some cases, this layer also takes care of security. Application layer: It defines how user-centric applications should send and receive data. An example is the web browser (a user-centric application) using HTTP (an application layer protocol) to talk to a web server.

model

Typically, each computer in a network will have a local DNS server configured in the file /etc/resolv.conf. In most cases, this points to the ISP's DNS server. This might also point to the home Wi-Fi router's DNS server. In that case, the DNS will transparently proxy requests to the ISP's DNS server. The OS will then query that server, asking the IP of the given name www.google.com.

TCP header format

评论区

写评论
Mike Tang 2019-04-13 14:11

这个文章哪儿的?

1 共 1 条评论, 1 页