You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Jason Johnston <Ja...@epa.state.il.us> on 2002/12/02 18:03:30 UTC

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. 

RE: Socket GURU'S PLEASE HELP ON OBSCURE PROBLEM

Posted by Jeremy Joslin <je...@spotlife.com>.
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>