D
D
Daniel2018-01-27 08:42:03
Java
Daniel, 2018-01-27 08:42:03

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);
        }
        
    }

Method of reading plus checking every second the status of the Client Socket thread.
In general, there are no errors, but why the socket stream will always show true, and how to clear the client connection object . He is in memory. And how to kill him on false to throw off, in general, how, there must be a method like disconnect ();
(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

1 answer(s)
S
Sergey Gornostaev, 2018-01-27
@daniil14056

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 question

Ask a Question

731 491 924 answers to any question