Skip to main content Link Menu Expand (external link) Document Search Copy Copied

Socket

Table of contents

  1. Listening for connections
  2. Handling connections

In this part, you will implement the server socket, allowing it to listen for and handle connections.

For the rest of the homework, you will need to read documentation in order to complete the required tasks. Rust documentation can be a little tricky since the documentation is versioned, so search results on Google often link to outdated versions of the documentation. Always make sure that you do not see this warning sign at the top of the documentation, otherwise you are looking at outdated information:

Rust version warning

Listening for connections

Let’s now take a look at src/server.rs. Currently, the server::main function parses arguments, sets the current working directory of the server to the directory supplied by the --files argument, and creates a thread pool that starts running the server::listen function, which currently does nothing.

Begin implementing the server::listen function, which should bind the server socket to the provided port with a host of 0.0.0.0. The documentation for tokio::net::TcpListener may come in handy.

You are not expected to pass any additional tests after completing this section.

Handling connections

Now that we listen for connections, our HTTP server must be able to accept and handle new connections.

Update your implementation of the server::listen function. Since we want the server to be able to handle an indefinite number of requests, this should be done using an infinite loop that repeatedly accepts a connection and spawns a tokio task that calls the server::handle_socket function on the resulting socket (don’t worry that the handler function currently always panics; you will implement the correct functionality in the next part of the assignment).

Once again, take a look at the documentation for tokio::net::TcpListener as well as that for tokio::spawn.

There will be autograder tests that ensure you can handle several requests concurrently, but you are not yet expected to pass any additional tests yet.

If you would like to sanity check your implementation before moving on, you can start your server by following the instructions in the Usage section. It should print out some configuration and stay running indefinitely. In another terminal, try running curl http://0.0.0.0:8000.

If your implementation is correct, your server should crash with an error message that looks like the following, which indicates that the server::handle_socket function was called successfully:

thread 'tokio-runtime-worker' panicked at 'not yet implemented: TODO: Part 3', src/server.rs:64:5