Heloo,this wil be my first post here, the reason for that is I can't figure this one out by myself
public class Client extends Thread{
Socket socket = null;
ServerResponse sr = null;
private class serverResponse extends Thread{
InputStream in = null;
public serverResponse(Socket socket) throws IOException{
if (socket == null) { System.out.println("socket:null");}
this.in = socket.getInputStream();
}
public void run() {
try {
while (true) {
if (!socket.isClosed() || socket.isConnected()) {
byte[] input = new byte[1024];
// read the message
in.read(input, 0, input.length);
// unserialize and dispatch
}
}
} catch (IOException ex) {
System.out.println("IOException in waitForReplyFromServer");
ex.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public client() throw Exception{
this.socket =new Socket(InetAddress.getBytName("localhost", 4444);
this.sendMessage(new String(john").getBytes());
}
sendMessage(byte[] tosend){
OutputStream out = null;
try {
out = this.socket.getOutputStream();
out.write(bytesToSend, 0, bytesToSend.length);
out.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("Exception in sendBytes()");
e.printStackTrace();
} finally {
if (out != null) {
out.close();
}
}
}
public void run(){
this.serverResponse.start();
}
}
Excepting for the lack of some client class members, this what my class looks like, the problem that I was referring about is I get an IOException :IOException in waitForReplyFromServer
java.net.SocketException: socket closed
from this line line: in.read(input, 0, input.length);
, the thing that confuses me the most is that is after the if-condition