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 2. UDP Sockets (10 points)

In this part, you will reimplement your server using UDP (User Datagram Protocol) sockets instead of TCP. As with Part 1, the client code (to test your server) is provided for you. Note that the client will again print out time elapsed, which is relevant to Part 3.

Your task

In your assignment repo, cd into the udp directory. Create a new cargo project called server with cargo new server. cd into the server directory. This is where you will implement your UDP web server.

You will have 3 main tasks to do with your server:

  1. Create (and bind to) a UDP socket.
  2. Receive a message from that UDP socket.
  3. Send a message back to the client through the UDP socket.

To do this, you will follow the examples in the Rust documentation for UDP sockets. Pay particularly close attention to the first example; you can (and should) copy code directly from the documentation.

Bug

The example code uses the address "127.0.0.1:34254". This address will not work with the provided client code. The client expects the server to be running on port 7878, so you should use the address "127.0.0.1:7878".

Make sure your server prints out the request it receives from the client. You can copy this line from your TCP server code: println!("Request:\n{request}");.

You should also send the same response to the client that you do in the TCP example: let response = "HTTP/1.1 200 OK\r\n\r\n";.

Bug

The example in the documentation uses an array of size 10 to receive the incoming message. This will likely be too small. I used 1024 in my implementation.

Tip

When you receive from a UDP socket, it stores the received message as an array of bytes. You will want to convert this to a String to print it out. One way you can do this is with String::from_utf8_lossy. This method will take as input an array of bytes, and convert it to valid UTF-8 string. If there are any invalid UTF-8 characters, this method will replace them with a replacement character. You can find more details in the documentation.

Tip

Similarly, when you want to send a message through a socket, you will need to convert the String (or &str) to bytes. You will have done this in Part 1, and you can do the same thing here.

Testing Your Server

You will test your server in the same way you did for Part 1. You will need to have 2 terminals open (you can open multiple terminals in VSCode, or you can open up separate terminals). Make sure you’re ssh’ed into mcnulty on both.

In the first terminal, you will start the server by running cargo run from the udp/server directory.

In the other terminal, you will run the client by doing cargo run from the udp/client directory.

Note

You can only run one server at a time. This means that you won’t be able to start your UDP server if your TCP server is still running (you’ll likely get an error about the port already being in use). Make sure to close your server when you’re done. You can do this by typing Ctrl-C in the terminal in which the server is running.

If your server is working correctly, it should print out the request it receives from the client. The client will print out the response it gets from your server (which should be HTTP/1.1 200 OK).

Make note of the value the client prints out for time elapsed; you will need it for the next part.