Re: How can I detect a carriage return using java.net



Gordon Beaton wrote:
If you don't need to keep the connection open, then the server can
just close it and readLine() will return null, indicating EOF.

With 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.

the server should send information the client can use to determine where
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.

A 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.,

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 |
.



Relevant Pages

  • Re: .Net Scalability problem
    ... LoadRunner will peak out a server with a few virtual users. ... To get an idea of load, ... Fire off the test client and watch the number of ... > So I think that the MTC generate concurrent connection and per ...
    (microsoft.public.dotnet.framework.adonet)
  • Re: Connection lost at same time every hour (sometimes)
    ... After making the two following alterations on the server the problem seems ... After analyze your ipconfig on SBS and client, ... Then, other connection is good, ...
    (microsoft.public.windows.server.sbs)
  • Re: server disconnection - very often
    ... Reason of permanent popups is VMware server aplication on clients. ... Run CEICW to configure the network of SBS: ... Two network adapters - manual router connection to broadband ... Uninstall VMware on client. ...
    (microsoft.public.windows.server.sbs)
  • Re: Lan setup 2 nic
    ... The external nic only has TCP/IP enabled. ... Ipconfig of the server is looking good, but the client is still missing the ... > connection so we have a 2 nic with router setup now. ...
    (microsoft.public.windows.server.sbs)
  • Re: Regular disconnections from remote web workplace
    ... I can connect to office server and all office clients from home at all times ... be physically working right up until the connection is lost. ... If I enter http://companyip from a client I receive the login screen for the ... Click Services tab and select Hide All Microsoft Services and Disable ...
    (microsoft.public.windows.server.sbs)