Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Part 1. TCP Sockets (10 points)

The first application you will write is a simple web client and server, using a TCP (Transmission Control Protocol) socket.

Your task

Getting Started

To get started, cd into the assignment repository you cloned. Then, cd into the tcp directory.

Create a new cargo project called server with cargo new server. cd into the server directory. This is where you will implement your web server.

You will follow the instructions in the Listening to the TCP Connection section of Chapter 21 in the Rust textbook, with the following adjustments:

  • Ignore the instructions for creating a new cargo project called hello. You’ll write your code in the src/main.rs file in the server project you just created.

  • THIS IS VERY IMPORTANT: Skip the instructions for testing your server with a web browser. This will not work if you’re connected to mcnulty. Instead, you’ll be provided with a simple web client (based on the code presented in class) that you can use to test your server. (Instructions on testing are below)

Setting the Address

As mentioned in class, network addresses consists of 2 pieces: the IP address and the port number (formatted as a string with the IP address and port number separated by a colon - "127.0.0.1"). There is one IP address per computer, and one port number per application running on that computer (it’s actually more complicated than this, but this simplified explanation will work for our purposes).

For this lab, you should always use 127.0.0.1 as the IP address. This is the address included in the Rust textbook example; in other words do not change the IP address in the server code. This is a special address, called the loopback address, that means a computer should send the packets to itself, instead of sending it out to the network. In this lab, your server and client will be running on the same computer, so we dont’ want the traffic to leave your machine.

You’ll notice that the Rust textbook uses 7878 as the port number. Do NOT use this as your port number. Instead, replace the 7878 in your server code with any 4 digit number greater than 1024. Once you choose a number, open up the main.rs file for the client (located in tcp/client/src/). On line 5, you’ll see const SERVER_PORT_NUMBER: u32 = todo!();. Replace the todo!() with the same port number you’ve chosen for your server. This step tells the client where to send the messages to.

Note

The reason you need to change the port number is because we’ll see unintended behavior if everyone uses the same port number at the same time. We’re all running our server on the same machine (mcnulty). That means if we all use the same address, only one person can run the server at a time. If someone else tries to run it, your code will throw an error saying the address is already in use. If I’m running a server, and someone else starts their client, their packets should in theory get delivered to my server. To avoid conflicts, everyone should pick a different port number.

Testing Your Server

To test your server, you will need to open up another terminal window to run the client code. You can open up a second terminal in VSCode, or you can open a separate window (just make sure you’re ssh’ed into mcnulty).

You will first start your server by running cargo run from the tcp/server directory.

Then, you will run your client with cargo run in the tcp/client directory.

At this point, the cient code will panic and have a Connection reset by peer error message. This is ok! It happens because the sever doesn’t do anything with the client (yet); it accepts the connection and immediately closes it.

The server code should print out Connection established!.

When you’re done running the server, you can kill the process with Ctrl-C.

Note

The server code is designed so that it never exits (i.e., it’s always on, listening for new clients). When you’re done with your server, or you need to recompile it, you MUST kill it with Ctrl-C. DO NOT LEAVE YOUR SERVER RUNNING AFTER YOU’VE FINISHED.

Reading a Request and Sending a Response

You will next follow the instructions in the Reading the Request section of Chapter 21 of the Rust textbook. Once again, ignore any instructions to use the web browser, as this won’t work with mcnulty. You should only follow the instructions for editing src/main.rs in your server project.

You can test your server in the same way as decribed above. At this point, you’ll see more info about the actual request made by the client.

Read the A Closer Look at an HTTP Request section. You do not need to make any code changes with this section.

Finally, follow the Writing a Response section.

Make sure to test your server again. Your client will print out the time elapsed. Make a note of this number; it will be relevant for Part 3.

At this point, your client and server are talking to each other! Move on to Part 2, to work on a different implementation of a web server.