CSE333 Exercise 10

Out:   Wednesday, May 20
Due:   Wednesday, May 27 by 11 am PST
Rating:   3 (note)
CSE 333 Exercise Rating Scale

Each exercise this quarter is rated on a integer scale of 1 – 5, inclusive, with 1 being the "least time-consuming" and 5 being the "most time-consuming".

This difficulty scale is meant as a rough guide for you in predicting the amount of time to set aside for each exercise as you balance the work required for 333 with your other obligations. However, it is necessarily imperfect as everyone's set of circumstances and experiences with the exercises differ. If your experience with an exercise does not align with its rating, that is not a reflection of you or your abilities.

Goals

  • Adapt client-side networking code (TCP) in C/C++.
  • Use the utility netcat (nc) to interact with clients over the network.

Problem Description

Write a C++ program that connects (via TCP) to a server specified by a user-supplied hostname and port number, sends the bytes of a specified local file to the server, and then closes the connection and exits. Your program should accept the command-line arguments in the following order:

  1. The hostname of the server
  2. The port number of the server
  3. The name of a local file

Because you must use the POSIX write() to write bytes to the server, we also want you to use the POSIX read() to read in the local file.

An example execution of the completed application is:

$ ./ex10 localhost 5555 test.txt

Provided Files

We have provided you with the following four source files, which can be downloaded from or with the commands:

$ wget https://courses.cs.washington.edu/courses/cse333/26sp/exercises/ex10_files/<filename>
  • SocketUtil.h — Provides the public interface for various client-side networking utility functions. DO NOT MODIFY.
  • SocketUtil.cc — Contains empty implementations of the utility functions declared in the header file.
  • ex10.cc — Contains an empty main function for the client-side networking program.
  • Makefile — Provided for your convenience in compiling the executable ex10.

Implementation Notes

Code Adaptation

Feel free to adapt sample code from lecture and section as part of your solution if it helps, but be sure you understand what your code does when you're done.

User Input

Since you will be reading in user input via command-line arguments, you should make sure your code handles various inputs from the user, which may be in an unexpected format. Note that each of the three inputs in this exercise has a different expected format.

Error Handling & Robustness

When you are using POSIX functions, you should handle errors by retrying in the case of recoverable errors (EAGAIN and EINTR) and returning an error status in the case of a non-recoverable error. Make sure that you clean up system resources in all possible cases.


Testing Notes

Server Setup

To test your program, you will need to setup a server to receive the data that your ex10 executable will send. The recommended way to do this is using the nc utility:

$ nc -l <port> > output.bytes

This command will run a netcat listener (-l) on port <port> (e.g., 5555), which needs to match the port you provide to the ex10 executable, and redirect any received bytes to the file output.bytes. Note that this will create the file if it didn't exist or overwrite if it does exist.

Local Testing

If you are running the server on the same computer/host that you are testing your code on, you can use the special loop-back IP address 127.0.0.1 or localhost to refer to the same host. Please note that each attu (e.g., attu1, attu2) counts as a separate host, so if you are testing on attu, both client and server must be running on the same one for this to work. To log into a specific attu machine, run:

$ ssh <netid>@attu<#>.cs.washington.edu

where <#> should be replaced by a number 1 – 8.

Style Focus

General

For the sake of our autograder, you may not modify SocketUtil.h, which also means that you should not modify the function signatures in SocketUtil.cc.

C/C++ Idioms

To work with the POSIX networking API, we, unfortunately, have to mix C and C++ idioms in our code. But you should still try to use C++ idioms whenever possible (e.g., use cout instead of printf, use C++ casting).

Submission

Submit the following file(s) by creating an ex10-submit tag in your exercise repo before the assignment deadline. The file(s) should be located in the exact directory listed below (there should be a folder titled ex10 with ex10.cc and SocketUtil.cc within that folder), including capitalization:

  • ex10/ex10.cc
  • ex10/SocketUtil.cc

Other files in the ex10 folder will be ignored, so you may keep the files unnecessary for submission in there!

For full credit, your code must:

  • Compile without errors or warnings on CSE Linux machines (lab workstations, attu, or CSE home VM).
  • Have no runtime errors, memory leaks, or memory errors (g++ and valgrind).
  • Be contained in the files listed above and compile with the provided Makefile.
  • Have a comment at the top of your .cc files with your name(s) and CSE or UW email address(es).
  • Be pretty: the formatting, modularization, variable and function names, commenting, and so on should be consistent with class style guidelines. Additionally, the linter shouldn't have any complaints about your code (cpplint.py).
  • Be robust: your code should deal with hard-to-handle/edge cases and bogus user input (if there are any) gracefully.