Marking the end of an OutputStream

B

Bob

Hi guys,

I would like to implement a simple http-like client: the client should
send a request, wait for reply from the server and then terminate the
communication.

I am trying to implement this using a PrintSream implementation of an
outputstream.

Here's a simple version of the client code:

----------- C L I E N T -------------
1. String message = buildMessage();
2.
3. printStream.write(message.getBytes());
4. printStream.flush();
5.
6. waitForReply();
7. closeSocket();
-----------------------------------------

on the other hand the server does something like (very simplified)

------------ S E R V E R --------
1. while(r!=1){
2. r = inputStream.read(...);
3. ...
4. }
5. sendReply();
-----------------------------------

My problem is that line "5" of the server is never executed, which
means that the condition "the end of the stream has been reached" is
never met (according to the read method's apidocs).

My question is how to make it explicit (or even implicit, I don't mind)
on the client-site that the end of the request has been reached.

i.e. is there anything like:

printStream.this_Is_The_End_Of_The_Stream(); (?)

or do I need to explicitly send an EOF character?

Thanks, and sorry for the long message,

Bob
 
G

Gordon Beaton

I would like to implement a simple http-like client: the client should
send a request, wait for reply from the server and then terminate the
communication.
[...]

My problem is that line "5" of the server is never executed, which
means that the condition "the end of the stream has been reached" is
never met (according to the read method's apidocs).

My question is how to make it explicit (or even implicit, I don't
mind) on the client-site that the end of the request has been
reached.

i.e. is there anything like:

printStream.this_Is_The_End_Of_The_Stream(); (?)

or do I need to explicitly send an EOF character?

There is no EOF character. EOF is a *state* that the stream enters
when the end of the stream has been reached. Different methods
indicate EOF to their callers in different ways (e.g. by returning -1
or NULL or something along those lines).

The reader reaches the end of the stream when there is no more data to
read *and* the sender has closed his end. Once your stream is at EOF
you can no longer read data from it. So if you use EOF to end the
request, you can't continue to use the (same) stream after that.

If you want to continue using the connection, you need a different way
to tell the server that you have sent the entire request. That's part
of defining your protocol.

There are various alternatives (and this list is not complete):

- send the request on a single line.

- for requests longer than one line, use a special continuation
character at the start of additional lines, similar to FTP.

- send only fixed length requests. Server and client agree beforehand
about the length of a valid request.

- preceed every request with its length. Server reads length, knows
how much more to read.

- send the request on multiple lines, followed by a special
end-of-request token. HTTP does this by sending an empty line after
the request (i.e. request ends with \r\n\r\n).

/gordon
 
H

HK

Bob said:
------------ S E R V E R --------
1. while(r!=1){
2. r = inputStream.read(...);
3. ...
4. }
5. sendReply();
-----------------------------------

My problem is that line "5" of the server is never executed, which
means that the condition "the end of the stream has been reached" is
never met (according to the read method's apidocs).

If no socket were involved, you would just close
the PrintStream on the client side and the server
would realize that no more data can be expected.

With sockets, however, it is not possible to close
the OutputStream without screwing up (read closing)
the whole Socket. Instead you have to use
Socket.shutdownOutput()

http://java.sun.com/j2se/1.4.2/docs/api/java/net/Socket.html#shutdownOutput()

This keeps the socket alive but signals EOF to the server.


Harald.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top