Estuve haciendo algunas prácticas para acelerar mi desarrollo en sockets Java, por eso intento crear un shell remoto, funciona correctamente, pero el problema viene cuando quiero ejecutar más de un comando una vez ejecutado el cmd ya que se congela
Código del servidor:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class RemoteShell {
private int port;
private String commandStr = "cmd";
private Socket socket;
public RemoteShell(String [] args) {
try {
port = 2222;
} catch (NumberFormatException e) {
System.out.println("Invalid port");
System.exit(1);
}
}
public void startServer() {
try {
ServerSocket server = new ServerSocket(port);
socket = server.accept();
} catch (IOException e) {
e.printStackTrace();
}
}
public InputStream inputStream() {
try {
InputStream input = socket.getInputStream();
return input;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public OutputStream outputStream() {
try {
OutputStream output = socket.getOutputStream();
return output;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public String getCommand() {
return commandStr;
}
public static void main(String []args) {
RemoteShell shell = new RemoteShell(args);
shell.startServer();
// Read from client
InputStream input = shell.inputStream();
// Write output to client
OutputStream output = shell.outputStream();
try{
Process command = Runtime.getRuntime().exec(shell.getCommand());
// Reading output from Process
BufferedReader reader = new BufferedReader(
new InputStreamReader(command.getInputStream()));
// Write data to the Process
OutputStream commandStream = command.getOutputStream();
final BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(commandStream));
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
byte[] buffer = new byte[1024];
try {
int bytesRead;
while (command.isAlive() &&
((bytesRead = input.read(buffer)) != -1))
{
buffer[bytesRead] = '\n';
if (command.isAlive()) {
writer.write(new String(buffer));
writer.flush();
// Sleep to make the command exit
// in case 'exit' was written
Thread.sleep(200);
} else {
break;
}
}
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
thread.start();
String line = "";
while ((line = reader.readLine()) != null) {
System.out.println(line);
line += "\n";
output.write(line.getBytes());
}
command.waitFor();
thread.join();
System.out.println("program exit");
}
catch(IOException e){
e.printStackTrace();
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
}
Codigo del cliente:
import java.net.*;
import java.io.*;
import java.net.Socket;
public class Client
{
public static void main(String[] args) {
String hostname = "127.0.0.1";
int port = 2222;
try (Socket socket = new Socket(hostname, port)) {
BufferedReader us = new BufferedReader(new InputStreamReader(System.in));
OutputStream output = socket.getOutputStream();
PrintWriter writer = new PrintWriter(output, true);
writer.println(us.readLine());
//writer.println("ipconfig");
InputStream input = socket.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
} catch (UnknownHostException ex) {
System.out.println("Server not found: " + ex.getMessage());
} catch (IOException ex) {
System.out.println("I/O error: " + ex.getMessage());
}
}
}
Nota : no quiero ejecutar más de un comando en un lanzamiento Runtime.getRuntime().exec("lista de comandos"); si no lo que quiero es abrirlo y mandar tantos comandos como desee el usuario