Due: Monday, August 5th, 2024 by 10:00 am; No late exercises accepted. Goals: Create a program that uses TCP sockets to connect to a server and exchange data with it.
Description: Write a C++ program that accepts three command line arguments:
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 test your program, you can run a server using the nc
program. For example, to run the server on port 5555, and
to have the server redirect the incoming bytes to file
output.bytes
, run the following command:
Note thatnc -l 5555 > output.bytes
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 nc
on the same computer/host
where you are running your ex15
code, you can use the special
loop-back IP address 127.0.0.1
to refer to the
same local host. Please note that each attu
(e.g.
attu1
, attu2
, etc.) is a separate machine
with its own IP addresses,
so if you are running on attu
you can
only reliably use 127.0.0.1
if both client
and server are running on the same actual machine.
There are a few requirements on your code:
read
and write
both to read from the input file
and write over the socket to the server; note this means you
need to pay attention to the possibility that read
/write
might
return EINTR
or EAGAIN
,
and they might read/write less than you ask for.
We recommend writing some utility functions to deal with this.Makefile
so that we can compile your code by
typing make
. Your Makefile
should produce an executable
binary called ex15
and should only recompile and relink files that need to be rebuilt to bring
the program up to date.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.
As usual, your code must:
Makefile
, as described above, that compiles the code
with the g++
options -Wall -g -std=c++17
cpplint
may be helpful in flagging potential style
problems. (You can ignore cpplint
complaints about
using namespace
directives)
You should submit your exercise to the course Gradescope.