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.
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:
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
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.SocketUtil.cc and
ex10.cc so you should not modify the other
source files.
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.
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.
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.
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.
nc will exit once it has processed a single
connection, so you'll need to rerun nc each time you
test your client.
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.
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.
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).
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.ccex10/SocketUtil.ccOther 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:
attu, or CSE home VM).g++ and valgrind)..cc files with
your name(s) and CSE or UW email address(es).cpplint.py).