title: Server socket has to be wait for client's next command java
body: <p>I am making a simple client server app. In which client will send shutdown,restart command to server. But how to hold the server socket for client's next command.Means socket is accepting command for once. after that server is not accepting other commands.For eg. if client send "restart" then server will execute the command with given time and after if client send "abort" command then it should be aborted.but server socket is getting closed after only one command is accepted.</p>
  
  <p>how can i make the server socket wait for further commands?</p>
  
  <p>Here is server code :</p>
  
  <pre><code> running = true;
      try {
          System.out.println("Waiting for client........");
          ServerSocket serverSocket = new ServerSocket(8002);
          Socket client = serverSocket.accept();
          System.out.println("Connection Accepted......");
          BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
          try {
              while (running) {
                  String usercmnd = in.readLine();
                  if (usercmnd != null) {
                      Runtime runtime = Runtime.getRuntime();
                      Process p = runtime.exec(usercmnd);
                      //serverSocket.setSoTimeout(30000);
                  }
              }
  
          } catch (Exception e) {
              System.out.println("Error");
              e.printStackTrace();
          }finally{
          client.close();
          System.out.println("Connection Closed......");
          }
      } catch (Exception e) {
          System.out.println("Error");
          e.printStackTrace();
      } 
  </code></pre>
  
  <p>Here is client code</p>
  
  <pre><code>restart.setOnClickListener(
              new View.OnClickListener() {
                  public String rest = "shutdown -r -t 30";
                  @Override
                  public void onClick(View v) {
                      Socket cs = null;
                      try{
                          cs = new Socket("192.168.20.128",8002);
                          DataOutputStream out = new DataOutputStream(cs.getOutputStream());
                          out.writeBytes(rest);
                          Toast.makeText(PowerActivity.this, "Restart Success", Toast.LENGTH_LONG).show();
  
                      }
                      catch (IOException e) {
                          Toast.makeText(PowerActivity.this, e.getMessage(), Toast.LENGTH_LONG).show();
                      }finally {
                          try {
                              cs.close();
                          } catch (IOException e) {
                              e.printStackTrace();
                          }
                      }
                  }
              }
      );
  </code></pre>