Xizhen Wang wrote:

> Hi! all,
>
> In java, how to determine that an input stream (or a socket) is broken?
> I use a BufferedReader (i.e. in) to read from this stream, and when
> in.readline() == null, I understand the connection is broken. Does that
> make sense on NT and Solaris? Or is there a better way to do that?
>

Null return indicates end-of-file, not connection broken.  If a content length
was specified (request.getContentLength()), you might be better off using a
loop to read that many characters instead -- if you get end-of-file before the
specified count, that means the incoming connection was prematurely
closed (perhaps the user pressed STOP on a file upload).

>
> Is there a sample code or a way to do this?
> 1. Keep the socket connection open.
> 2. read from the input stream whenever there is new data coming in.
> (3. the reading thread is blocked when there is not no new data. or at
> least does not occupy big CPU time for looping)
>
> Can anyone give me a hand? Help is needed urgently.
>

If you are talking about doing this with servlets, you need to review how the
basic servlet life cycle works.  You must handle a complete request, generate
the response, and then return from the doGet() or doPost() method, before any
subsequent request can be processed.  If both the browser and the servlet
engine support HTTP/1.1, and you set the content length in your response, it
is entirely possible that calling close() on the input reader will not
physically close the socket -- but that does not change the rules on request
processing at all.

If you are not talking about servlets, the question is off topic.  You might
do better to look at an introduction to socket-level programming in Java, such
as the network programming trail in the Java Language Tutorial
(http://java.sun.com/docs/books/tutorial).

>
> Thank you!
> Xizhen
>

Craig McClanahan

___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".

Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html

Reply via email to