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
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:
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:
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:
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:
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?