Answer the question
In order to leave comments, you need to log in
How to disable client connection. isConnected() always true?
How to disable the client socket on the server. isConnected(); always true. Under what conditions is it false at all? At least to see.
public void disconnectClient(){
connected=false;
try{
if(client!=null){
printServer.print("Client "+address+ " disconnect\n");
printServer.flush();
client.close();
}
}catch(IOException ex){}
synchronized(connections){
connections.remove(this.address);
}
}
(new Thread(){ // для демонстрации проблемы только
public void run(){
while(client.isConnected()){
System.out.println(client.isConnected(); // вечное true ни как не зависит от клиента
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
System.out.println(ex);
}
}
}
}).start();
try{
while((line=bis.readLine())!=null){
if(line.equalsIgnoreCase("exit")){
disconnectClient();
return;
}
// .....
}catch(IOEcxeption ex){
disconect();
}
Answer the question
In order to leave comments, you need to log in
From documentation :
What disconnect() should look like depends on the protocol used. The correct sequence of actions is to notify the client that the connection is closed, call close() on the client socket, and remove the reference to it.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question