Any updates/clarifications will appear here.
- Clarified that you can modify files provided to you
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.
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
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
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.
In the following examples, EOF indicates that the client has closed the
connection. Although not shown, there is an implicit
CRLF after each
ADD 3 SUB 4 ADD 10 <CRLF> <EOF>
SimpleCalc should send this to the client:
SET 3 ADD 3 ADD 2 <CRLF> ADD 10 SUB 3 <CRLF> <EOF>
SimpleCalc should send this to the client:
Notes about line feeds
By default, Mac and Unix computers (e.g., Linux) end a line with a
feed character. Windows/DOS traditionally ended lines with a
CR then a
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
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
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.
Usage: ./server server-port
- server-port: the port your server should listen to
$ ./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’.
$ 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.
To get the starter code, accept the GitHub invitation at this URL: https://classroom.github.com/g/vq26KN5z
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
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.
- Did you update README.txt to add your username + PID?
- Did you submit your code to GradeScope.com?