Application layer protocols allow different processes to communicate with one another in an orderly and meaningful fashion. By agreeing on a pre-arranged set of messages (the protocol) the two processes can communicate to achieve every internet-enabled application you can think of. Internet hostnames are translated to IP addresses with the DNS protocol. Web browsers recieve web pages with the HTTP protocol. Servers provide secure terminal access through the SSH protocol, and etcetera.
In this studio, you will:
Please complete the required exercises below, as well as any optional enrichment exercises that you wish to complete.
As you work through these exercises, please record your answers, and when finished submit your work via the git repository.
Make sure that the name of each person who worked on these exercises is listed in the first answer, and make sure you number each of your responses so it is easy to match your responses with each exercise.
Modify your server so that it interprets incoming messages (other than
"quit") as file names. It should attempt to open these files with the
open()
or fopen()
functions. Check the return value:
if you cannot open the file then return an error message to the client by
writing to the socket (the one returned by accept()
in the
server). Otherwise, write the contents of the file to the socket. Once
the server is done writing (either the error message or the file contents)
you should terminate the server's end of the connection by calling
the function shutdown()
with option SHUT_RDWR
(see man 2 shutdown
for details).
Similarly, modify your client so that after sending its request it will
read()
from the socket, printing everything it recieves to
standard output. Once the call to read()
returns zero (signifying
there is nothing more to read from the socket) it should terminate.
Hint: We had several studios early on (two and four) that involved reading and reprinting text files through streams. Feel free to use this code again.
Now, add one more special context word along with "quit". If the client
sends the string "ls" then the server should execute the command ls
and send the result back to the client. I would suggest you do this with the
popen()
function documented at man 3 popen
. Note that
this function returns a FILE*
type, so you will need to use
fgets()
to read from the returned file pointer.