title: Keeping a java server open after the client sends a message
body: <p>I am having an issue figuring out how to keep a connection open with my server class.  When it connects I want the client to send to the server that a client has connected, which it does.  My issue is that right after it receives the message the java file stops running.  Could anybody give me some advice on how to keep the Server waiting for a message from the user until a certain message is received?  Thank you in advance, will be researching in the mean time.</p>
  
  <p>Client class:</p>
  
  <pre><code>import java.io.*;
  import java.net.*;
  import java.util.*;
  
  public class Client {
  
      public static void main(String[] args) throws Exception
      {
          Client myClient = new Client();
          myClient.run();
      }
  
      public void run() throws Exception
      {
          Socket clientSocket = new Socket("localhost", 9999);
          PrintStream ps1 = new PrintStream(clientSocket.getOutputStream());
          ps1.println("A client has successfully connected.");
          //Sends message to the server
          PrintStream ps = new PrintStream(clientSocket.getOutputStream());
          Scanner scan = new Scanner(System.in);
          String cMessage = scan.nextLine();
          ps.println(cMessage);
          //Reads and displays response from server
          InputStreamReader ir = new InputStreamReader(clientSocket.getInputStream());
          BufferedReader br = new BufferedReader(ir);
          String message = br.readLine();
          System.out.println(message);        
      }
  
  }
  </code></pre>
  
  <p>Server class:</p>
  
  <pre><code>import java.io.*;
  import java.net.*;
  
  public class Server {
  
      public static void main(String[] args) throws Exception
      {
          Server myServer = new Server();
          myServer.run();
      }
  
      public void run() throws Exception
      {
  
          //Initializes the port the serverSocket will be on
          ServerSocket serverSocket = new ServerSocket(9999);
              System.out.println("The Server is waiting for a client on port 9999");
          //Accepts the connection for the client socket
          Socket socket = serverSocket.accept();
  
          InputStreamReader ir = new InputStreamReader(socket.getInputStream());
          BufferedReader br = new BufferedReader(ir);
          String message = br.readLine();
          //Confirms that the message was received
          System.out.println(message);
  
              if(message.equals("HELLO"))
              {
                  PrintStream ps = new PrintStream(socket.getOutputStream());
                  ps.println("Received our hello message.");
              }
              else
              {
                  PrintStream ps = new PrintStream(socket.getOutputStream());
                  ps.println("Did not receive your hello message");
              }
      }
  
  }
  </code></pre>