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.