In today's studio you will explore how to use Tokio to develop a simple server, which can handle multiple I/O and connection establishment events concurrently. You will also explore how Tokio supports asynchronous execution of code for event handling.
Please complete the following required exercises. I encourage you to please work in groups of 2 or 3 people on each studio (and the groups are allowed to change from studio to studio) though if you would prefer to complete any studio by yourself that is also allowed.
As you work through these exercises, please record your answers, and when you finish them please log into Canvas, select this course in this semester, and then on the Canvas page for this studio assignment upload (1) a file containing your answers and (2) any code you produced while answering the exercises. Only one submission per team, please, and if you need to re-submit it the person who originally submitted the studio should please be the one to do that.
Make sure that the name of each person who worked on these exercises is listed in the first answer, and make sure you number each of your responses so it is easy to match your responses with each exercise.
As the answer to the first exercise, list the names of the people who worked together on this studio.
Log in using ssh into shell.cec.wustl.edu using your WUSTL
Key id and password, issue the qlogin command to
get onto one of the Linux Lab machines, and then within the directory you
created for this course, add a new directory for this studio.
In that new directory, use the cargo new command to create a new
server package (named e.g., reactive_server).
Change into the src directory and modify the
main.rs file that was automatically generated there so that it requires
one command line argument (a port number on which the server will listen) and prints out
the arugment it received.
Otherwise, the program should print out a helpful usage message to that effect and return an error.
Compile and run your server program with and without a single command line argument, and
as the answer to this exercise show the output that was produced in each case.
Add the following to the [dependencies] section of your server package's Cargo.toml file:
tokio = { version = "1.53.0", features = ["full"] }
Try to compile and run your program, and if you are running on the Linux Lab machines you should see an error
to the effect that the Tokio version that is installed requires a newer version of the rustc compiler
than we have installed.
To work around that error, you should please downgrade the version of Tokio to 1.53.0
by issuing the following command in the terminal window:
cargo update -p tokio@1.54.1 --precise 1.53.0
As the answer to this exercise please (1) indicate whether or not you saw an error like that, and if you did, show
(2) the error that was produced, (3) the output that was generated when you downgraded Tokio to version 1.53.0,
and (4) the output showing successful compilation of your program after that.
Add the following statement to your program:
use tokio::net::TcpListener;
and then modify your main function so that rather than saying fn main() is says:
#[tokio::main]
async fn main()
instead.
Then, construct a string with an IP address on which the server will listen, which contains
"127.0.0.1:" followed by the port number that was passed on the command line
(for example, if port number 8009 were passed in, the resulting address would be
"127.0.0.1:8009").
Your server should call the bind method of a tokio::net::TcpListener and
await the result of that async call - if it succeeds the program should report the address
on which it can now listen for connection requests and return success, or otherwise it should return an error.
Compile and run your server with a valid port number like 8009 and an invalid one like 1001, and as the answer to this exercise please show the output from those runs.
Add the following statements to your program:
use tokio::sync::mpsc;
use std::sync::atomic::{AtomicBool, Ordering};
use std::io::{self, BufRead};
static QUIT_FLAG: AtomicBool = AtomicBool::new(false);
and then modify your main function (just after the code you added in the previous exercise) to add a
while loop that iterates until a call to the load method of the
AtomicBool quit flag you just added evaluates to true.
Just above that loop please declare a Tokio mpsc::channel and extract the transmit and receive
ends of the channel into separate variables (the receive variable should be mutable).
Then use std::thread::spawn and move to launch a separate input thread that reads
lines from io::stdin and pushes them into the channel.
Then use tokio::spawn and async move to launch an asynchronous execution block that
reads lines of text from the channel and prints out each of the lines. If the line that was read is
"quit", the input thread should store true into the quit flag, which should allow the
program to break out of the loop, and return success.
Compile and run your code, and test it by typing in a few other lines, and then typing in "quit" to confirm the program exits successfully. As the answer to this exercise, please show the output from that run.
Modify your main function so that it obtains a listener by unwrapping the result of the
bind call, and in each iteration of its while loop awaits the result of calling the
accept method of the listener - if that call fails the program should return an error, or otherwise
it should unwrap the result into socket and address variables, and should print out the
address that was extracted.
Compile and run your server, and in a separate terminal window use your studio 18 client to connect to the server and confirm it is accepting connections. As the answer to this exercise, please show the output the server produced during those tests.
Note that if you are using the loopback IP address
(127.0.0.1) you will need to make sure qlogin gives you the same host maching in the
Linux Lab, for your client and server, and you also may need to modify which port each is assuming.
Note that because the server may be in an unfinished accept call when
you type quit into the standard input stream, you may have to have your client
connect one more time after that in order to have that call return. That is fine for this exercise,
though in practice you would use a cancellation token and logic for handling it to address that issue.
For this studio, please turn in the following:
Page posted 4:05am Thursday November 12, 2026, by Chris Gill.