In this lab assignment you will write a simple network client. Your client will send text specified on the command line to a remote server, and output the text received back from the server.
You'll be writing this simple client to learn about how to use the socket abstraction and its associated system calls. These calls are useful in order to build networking applications. Your later labs will build heavily upon these few system calls, so it is important that you feel very comfortable with their use.
In this lab, we use client to mean an application program that establishes connections for the purpose of sending data. You use such programs in everyday life, e.g., your web browser. We use server to mean an application program that accepts connections in order to service requests by sending back responses, e.g., the Apache web server.
For this lab, all network communication will occur over TCP -- the transmission control protocol. We will learn about TCP later in the course; from the application writer's point-of-view, once a TCP connection is established between a client and server, if one party writes a stream of data into its socket, the other party reliably receives the same stream of data out from its socket.
For more information about sockets and the various system calls we will use throughout the lab, please see the IPC Tutorial, as well as following man pages:
% man tcp % man connect % man send % man recv
Your client will create a blocking TCP socket, connect to a specified server address, and send text as specified on the command line. The client will read the data returned from the server, and output it to stdout.
By blocking, we mean that your client does not need to handle asynchronous events -- i.e., after you write to the server, you can just wait until you receive data back from the server. Most full-fledged client-server applications handle non-blocking operations -- i.e., the client continually polls whether it can send out data or has received data, and interleaves such accesses.
Your simple client application should be run in the following way:
% ./sc [hostname|ipaddr] [port] [string]
where the first argument can be a hostname (e.g., market.scs.stanford.edu) or an IP address (e.g., 184.108.40.206) on which the server runs, the second argument is the port on which the server listens on its host, and the third argument is the string that the client should send to the server.
Your simple client application should output to stdout any data received back from the server.
To get started, log in to a cluster machine, and copy the starter directory:
% cp -r /usr/class/cs144/src/sc .
You can then start editing the sc.c starter file, which needs to be completed to implement the assignment, and run make to create the sc binary. To add headers and sources to the project, edit the SOURCES and HEADERS lines in the Makefile (e.g., SOURCES = sc.c foo.c bar.c).
Some good general coding guidelines can be found on the CS244A coding guidelines page, including some strategies for dealing with handling errors.
Note that since you may have to write a binary response to stdout, you will want to write (or fwrite) rather than printf the response to stdout:
void *buf; int len; /* buf = malloc (whatever...) */ /* fill up buf, with len response bytes */ /* write the binary buffer buf to stdout (1 is the file * descriptor for stdout in unix. (Note that this only works * because we have blocking sockets in this assignment. write * may write less than len bytes of buf to file descriptor 1 * if the O_NONBLOCK flag is set.) */ write(1, buf, len);
You might wish to test your client using the following commands.
Try a simple HTTP/0.9 simple request, much like a web browser sends:
% ./sc www.scs.stanford.edu 80 "GET /"
You should receive back an HTML page.
To test whether your client can handle binary responses, try requesting a binary with NULL bytes (many images will have one) from a webserver, and redirecting the output to a file (example: ./sc host port "GET /image.jpg" >image.jpg).
You may find the cmp program useful for comparing the output of your simple client to the original file. See man cmp for details. You may also find the wget command useful to download web objects, and check that sc produces the same output as the files wget downloads.
To submit, run
make submitfrom your project directory and submit the resulting tarball on the submission page.
You must write all the code you hand in for the programming assignments, except for code that we give you as part of the assignment. You are not allowed to look at anyone else's solution (and you're not allowed to look at solutions from previous years). You may discuss the assignments with other students, but you may not look at or copy each others' code.
Here is a histogram for the final grades on this assignment:
Click here to see your grade.