Reputation: 310
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.
how can i make the server socket wait for further commands?
Here is server 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();
}
Here is client 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();
}
}
}
}
);
Upvotes: 1
Views: 1593
Reputation: 19158
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.
That depends on how you're sending commands to the server.
If you're sending commands to the server in the same button's on click, then I don't find the other message being sent from your client to the server. Whereas, the server is waiting for the data in a while-loop.
If you're sending commands to the server, with each button click; then in that case, the server can accept only one connection as it has only one socket.accept() method.
If you're to send commands in this way, then wrap your server code inside a while(true) loop.
In that case, your server code should be something like this :
try {
System.out.println("Waiting for client........");
ServerSocket serverSocket = new ServerSocket(8002);
while(true){
Socket client = serverSocket.accept();
System.out.println("Connection Accepted......");
...//
}
In this case, you'd have to use thread to manage your socket input-stream as in.readLine();
is a blocking call; and you have infinite loop here too. So, use a thread, pass your socket object(referred as client
in your Server code) to this thread, and wrap the rest body inside the thread's run method.
NOTE : BTW, I suspect that you're trying to shutdown the server as per the command you've given. If such is the case, then it won't be able to communicate further as server would be shutdown.
If it is not the case, then it is fine. Probably, in that case, I misunderstood your question's commands then.
Upvotes: 1