Re: How can I detect a carriage return using java.net
- From: Martin Gregorie <martin@xxxxxxxxxxxxxxxxxxx>
- Date: Sat, 21 Oct 2006 22:32:48 +0100
Gordon Beaton wrote:
If you don't need to keep the connection open, then the server canWith all due respect, I think that the connection should always be opened and closed by the client: if the server closes the connection you have no way of telling if the network broke, the server crashed or if it was an intentional "end of dialog" closure.
just close it and readLine() will return null, indicating EOF.
the server should send information the client can use to determine whereA better way is to precede each message with its length, which should be either a binary byte or (better) a fixed length character string, e.g.,
the end of the response is. One way is to precede the response with
the number of lines to expect so the client can count them. Another is
to send an extra, empty line ("") after the response (assuming that
can't occur within the response itself), or to mark the final line of
the response differently from the others in some way.
0012Message data
This way the receiver starts with a fixed length read to get the message data length, translates it into an int and then reads that many bytes to get the message. If either read gets the wrong number of bytes you know there's a problem and you can take corrective action.
If both client and server send this type of message both can use the same error checking and message decoding code with the addition that, if the server receives a zero length message when its expecting a message length it knows that the client has closed the connection.
I usually build messages this way, but assemble them from comma separated fields. If the client always sends messages consisting of a command and an associated value a message might look like:
0024,STORE,Text to be stored
While the reply from the server might look like
0004,OK,
or
0023,ERROR,The file is full
This format has the double advantage that its easy to decode and is human-readable when it appears in debugging messages, etc.
--
martin@ | Martin Gregorie
gregorie. | Essex, UK
org |
.
- Follow-Ups:
- Re: How can I detect a carriage return using java.net
- From: Gordon Beaton
- Re: How can I detect a carriage return using java.net
- From: EJP
- Re: How can I detect a carriage return using java.net
- References:
- How can I detect a carriage return using java.net
- From: Angus
- Re: How can I detect a carriage return using java.net
- From: Gordon Beaton
- Re: How can I detect a carriage return using java.net
- From: Angus
- Re: How can I detect a carriage return using java.net
- From: Gordon Beaton
- How can I detect a carriage return using java.net
- Prev by Date: Re: Signed Applets, Certificate Authorities
- Next by Date: Re: Method naming convention required
- Previous by thread: Re: How can I detect a carriage return using java.net
- Next by thread: Re: How can I detect a carriage return using java.net
- Index(es):
Relevant Pages
|