-
Notifications
You must be signed in to change notification settings - Fork 259
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
+597
−199
Merged
Changes from 11 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
324da13
More beginner-friendly TCP server example
lbernick aaf281b
More beginner-friendly TCP server example
lbernick 12d6fad
Merge
lbernick d14d093
Add new words to dictionary
lbernick eecf6ee
Responding to comments from Didrik
lbernick fb5de20
Revert formatting changes to other examples
lbernick 3ab9e17
Revert formatting changes to other examples
lbernick b8bc9d8
Merge branch 'tcpserver' of github.com:lbernick/async-book into tcpse…
lbernick 57093a6
Revert formatting for example 2
lbernick 2a77cbd
Introduce concurrency before multithreading
lbernick 8619046
Address second round of comments from Didrik
lbernick b9d975f
Formatting and spellcheck
lbernick 61efda5
Apply suggestions from code review
lbernick 6352134
Update in response to comments from Tyler
lbernick f133e20
Merge branch 'tcpserver' of github.com:lbernick/async-book into tcpse…
lbernick File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
[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] | ||
version = "1.6" | ||
features = ["attributes"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
#[async_std::main] | ||
async fn main() { | ||
let listener = TcpListener::bind("127.0.0.1:7878").unwrap(); | ||
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
[package] | ||
name = "slow_request" | ||
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] | ||
version = "1.6" | ||
features = ["attributes"] | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
use std::fs; | ||
use std::io::{Read, Write}; | ||
use std::net::TcpListener; | ||
use std::net::TcpStream; | ||
use std::time::Duration; | ||
|
||
#[async_std::main] | ||
async fn main() { | ||
let listener = TcpListener::bind("127.0.0.1:7878").unwrap(); | ||
for stream in listener.incoming() { | ||
let stream = stream.unwrap(); | ||
handle_connection(stream).await; | ||
} | ||
} | ||
|
||
// ANCHOR: handle_connection | ||
use async_std::task; | ||
|
||
async fn handle_connection(mut stream: TcpStream) { | ||
let mut buffer = [0; 1024]; | ||
stream.read(&mut buffer).unwrap(); | ||
|
||
let get = b"GET / HTTP/1.1\r\n"; | ||
let sleep = b"GET /sleep HTTP/1.1\r\n"; | ||
|
||
let (status_line, filename) = if buffer.starts_with(get) { | ||
("HTTP/1.1 200 OK\r\n\r\n", "hello.html") | ||
} else if buffer.starts_with(sleep) { | ||
task::sleep(Duration::from_secs(5)).await; | ||
("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(); | ||
|
||
let response = format!("{}{}", status_line, contents); | ||
stream.write(response.as_bytes()).unwrap(); | ||
stream.flush().unwrap(); | ||
} | ||
// ANCHOR_END: handle_connection |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
[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] | ||
futures = "0.3" | ||
|
||
[dependencies.async-std] | ||
version = "1.6" | ||
features = ["attributes"] |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.