CSE 291
2018 April 4: Homework 1: Framing, parsing, sending, and receiving

FAQ

Any updates/clarifications will appear here.

  • Clarified that you can modify files provided to you

Overview

In this exercise you are going to get some practice working with protocols and network sockets. In particular, you’re going to learn to frame and parse input to implement a very simple calculator. You are going to write a C or C++ program that reads arithetic commands from a network connection, and sends the appropriate responses back to the client over that same socket. The calculator protocol specified below is intentionally designed to be similar to HTTP/1.1, and so once you complete this assignment you will be in good shape to adapt this code for your project 1 web server.

SimpleCalc specification

Each SimpleCalc instance has an Accumulator, which is simply an integer variable. The accumulator’s initial value is 0. This value can be changed via the following commands:

  • ADD: Adds an integer to the accumulator, and stores the result back in the accumulator
  • SUB: Subtracts an integer from the accumulator, storing the result back in the accumulator
  • SET: Sets the value of the accumulator to the given integer

You only have to support integers (no floating point numbers), and the accumulator’s value, and all arguments to the above three commands, should fit into the uint64_t datatype (you do not need to support values outside this range). You do not need to support overflow or other corner-case/exceptional conditions.

Each command consists of the command name, followed by a space, followed by the argument, followed by a CRLF terminator. Thus, each command is on a line by itself.

A sequence of commands is permitted, and the end of that sequence is signified by a blank line (which is simply a CRLF terminator). The accumulator is reset to 0 after each sequence.

Your program should read and parse commands, keeping a running value in the accumulator, until you encounter the end of the sequence (i.e., a blank line). At that point, your program should send the value of the accumulator back over to the client. Your program should keep reading in sequences until the client closes the connection.

SimpleCalc Examples

In the following examples, EOF indicates that the client has closed the connection. Although not shown, there is an implicit CRLF after each command.

Example 1

Given input:

ADD 3
SUB 4
ADD 10
<CRLF>
<EOF>

SimpleCalc should send this to the client:

9

Example 2

Given input:

SET 3
ADD 3
ADD 2
<CRLF>
ADD 10
SUB 3
<CRLF>
<EOF>

SimpleCalc should send this to the client:

8
7

Notes about line feeds

By default, Mac and Unix computers (e.g., Linux) end a line with a LF line feed character. Windows/DOS traditionally ended lines with a CR then a LF. If you want to create your own input files, and you do so on the lab computers or a UNIX-based system (including Macs), you’ll need to make sure that each line ends in the CRLF delimiter. There is a tool called unix2dos.sh inside of the tools/ subdirectory that will do just this for you. There is also a tool in that same directory that will print out information about line endings for files you give it, in case you want to verify that any sample files you create are of the right format.

For the example files that I’ve provided to you, I’ve already ensured that each line ends in a CRLF pair. If you want to create your own files to test with, you will need to run unix2dos.sh to ensure that each line ends in CRLF.

The SimpleCalc server

Your server will open a listening socket and wait for an incoming request. When a request arrives, the client will issue a SimpleCalc program, and then close the connection. When the server is finished handling this incoming command, it should close the server socket and waits for the next incoming connection.

You do not have to implement a concurrent/parallel server–it is OK to simply handle one incoming client at a time. For this homework, we will only send you correctly formatted SimpleCalc programs–we won’t test corner cases or invalid input.

Client and server programs

The starter code includes a basic build environment for building the server.

The server

Usage: ./server server-port
  • server-port: the port your server should listen to

An example:

$ ./server 8080

Testing your server

You will need a client to send data to your server to test it. You can use the ‘nc’ tool, which is bundled in most versions of Unix/Mac. ‘nc’ takes the hostname and port number to connect to, and can accept as input data provided to it via ‘stdin’.

An example:

$ nc localhost 5000 < tests/test1.txt
10
$ nc localhost 5000 < tests/test2.txt
3
$

The first example connects to the ‘localhost’ host on port ‘5000’ and sends the contents of the file tests/test1.txt to the server, and prints the results.

Starter code

To get the starter code, accept the GitHub invitation at this URL: https://classroom.github.com/g/vq26KN5z

Submission

You can modify and/or add to the files in the starter code as long as we can type “make” on the ieng6 servers and have it build correctly. You can use C or C++ (up to and including version C++11). You are free to use man pages, standard libraries installed on the ieng6 servers, any code we talk about in class, and any code from the assigned readings/texts. For anything else, please ask first!

Make sure to modify the README.txt file to add your name and student ID.

To submit your code, visit https://GradeScope.com and submit to Homework 1. The GradeScope entry code is 9YVY8Y

Grading

The GradeScope autograder will send a variety of test files to your server, and compare the results to see if they are correct. Points will be assigned one per test case.

Checklist

  • Did you update README.txt to add your username + PID?
  • Did you submit your code to GradeScope.com?