In this lab, you will use select()
to complete a simple
version of nc
in client mode!
The program should take 2 command line arguments: dot-notation IP address and port to connect to. The starter code babync.c has already done this and opened a connection.
Then it should use a select()
loop to perform:
Whenever stdin has data, write it to the socket.
(To keep this lab short, we assume that sending to the socket is successful, complete, and does not block. Ideally, we should toggle between two states: monitor stdin for reading but don’t write to the socket; monitor the socket for writing but don’t monitor stdin until the writing is complete.)
Whenever the socket has data, write it to stdout.
(To keep this lab short, we assume that writing to stdout is successful, complete, and does not block. Similar to the above.)
Do not exit until both stdin and the socket are EOF when reading.
Do not monitor an FD for reading if it reached EOF in the past, lest you would cause busy-polling. (Why?)
Please consult the Testing Tips section in Assignment 4 for testing tips.
You should also use redirection to test for data corruption. Example:
Server side:
nc -v -n -q 1 -l PORT < IN1 > OUT2
Client side:
./a.out DOTADDRESS PORT < IN2 > OUT1
Then IN1 and OUT1 should be identical, IN2 and OUT2 should be identical.
If you like to print debugging or error messages for your own sake, please send them to stderr only.
The code file babync.c.