EE 607 Spring 2006 Project 1
Due Feb. 6 (mon), 10 points
For this assignment you will write (actually modify) C code for a
client
and server.
The server will serve files. It should be located in one of your UNIX
accounts and in its own directory. In the directory, you should also
have the following text files (these can be downloaded by clicking
them):
- file1.txt
- file2.txt
- file3.txt
The client will get a file from the server and print it out on the
terminal. If the file is not found then the client prints out a
corresponding message. The following is an example of using a client,
assuming its executable
has been renamed "getfile". Shown in red are
what
was typed in, and shown in green are the
responses from the computer.
getfile file1.txt
Aloha!
You have reached file 1.
This means that your client and server works at least partially.
Congratulations!!
Adios
getfile whoa
File "whoa" not found
Instruction
- Step 1. Read
Beej's Guide to
Network Programming
- Step 2. Modify the Simple Stream Server and Simple
Stream Client into your server and client. To test, you can use ephemeral
ports between 2000 and 5000.
- Step 3. Email me your client by the due date 11:59pm.
You should include your source code, makefile, and a README file
(text), that describes how to operate your client. Your code
should be compilable on spectra.
- Step 4. After you turn your client in, I will give you
instructions to run your server on spectra at a particular port number.
Run it for a couple of days in background (e.g., type "a.out &").
Afterwards, you
can kill the job by first finding its id using ps, and then killing it
using
"kill". For more information, see the man pages of your machine.
Hints:
- Download the client.c and server.c files from Beej's Guide.
Make
sure you can compile this on specta. I was able to compile using
(note
that this was suggested in Beej's Guide under Notes for Solaris/SunOS,
but
there the example was for "cc" rather than "gcc"):
gcc client.c
-lnsl
-lsocket -lresolv
- Examine client.c and server.c. Beej's Guide explains what's
there
but basically there's two parts. First, a connection is set up
using
"connect" and "accept" . Second data is transferred using "recv"
and
"send". Note that both "recv" and "send" can be used at client
and
server to transfer strings of bytes. In the examples of client.c
and
server.c, file descripters "sockfd" and "new_fd" are used.
- For client.c, the host name of the server is given as an
argument,
argv[1].
- Many of the modifications will be using functions that do
character
string operations and opening, closing, reading, and writing to files.
If you're rusty with C then check these sites: C Programming Tutorial,
Tutorial
From Physics Dept Drexel U., Eddie's Basic Guide to
C
Programming. The last site is pretty good since it has
examples.
This site has a
bunch
of links about C. For practice, write a short program that will
have
as arguments two file names and it copies the first file to the second
file.
Before copying, your program should check if the first file is
there
and the second file does not already exists, since you want to avoid
overwriting
an existing file. A string function that is useful is "strlen"
which
returns the length of a string. For IO, some of the key
operations
are fopen, fclose, fscanf, fread, getchar, and printf.
- Note that to run an executable, say "a.out", on spectra, the
command
line is "./a.out"
- Write your code in stages, starting from the original client.c
and
server.c code. Have the client send a file name to the server.
Then
have the server return the file name it received back to the client.
Then
have the server open a file and send it to the client.