Example
of using BSD sockets: "Echo Server"
Design
This program implements a simple client-server
program. The client connects to the server
via TCP and sends it any ASCII text message.
The server replies to the client with the
same message. The client then outputs the
message the server returned.
The message
sent from the client to the server consists
of two parts. The first byte sent contains
the length of the message, not including
itself. The rest of the message is the message
to be repeated back from the server to the
client. The reply message is in the same
format. The length is passed in order to
allow the server to be sure it has read
all available data. Compiling
To compile this program, go to any Solaris
machine. Copy the following files to any
subdirectory in your Unix home directory:
Type "make" in the directory
where the source code is located. Two
binaries should be produced: "client"
and "server". Telnet to another
machine. Run "server" on one
machine and "client" on the
other machine.
Usage
Server: The server is started
with a single command line argument, the
port number. The server will wait on this
port until contact is made by the client.
Once the server replies to the message sent
by the client, it exits.
Example: #server 2000 (note:
the pound sign indicates the Unix prompt)
Client: The client is started with
three command line arguments. These arguments
include the host, port number, and message
to send. The client will then send the given
message to the given host and port number
and wait for a reply. Once the reply is
recieved, the client exits.
Example:#client ece2.ece 2000 "This
is a test of the emergency broadcast system"
Design Tradeoffs
The program is limited to transmitting a
message containing 255 characters. This
could be improved by seperating the message
into smaller chunks. The server will continue
to receive the chunks until all has been
sent. This could be done with minimal modification
of code. All the is needed is some sort
of "all done" message sent between
the client and server.
NOTE: There is a quick
script called "Test" that will
run a simple test case. |