Out: Wednesday November 17th
Due: Monday November 22th by 10 am pacific
Write a C++ program that accepts three command-line arguments in the following order:
The program should connect (via TCP) to the server on the supplied hostname and port. Once connected, the program should read the bytes from the local file, and it should write those bytes over the TCP connection. Once all of the bytes have been written, the program should close the TCP connection and exit.
To get you started on this, we have provided you with the following four files. To download the files, go to the link here. The first two are already implemented for you and the last two are skeleton files:
Makefile
: compiles the code into an executable called ex14
.
SocketUtil.h
: declares various utility functions to achieve the functionality
described above.
SocketUtil.cc
: contains empty implementations of the utility functions declared in the
header file.
ex14.cc
: contains an empty main function.
$ ./ex14 localhost 5555 test.txt
Note: the values of your command line arguments may vary depending on how you setup your testing.
Code Adaption: 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 arguements, you should make sure your code handles various inputs from the user, which may be in an unexpected format. Note that the three inputs you get from the user in this exercise each has a different expected format.
Read/Write: You should use POSIX read()
to read in the local file and POSIX
write()
to write over the socket to the server.
Error Handling & Robustness: When you are using POSIX functions,
you should make sure that you handle possible errors correctly by retrying in the case of
recoverable errors (EAGAIN
and EINTR
) and returning appropriate status in the case
of a
non-recoverable error. If you encounter a fatal error in main, you can simply exit the program gracefully.
Be sure to clean up resources accordingly in all cases.
Server Setup: To test your program,
you will need to set up a server to receive the data your ex14
executable will send.
One way to do this is using the nc
program. For example:
$ nc -l 5555 > output.bytes
will run a netcat listener on port 5555 and redirect the incoming bytes to
the file output.bytes
(i.e., write if it didn’t exist, 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 its own host, so if you are running on attu
you can
only reliably use 127.0.0.1
or localhost
if both client and server are running on the same one. And as a reminder, you can
choose to log into a specific attu
machine by doing
$ ssh <your_netid>@attu#.cs.washington.edu
where #
should be replaced by a number between 1 to 8.
Restarting: Note that nc
will exit once it has processed a single connection,
so you'll need to rerun nc
each time you test your client.
Don’t forget that good practices from previous exercises still apply!
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 vs. 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 when possible (e.g., use cout
instead of
printf
, use C++ casting, etc.).
Files to submit:
SocketUtil.cc
ex14.cc
Your code must:
attu
, or CSE
home VM)Makefile
should compile your
code without errors or warnings.
SocketUtil.cc
, ex14.cc
with your name,
student number, and CSE or UW email address. These are the only two files you need to submit for this
exercise.cpplint.py
) shouldn’t have any
complaints about your code.