title: How to check if client has disconnected?
body: <p>I have a java program with Socket. I need to check if client has disconnected. I need a example how to do that. I have researched but I don't understand. So can someone make example code and explane everything.</p>
  
  <p>sorry for bad English
  my code:</p>
  
  <pre><code>    package proov_server;
  
  //SERVER 2
  import java.io.*;
  import java.net.*;
  
  class server2 {
      InetAddress[] kasutaja_aadress = new InetAddress[1000];
      String newLine = System.getProperty("line.separator");
      int kliendiNr = 0;
      int kilene_kokku;
    server2(int port) {
  
      try {
  
        ServerSocket severi_pistik = new ServerSocket(port);
          System.out.println("Server töötab ja kuulab porti " + port + ".");
  
  
        while (true) {
  
          Socket pistik = severi_pistik.accept();
          kliendiNr++;
  
          kasutaja_aadress[kliendiNr] = pistik.getInetAddress();
          System.out.println(newLine+"Klient " + kliendiNr + " masinast "
                        + kasutaja_aadress[kliendiNr].getHostName() + " (IP:"
                        + kasutaja_aadress[kliendiNr].getHostAddress() + ")");
  
          // uue kliendi lõime loomine
          KliendiLoim klient = new KliendiLoim(pistik,kliendiNr);
  
          // kliendi lõime käivitamine
          klient.start();
  
        }
      }
      catch (Exception e) {
        System.out.println("Serveri erind: " + e);
      }
    }
  
    DataOutputStream[] väljund = new DataOutputStream[1000];
    DataInputStream[] sisend = new DataInputStream[1000];
  
    int klient = 0;
    int nr;
    // sisemine klass ühendusega tegelemiseks
        class KliendiLoim extends Thread {
  
          // kliendi pistik
  
        Socket pistik;
          // kliendi number
  
  
          KliendiLoim(Socket pistik2,int kliendiNr) {
            nr = kliendiNr;
            this.pistik = pistik2;
  
          }
          public boolean kontroll(){
              try{
                  System.out.println("con "+pistik.isConnected());
                  System.out.println("close "+pistik.isClosed());
                  if(pistik.isConnected() &amp;&amp;  !pistik.isClosed()){
                      //System.out.print(con_klient);
                      return true;
  
                  }
              }catch(NullPointerException a){
                  System.out.println("Sihukest klienti pole!!!");
  
              }
              kliendiNr --;
              return false;
  
          }
  
          public void run() {
                try { 
                      sisend[nr] = new DataInputStream(pistik.getInputStream()); //sisend
                      väljund[nr] = new DataOutputStream(pistik.getOutputStream()); //väljund 
                }catch (Exception ea) {
                      System.out.println(" Tekkis erind: " + ea);
                }   
                while(true){
                        try{
  
  
                          BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  
                          System.out.print("Sisesta k2sk: ");
                          String k2sk = null;
                          k2sk = br.readLine();
                          /*
                          String command;
                          if(k2sk.indexOf(" ") &lt; 0){
                              command = k2sk;
                          }else{
                              command = k2sk.substring(0, k2sk.indexOf(" "));
                          }
                          */
                          String[] words = k2sk.split("\\s+");
                          for (int i = 0; i &lt; words.length; i++) {
                              words[i] = words[i].replaceAll(" ", "");
                          }
                          switch(words[0]){
                          case "suhtle":
  
                              if(väljund.length &gt; klient &amp;&amp; väljund[klient] != null)
                              {
                                  väljund[klient].writeUTF("1");
  
  
                              }else{
                                  väljund[klient] = null;
                                  sisend[klient] = null;
                                  System.out.println("Sihukest klienti pole");
                              }
                          break;
                          case "vaheta":
                              try{
                                  int klinetnr = Integer.parseInt(words[1]);
                                  //if(kontroll(klinetnr) ){
                                      klient = Integer.parseInt(words[1]);
                                  //}
                              }
                              catch(NumberFormatException e){
                                  System.out.println("See pole number!!! ");
                              }
                          break;
  
                          case "kliendid":
                              if(kliendiNr != 0){
                                  for(int i=1;i &lt;= kliendiNr;i++){
                                      if(kontroll()){
                                          System.out.println("Klient:"+i+" ip: " + kasutaja_aadress[i] );
                                      }else{
                                          System.out.println("Pisi");
                                          väljund[klient] = null;
                                          sisend[klient] = null;
                                      }
                                  }
                                  System.out.println(newLine);
                              }else{
                                  System.out.println("Kiente pole");
                              }
                          break;
  
                          }
                        System.out.println(kliendiNr);
                      }catch(SocketException a){
                          System.out.println("Klient kadus");
  
                      }
                      catch(Exception e){
                           System.out.println(" Viga: " + e);
                      }
  
            }
          }
  
  
  
    }
    public static void main(String[] args) {
          new server2(4321);
  
    }
  }
  </code></pre>