Alert! A problem has emerged with the experiments in this
chapter! Many thanks to Professor Cozart of Mercer University
for bringing it to my attention.
It is a UNIX problem only (so far)!
If you are using a 64-bit computer with an up to date
compiler, echoserver will always terminate.
I have let Professor Comer know about this, but for the
time being here is a quick fix.
In the directory h there is a file named cnaipi.h.
On line 26 of this file there is a line that says typedef
long computer. Just change long to int,
and and the echo and chat software will work fine.
You might want to change long to int on line
43 of cname_to_comp.c in the api directory to avoid
some warnings. Many thanks to Dr. Cozart!
It is a good idea to think ahead on this one.
Your approach should be influenced by whether you plan
to do Optional Extensions 8 and 9. If so, you will be
well advised to see the remarks below and just use
the curses library from the beginning.
Otherwise begin with the chat server and client obtained
and compiled as part of Experiment 5.1.
After testing the programs you move on to the first
modification, adding a name for each. (Tip: when testing
do not use Harry and Sally, use Client and Server. It makes
things a lot easier, especially if you have short-term memory
problems.) I used a printf
followed by
a gets
. If you just add those two lines along
with a variable for the name, you will get a compiler warning.
You should do that if you are somewhat new to C even though
I am about to tell you how to avoid it. Hint: in the absence
of any indication as to the type returned by a function, what
does the C compiler assume? Now the fix: just add
string.h
to the files you are including. Why did
that fix the problem?
In order to prepend the name to the message you need to use
strcat
. This is harder to do in C than in C++,
say, because you need to use a strcpy
followed
by two strcat
functions.
This small step has an important implication for testing.
Depending on where you put this code you may encounter the
following oddity: you fire up your server and while it is
still waiting for the name, you fire up your client which
immediately terminates. Why?
Optional Extension Number 7 is very interesting! It
requires slightly different behavior when sending and
receiving depending on whether it is the first time through.
Straightforward, but my first effort met a fascinating
problem. The client sent data and then, somehow, received
data before the server had a chance to respond. Then the
two got out of sync, each being in input mode. For my own
sanity in debugging I cleared buffers right after each use.
Voila! The problem was gone. Apparently, the function
readln
did not provide the NULL
termination. When I later used strlen
to
determine how many bytes to send, I was way over and
somehow overwrote my connection
variable.
Optional Extension Number 8 seems at first blush to be
easy, almost trivial. It is not! You would probably first try
to use getchar
in a loop. This will not do what
you want because input is buffered. No information will come to
your program until you hit a return character.
There are ways to do Number 8 that leave significant problems
for Number 9, but there is also a way to do both extensions together.
Use the
curses library. Fortunately, there is a very good
online
tutorial available. Optional Extension Number 10 is a
good introduction to the
select
with timeout
function.
This is a very interesting
and rewarding exercise. I personally found a great deal of satisfaction
in producing
a chat program that acutally resembles some that I have used in the past,
and think how much better you are prepared for its challenges than
I was!
This site is maintained by W. David Laverell
of the Computer Science Department
at Calvin College. For assistance or corrections,
please contact him at .