This is one of the less difficult experiments. I started
with echoclient.c and echoserver.c and made the obvious
modifications. There was really only one wrinkle. When I think
of writing a file server, I think in terms of reading the file
in seqments of some fixed number of bytes. That will not work
in this case due to the simple fact that the function
recvln
does exactly what you might expect from
its name: it reads a line of text which it assumes is
terminated by a \n
(012
). So you
need to be careful to use something like fgets
.
You also need to be careful in computing the number of bytes
you send as that newline character must be included.
The reason you need to send a newline is that you are dealing
with a stream when you use TCP. It has
only the structure that you give it. This idea comes up
in an important way in the next chapter.
The task of file transfer comes up in later experiments, 6.2, 8.3,
and 8.4. You want to do this well as the experience and the code
will be very useful later on.
The optional extensions are of interest and are not too
terribly difficult except for number 13. At first blush,
it would seem to be easy enough to add encryption and
decryption, and it is if you make a lot of assumptions that
you should not make. My solution involved a new directory
just for this extension and some rewriting of the recvln
function in readln.c
. Why? Because recvln
is looking for a new line character, and when it finds it, it will
return. But what is to prevent a character internal to the message
from being encrypted as a new line? Absolutely nothing.
I rewrote the recvln
function, and added a function
called mysend
with the same parameters as send
.
mysend
first sends one byte with the number of encrypted
characters that follow. recvln
receives one byte and
uses the integer equivalent in a for-loop to get exactly the right
number of characters.
There are many possibilities for encryption. I used a secret string
shared by the client and server. Each byte of the message was
xored
with the corresponding byte of the plaintext
to get the enciphered text. To decode you simply do the same
thing since if A = B xor C, then B = A xor C.
This site is maintained by W. David Laverell
of the Computer Science Department
at Calvin College. For assistance or corrections,
please contact him at .