Skip to content

More beginner-friendly TCP server example #102

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 15 commits into from
Sep 16, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 0 additions & 18 deletions examples/01_05_http_server/Cargo.toml

This file was deleted.

91 changes: 0 additions & 91 deletions examples/01_05_http_server/src/lib.rs

This file was deleted.

11 changes: 11 additions & 0 deletions examples/08_01_sync_tcp_server/404.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Hello!</title>
</head>
<body>
<h1>Oops!</h1>
<p>Sorry, I don't know what you're asking for.</p>
</body>
</html>
9 changes: 9 additions & 0 deletions examples/08_01_sync_tcp_server/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "sync_tcp_server"
version = "0.1.0"
authors = ["Your Name <[email protected]"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
11 changes: 11 additions & 0 deletions examples/08_01_sync_tcp_server/hello.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Hello!</title>
</head>
<body>
<h1>Hello!</h1>
<p>Hi from Rust</p>
</body>
</html>
39 changes: 39 additions & 0 deletions examples/08_01_sync_tcp_server/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
use std::fs;
use std::io::prelude::*;
use std::net::TcpListener;
use std::net::TcpStream;

fn main() {
// Listen for incoming TCP connections on localhost port 7878
let listener = TcpListener::bind("127.0.0.1:7878").unwrap();

// Block forever, handling each request that arrives at this IP address
for stream in listener.incoming() {
let stream = stream.unwrap();

handle_connection(stream);
}
}

fn handle_connection(mut stream: TcpStream) {
// Read the first 1024 bytes of data from the stream
let mut buffer = [0; 1024];
stream.read(&mut buffer).unwrap();

let get = b"GET / HTTP/1.1\r\n";

// Respond with greetings or a 404,
// depending on the data in the request
let (status_line, filename) = if buffer.starts_with(get) {
("HTTP/1.1 200 OK\r\n\r\n", "hello.html")
} else {
("HTTP/1.1 404 NOT FOUND\r\n\r\n", "404.html")
};
let contents = fs::read_to_string(filename).unwrap();

// Write response back to the stream,
// and flush the stream to ensure the response is sent back to the client
let response = format!("{}{}", status_line, contents);
stream.write(response.as_bytes()).unwrap();
stream.flush().unwrap();
}
10 changes: 10 additions & 0 deletions examples/08_02_async_tcp_server/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "async_tcp_server"
version = "0.1.0"
authors = ["Your Name <[email protected]"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
async-std = "1.6"
23 changes: 23 additions & 0 deletions examples/08_02_async_tcp_server/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use std::net::TcpListener;
use std::net::TcpStream;

use async_std::task::block_on;

// ANCHOR: main_func
fn main() {
let listener = TcpListener::bind("127.0.0.1:7878").unwrap();
block_on(async {
for stream in listener.incoming() {
let stream = stream.unwrap();
// Warning: This is not concurrent!
handle_connection(stream).await;
}
})
}
// ANCHOR_END: main_func

// ANCHOR: handle_connection_async
async fn handle_connection(mut stream: TcpStream) {
//<-- snip -->
}
// ANCHOR_END: handle_connection_async
10 changes: 10 additions & 0 deletions examples/08_03_concurrent_tcp_server/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "concurrent_tcp_server"
version = "0.1.0"
authors = ["Your Name <[email protected]"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
async-std = "1.6"
20 changes: 20 additions & 0 deletions examples/08_03_concurrent_tcp_server/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use std::net::TcpListener;
use std::net::TcpStream;

// ANCHOR: main_func
use async_std::task::{block_on, spawn};

fn main() {
let listener = TcpListener::bind("127.0.0.1:7878").unwrap();
block_on(async {
for stream in listener.incoming() {
let stream = stream.unwrap();
spawn(async {handle_connection(stream).await} );
}
})
}
// ANCHOR_END: main_func

async fn handle_connection(mut stream: TcpStream) {
//<-- snip -->
}
10 changes: 10 additions & 0 deletions examples/08_04_nonblocking_tcp_server/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "nonblocking_tcp_server"
version = "0.1.0"
authors = ["Your Name <[email protected]"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
async-std = "1.6"
29 changes: 29 additions & 0 deletions examples/08_04_nonblocking_tcp_server/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// ANCHOR: main_func
use async_std::net::{TcpListener, TcpStream};
use async_std::task::{block_on, spawn};

fn main() {
block_on(async {
let listener = TcpListener::bind("127.0.0.1:7878").await.unwrap();

loop {
let (stream, _) = listener.accept().await.unwrap();
spawn(handle_connection(stream));
}
})
}
// ANCHOR_END: main_func

const response: &'static str = "hello";
// ANCHOR: handle_connection
use async_std::prelude::*;

async fn handle_connection(mut stream: TcpStream) {
let mut buffer = [0; 1024];
stream.read(&mut buffer).await.unwrap();

//<-- snip -->
stream.write(response.as_bytes()).await.unwrap();
stream.flush().await.unwrap();
}
// ANCHOR_END: handle_connection
11 changes: 11 additions & 0 deletions examples/08_05_final_tcp_server/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
name = "final_tcp_server"
version = "0.1.0"
authors = ["Your Name <[email protected]"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
async-std = "1.6"
futures = "0.3"
11 changes: 11 additions & 0 deletions examples/08_05_final_tcp_server/hello.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Hello!</title>
</head>
<body>
<h1>Hello!</h1>
<p>Hi from Rust</p>
</body>
</html>
Loading