title: java Simple Server Client program
body: <p>I'm trying to build a simple server client program, I'm trying to figure a way how to prompt the CLIENT if the server is down, or if the server is up and loses connection</p>
  
  <p>Question: How can I prompt the client that he's disconnected because the Server shuts down or loses connection</p>
  
  <p><strong>SERVER</strong></p>
  
  <pre><code>public class Server{
  
  private static Socket socket;
  
  public static void main(String[] args)
  {
      try
      {
          int port = 25000;
          ServerSocket serverSocket = new ServerSocket(port);
          //Server is running always. This is done using this while(true) loop
          while(true)
          {
              //Reading the message from the client
              socket = serverSocket.accept();
              System.out.println("Client has connected!");
              InputStream is = socket.getInputStream();
              InputStreamReader isr = new InputStreamReader(is);
              BufferedReader br = new BufferedReader(isr);
              String number = br.readLine();
              System.out.println("Message received from client is "+number);
  
              //Multiplying the number by 2 and forming the return message
              String returnMessage;
              try
              {
                  int numberInIntFormat = Integer.parseInt(number);
                  int returnValue = numberInIntFormat*2;
                  returnMessage = String.valueOf(returnValue) + "\n";
              }
              catch(NumberFormatException e)
              {
                  //Input was not a number. Sending proper message back to client.
                  returnMessage = "Please send a proper number\n";
              }
  
              //Sending the response back to the client.
              OutputStream os = socket.getOutputStream();
              OutputStreamWriter osw = new OutputStreamWriter(os);
              BufferedWriter bw = new BufferedWriter(osw);
              bw.write(returnMessage);
              System.out.println("Message sent to the client is "+returnMessage);
              bw.flush();
          }
      }
      catch (Exception e)
      {
          e.printStackTrace();
      }
  }
  </code></pre>
  
  <p><strong>CLIENT</strong></p>
  
  <pre><code>public class Client{
  
  private static Socket socket;
  
  public static void main(String args[])
  {
      Scanner input=new Scanner(System.in);
      while(true)
      {
          try
          {
              String host = "localhost";
              int port = 25000;
              InetAddress address = InetAddress.getByName(host);
              socket = new Socket(address, port);
              //System.out.println("You're now connected to the Server"); /*this should only print once */
              //Send the message to the server
              OutputStream os = socket.getOutputStream();
              OutputStreamWriter osw = new OutputStreamWriter(os);
              BufferedWriter bw = new BufferedWriter(osw);
  
              String number;
              number=input.next();
              String sendMessage = number + "\n";
              bw.write(sendMessage);
              bw.flush();
              System.out.println("Message sent to the server : "+sendMessage);
  
              //Get the return message from the server
              InputStream is = socket.getInputStream();
              InputStreamReader isr = new InputStreamReader(is);
              BufferedReader br = new BufferedReader(isr);
              String message = br.readLine();
              System.out.println("Message received from the server : " +message);
          }
          catch (IOException exception)
          {
              //System.out.println("Server is still offline");/*This should only print once*/
          }
  </code></pre>