Example client/server code. Do not represent this
code as good template code for industrial-strength client/server
programs. This code is pedagogically oriented, and thus I have
made some dramatic simplifications that you would not want to make if you
were developing software for long-term use. I'll talk about these
simplifications in class.
- tcpUtilities.c, a few of my own
utility functions for very simple client/server examples
- tcpUtilities.h, the associated
header file
- helloClient.c, a simple client
that connects to a server, reads a null-terminated string from
the connection, prints the results, and quits
- helloServer.c, an iterative server that
sends a null-terminated string whenever a client connects, and then
immediately closes the connection
- helloConcurrent.c, a concurrent
version of helloServer.c
- hello.h , a header file for the
previous three files
Test this code as follows. First, go into hello.h and change
HOST_NAME to the name of whatever machine you're working on.
Then compile two servers and a client.
- cc -o helloClient helloClient.c tcpUtilities.c
- cc -o iterativeHelloServer helloServer.c tcpUtilities.c
- cc -o concurrentHelloServer helloConcurrent.c tcpUtilities.c
Next, open two terminal windows. In one, start
iterativeHelloServer running. Then, run helloClient in the other window. The
server should report the connection in the server window, and
the client should report the message it received from the server in the
client window. The client will quit on its own, but the server
will keep running. You can shut down the server with a CTRL-C in
the server window.
You can play the same game again with
concurrentHelloServer and helloClient.
But even after the iterativeHelloServer is no longer running,
the port it had been listening to will be unavailable for a few
minutes. So if concurrentHelloServer tells you
"can't bind to 5554 port: Address already in use,"
just keep trying. Eventually the port will become available.