Results 1 to 3 of 3
  1. #1
    Sneaky Fox is offline Member
    Join Date
    Jan 2011
    Posts
    10
    Rep Power
    0

    Default BufferedReader closing by itself (not what i want)

    so i'm just making some simple program to send and receive messages through the web and i actually already did a simpler version but this time i wanted to use Threads.
    i got 4 classes:
    Server class
    Java Code:
    public class Server{
    
    	public static void main(String[] args) throws IOException{
    	
    		InetSocketAddress server_addr=new InetSocketAddress(InetAddress.getLocalHost(),4445);
    		ServerSocket server_sock=new ServerSocket();
    		server_sock.bind(server_addr);
    		
    		System.out.println("debug server... "+server_sock.getInetAddress().getHostAddress()+" : "+server_sock.getLocalPort());
    		
    		Socket client_sock=server_sock.accept();
    		PrintWriter out=new PrintWriter(client_sock.getOutputStream(),true);
    		BufferedReader in=new BufferedReader(new InputStreamReader(client_sock.getInputStream()));
    		
    		System.out.println("debug client... "+client_sock.getInetAddress().getHostAddress());
    		
    		ReadingThread rt=new ReadingThread(in);
    		WritingThread wt=new WritingThread(out);
    		
    		rt.start();
    		wt.start();
    		
    		in.close();
    		out.close();
    		client_sock.close();
    		server_sock.close();
    		
    	}
    	
    }
    Client class:
    Java Code:
    public class Client{
    	
    	public static void main(String[] args) throws IOException{
    	
    		InetAddress address=InetAddress.getByName("79.168.234.124");
    		System.out.println("debug inet... "+address.getHostAddress()+" "+address.getHostName());
    		Socket sock=new Socket(address,4445);
    		PrintWriter out=new PrintWriter(sock.getOutputStream(),true);
    		BufferedReader in=new BufferedReader(new InputStreamReader(sock.getInputStream()));
    		
    		System.out.println("debug client... "+sock.getInetAddress().getHostAddress());
    		
    		ReadingThread rt=new ReadingThread(in);
    		WritingThread wt=new WritingThread(out);
    		
    		rt.start();
    		wt.start();
    	
    		out.close();
    		in.close();
    		sock.close();
    	
    	}
    	
    }
    and the thread classes:
    WritingThread:
    Java Code:
    public class WritingThread extends Thread{
    	
    	private BufferedReader _stdIn=new BufferedReader(new InputStreamReader(System.in));
    	private PrintWriter _out;
    	private String _string;
    	
    	public WritingThread(PrintWriter out){
    		_out=out;
    	}
    	
    	public void run(){
    		try{
    			while(true){
    				_string=_stdIn.readLine();
    				_out.println(_string);
    			}
    		}
    		catch(IOException e){
    			e.printStackTrace();	
    		}
    	}
    	
    }
    and the ReadingThread:
    Java Code:
    public class ReadingThread extends Thread{
    	
    	private BufferedReader _in;
    	private String _string;
    	
    	public ReadingThread(BufferedReader in){
    		_in=in;
    	}
    	
    	public void run(){
    		try{
    			while(true){
    				_string=_in.readLine();
    				System.out.println("Him: "+_string);
    				if(_string.equals("bye")){
    					break;
    				}
    			}
    		}
    		catch(IOException e){
    			e.printStackTrace();
    		}
    	}
    
    }
    and this one is the one causing my problem. what is happening is that i get this exception:
    Java Code:
    java.io.IOException: Stream closed
            at java.io.BufferedReader.ensureOpen(Unknown Source)
            at java.io.BufferedReader.readLine(Unknown Source)
            at java.io.BufferedReader.readLine(Unknown Source)
            at ReadingThread.run(ReadingThread.java:16)
    meaning the BufferedReader is being closed automatically when passed as argument of the ReadingThread constructor or anywhere else i dunno about...
    by the way: the PrintWriter doesn't seem to be closing so it's even more strange...

    what can i do to solve this issue? is there any way to pass the bufferedReader without closing it?

  2. #2
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    Remove the following lines from Client...

    Java Code:
    		out.close();
    		in.close();
    		sock.close();
    ...and remove these from Server...

    Java Code:
    		in.close();
    		out.close();
    		client_sock.close();
    		server_sock.close();
    ...and it should work as expected. The problem is, you are closing the streams and the sockets down while you have threads that are trying to access them.

  3. #3
    lovelesh is offline Senior Member
    Join Date
    Nov 2010
    Location
    Delhi
    Posts
    135
    Blog Entries
    1
    Rep Power
    0

    Smile

    Quote Originally Posted by toadaly View Post
    Remove the following lines from Client...

    Java Code:
    		out.close();
    		in.close();
    		sock.close();
    Write finalize method in client and put these lines in it.


    ...and remove these from Server...

    Java Code:
    		in.close();
    		out.close();
    		client_sock.close();
    		server_sock.close();
    Same way, place these lines in finalize method.

Similar Threads

  1. Closing 1 Desktop App, But Not The Other
    By longphant in forum AWT / Swing
    Replies: 4
    Last Post: 08-25-2010, 10:38 PM
  2. closing FileOutputStream?
    By mgrootsch in forum New To Java
    Replies: 1
    Last Post: 05-17-2010, 05:36 PM
  3. Closing a JFrame
    By SBL in forum AWT / Swing
    Replies: 5
    Last Post: 11-23-2009, 02:54 PM
  4. Closing an Editor
    By janpol1 in forum Eclipse
    Replies: 0
    Last Post: 02-14-2009, 04:00 PM
  5. Replies: 5
    Last Post: 06-26-2008, 03:16 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •