You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Jeremy Joslin <je...@spotlife.com> on 2002/12/02 21:49:18 UTC

RE: Socket GURU'S PLEASE HELP ON OBSCURE PROBLEM [OFFTOPIC]

The condition in your while loop is incorrect.
BufferedReader.readLine() is only going to return null when the end of
the stream is reached, i.e. when the client closes the socket either
fully or partially.  What you really need to test for as a condition for
the end of the input are two consecutive line terminators, either
\r\n\r\n or \n\n.  Since the readLine method only reads up to \r\n or \n
you'll have to add in some additional logic to test for this condition.
Hope this helps you out and please try to post only tomcat related
questions to this list.  Thanks.

Jeremy

> -----Original Message-----
> From: Jason Johnston [mailto:Jason.Johnston@epa.state.il.us]
> Sent: Monday, December 02, 2002 12:27 PM
> To: tomcat-user@jakarta.apache.org
> Subject: RE: Socket GURU'S PLEASE HELP ON OBSCURE PROBLEM
> 
> Thanks Jeremy.
> 
> You're right in that I'm not correctly implementing HTTP, but I'm not
> sure that's my problem.  When I kill the lines that write to the
output
> stream prematurely, I still never exit that loop.  My program just
> simply waits forever.  In fact, the client never times out.  I've left
> it this was for hours with no timeout.
> 
> I've found some info in the forums on readLine() being a blocking
> thread and I think that's my problem though I'm still having trouble
> resolving the issue.  When I issue a timeout, my program errs out one
> the timeout is reached.
> 
> Does anyone know of any way to resolve a readLine() or a read()
> blocking forever?
> 
>  On a side-note, "Lab9" is where I work.  I needed a name :).
> 
> 
> >>> jeremy@spotlife.com 12/02/02 11:48AM >>>
> Looks like you're trying to implement a small webserver but you're not
> obeying the HTTP spec.  You need to read the entire request in from
> the
> client before responding and you should send the appropriate response
> header, e.g. HTTP/1.1 200 OK, or your results will be unpredictable.
> The reason you see it "hang" is because it's waiting for a response
> from
> your server, it finally times out (Connection reset by peer) and
> closes
> the connection.  I would suggest using HTTP 1.0 on the server side to
> avoid dealing with keep-alives sent by 1.1 clients.  For more
> information on the HTTP spec check out RFC 2616.  Good luck on your
> homework.
> 
> Jeremy
> 
> > -----Original Message-----
> > From: Jason Johnston [mailto:Jason.Johnston@epa.state.il.us]
> > Sent: Monday, December 02, 2002 9:04 AM
> > To: <Tomcat Users List
> > Subject: Socket GURU'S PLEASE HELP ON OBSCURE PROBLEM
> >
> > The project that I'm working on is actually much larger and more
> > complex, but I've thrown together this class that illustrates my
> > problem.  I'm basically starting a socket server on port 80 and then
> > connecting with a web browser.  The strange thing is that the
> connection
> > never terminates and I can't identify where it's hung up.
> >
> > The code is as follows:
> >
> > import java.io.*;
> > import java.net.*;
> > import java.util.*;
> >
> > public class Lab9 {
> >    ServerSocket ss;
> >    Socket tempSocket;
> >    BufferedReader instream;
> >    PrintWriter outstream;
> >    Socket connection;
> >
> >    public Lab9(){
> >    try{
> >    System.out.println("Server Started");
> >    ss=new ServerSocket(80);
> >    tempSocket=ss.accept();
> >    instream=new BufferedReader(new
> > InputStreamReader(tempSocket.getInputStream()));
> >    outstream= new PrintWriter(tempSocket.getOutputStream(),true);
> >    ss.close();
> >    String tempString;
> >    System.out.println("Starting to read from client.");
> >    while((tempString=instream.readLine())!=null)
> >    {
> >        //tempString=instream.readLine();
> >        System.out.print("got:");
> >        outstream.print("got:");
> >        System.out.println(tempString);
> >        outstream.println(tempString);
> >     }
> >     System.out.println("Done with Input.");
> >     tempSocket.close();
> >     instream.close();
> >     System.out.println("Server Closed.");
> >    }
> >    catch(IOException e){
> >     System.out.println("Error: "+e.getMessage());
> >    }
> >
> >    }
> >     public static void main(String[] args) {
> >         Lab9 bob=new Lab9();
> >     }
> > }
> >
> > The strange thing is where the output is concverned.  The browser
> just
> > hangs indefinitely and claims that it's downloading the page.  But
> on
> > the console I'm getting the following:
> >
> > Server Started
> > Starting to read from client.
> > got:GET / HTTP/1.1
> > got:Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg,
> > application/vnd.ms-powerpoint, application/vnd.ms-excel,
> > application/msword, */*
> > got:Accept-Language: en-us
> > got:Accept-Encoding: gzip, deflate
> > got:User-Agent: Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 4.0)
> > got:Host: localhost
> > got:Connection: Keep-Alive
> > got:
> >
> > Then it hangs.  This is what's really bugging me.  It never exits
> the
> > while loop and it never iterates the loop again.  It just hangs
> until
> I
> > close the browser window.  Then it gives me this:
> >
> > Error: Connection reset by peer: JVM_recv in socket input stream
> read
> >
> > There's obviously something here I'm not understanding.  If the
> > inputstream is not null, then in my mind it should continue with the
> > loop and keep printing "got" and the line of input.  Yet, the
> connection
> > stays alive and the loop stops iterating.  Is this a behavior of the
> > BufferedReader, is it a behavior of the Socket?  Is this something
> > unique to using browsers?  If anyone has any insight, I would
> appreciate
> > it.  Thanks.
> 
> 
> 
> --
> To unsubscribe, e-mail:
> <ma...@jakarta.apache.org>
> For additional commands, e-mail:
> <ma...@jakarta.apache.org>
> 




--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>