1

I have a simple server:

 public static void main(String[] args) {

        try {
            serverSocket = new ServerSocket(2004);  //Server socket

        } catch (IOException e) {
            System.out.println("Could not listen on port: 2004");
        }

        System.out.println("Server started. Listening to the port 2004");

        while (true) {
            try {

                clientSocket = serverSocket.accept();   //accept the client connection
                inputStreamReader = new InputStreamReader(clientSocket.getInputStream());
                bufferedReader = new BufferedReader(inputStreamReader); //get the client message
                message = bufferedReader.readLine();

               // getCommand(message);

                System.out.println(message);
                inputStreamReader.close();
                clientSocket.close();

            } catch (IOException ex) {
                System.out.println("Problem in message reading");
            }
        }

    }

    private void getCommand(String message) throws IOException{
        switch (message) {
        case "1":
            turnOffComputer();
            break;

        default:
            break;
        }
    }

    private void turnOffComputer() throws IOException{
        Runtime runtime = Runtime.getRuntime();
        Process proc = runtime.exec("shutdown -s -t 0");
        System.exit(0);
    }

}

now I want to add functionality that if I get number 1 in message I want to use method getCommand() to turn off the computer. How should I change my code to do that?

EDIT: this is my client:

  try {

         client = new Socket(IP_ADDRESS, PORT);  //connect to server
         printwriter = new PrintWriter(client.getOutputStream(),true);
         printwriter.write(messsage);  //write the message to output stream

         printwriter.flush();
         printwriter.close();
         client.close();   //closing the connection

        } catch (UnknownHostException e) {
         e.printStackTrace();
        } catch (IOException e) {
         e.printStackTrace();
        }
       }
      });
edi233
  • 3,511
  • 13
  • 56
  • 97
  • Don't call it `getCommand()`, make it more descriptive, like `executeCommand()`. Just call the method when you've retrieve the message from `readLine()` – Sotirios Delimanolis May 22 '13 at 20:53
  • Daniel Pereira and Ziyao Wei I know how I can turn off computer. I would like to know how I need to change my code to use that method in my application. – edi233 May 22 '13 at 20:55
  • Are you just planning on sending "1" as the whole message? – ryanbwork May 22 '13 at 20:56
  • maybe there are spaces or new lines symbols etc. in the message. Try message.trim() ? Also I think you will need java7 for strings in switch statemants. – HectorLector May 22 '13 at 20:58
  • this will work only in Java 7, here is the explanation: http://stackoverflow.com/questions/338206/switch-statement-with-strings-in-java – hoaz May 22 '13 at 20:58
  • yes - as hoaz says, switches won't work with strings in java versions below 7. This means you'll have to convert the string "1" to a number, then write a switch to deal with numeric values. – ryanbwork May 22 '13 at 21:00
  • You probably have to send `"1\n"`. And could do `bufferedReader.close()` instead of `inputStreamReader.close()`. – Joop Eggen May 22 '13 at 21:01
  • or just use `if ("1".equals(message)) { ... }` – hoaz May 22 '13 at 21:02
  • i use if ("1\n".equals(message)) { System.out.println("success"); } but still I don't get success and if ("1".equals(message)) { System.out.println("success"); } – edi233 May 22 '13 at 21:13

0 Answers0