
    How To Denote The "end of data"

When a client program is sending data to a server, and the
client has reached the end of its data and wants to wait for
a response from the server, the client needs a way to tell
the server that there is no more data and that the server
should switch from reading data from the client to writing
data to the client.

The three client/server pairs,

   AdditionClient_v1.java   AdditionServer_v1.java
   AdditionClient_v2.java   AdditionServer_v2.java
   AdditionClient_v3.java   AdditionServer_v3.java

show three ways in which a client/server pair can
coordinate the exchange of their read/write roles,
   1.) using end-of-stream,
   2.) using a count,
   3.) using a sentinel value.

The first pair uses end-of-stream to denote that the
client is done writing and so the server should stop
reading. Since the client uses end-of-stream to denote
it is done writing, the client can only send one request
to the server.

In the second pair, the client uses a count value to
let the server know how many reads the server must
perform before the server switches over to writing.
Since the client has not closed its output, the client
can send any number of requests to the server. The
client uses end-of-stream to denote the end of its
requests to the server.

In the third pair, the client uses a negative number
to let the server know that the server should switch
over to writing. Since the client is using a negative
number as a sentinel, it cannot use negative numbers as
data values. Since the client has not closed its output,
the client can send any number of requests to the server.
The client uses end-of-stream to denote the end of its
requests to the server.


In all three client/server pairs, the client uses
end-of-stream to denote the end of the client's requests
to the server. But is also possible for the client to use
a sentinel or a counter to let the server know when the
client no longer has more requests to make. It is left as
an exercise to write client/server pairs that use either a
sentinel or a counter to denote the end of client requests.


NOTE: In these examples, only the client is sending
a variable amount of information (the server always
sends back a single number). It could be the case
that the client always sends a fixed amount of
information but the server sends a variable amount.
And it could also be the case that both the client
and the server always send a variable amount of
information in each exchange. Any one of the above
three techniques could be use by either the client
or the server to denote the "end of data" in an
exchange of information.


NOTE: These three techniques for exchanging read/write
roles are used extensively in the HTTP protocol for
web servers and clients. We will see several different
sentinel values and a variety of counters being used
by the HTTP protocol.


NOTE: The client/server pairs in this folder all
use text data to send integer numbers. They could
be rewritten to use a binary integer data format
for sending integers back and forth. It is left
as an exercise to rewrite one of the client/server
pairs to use binary streams and a binary data
format.
