im making a program that you type ifconfig(Linux) or ipconfig(Windows) in the Client, Then it sends to the Server and there execute the shell.
It executes ok, and in the server I can see the ipconfig correctly, but i dont know how to return the result of the server to the client.
Client Side:
public class ShellClient {
public static void main(String[] args) throws IOException {
String comand;
Socket server =null;
server = new Socket("localhost", 1234);
while(true){
System.out.println("Insert ipconfig(windows)/ ifconfig(Linux): ");
@SuppressWarnings("resource")
Scanner keyboard = new Scanner(System.in);
comando = keyboard.next();
try{
System.out.println("Conecting...");
DataOutputStream out = new DataOutputStream(new BufferedOutputStream(server.getOutputStream()));
DataInputStream in = new DataInputStream(new BufferedInputStream(server.getInputStream()));
out.writeUTF(comand);
out.flush();
String result = in.readUTF();
System.out.println(result);
if (comand == "0"){server.close();
System.out.println("Finish. Thank You!");
System.exit(0);}
}catch(IOException ioe){
System.err.println(ioe);
}
}
}
}
Server Side:
public class ShellServer {
public static void main(String[] args) throws IOException {
ServerSocket socket = null;
Socket client = null;
String result;
String comand ;
String s = null;
socket = new ServerSocket(1234);
System.out.println("The server keep working...");
client = socket.accept();
System.out.println("The client is connected");
DataInputStream in = new DataInputStream(new BufferedInputStream(client.getInputStream()));
DataOutputStream out = new DataOutputStream(new BufferedOutputStream(client.getOutputStream()));
comand = in.readUTF();
result = Shell.CheckCommand(comand);
//out.writeUTF(result);
//out.flush();
// Ejcutamos el comando
Process p = Runtime.getRuntime().exec(result);
BufferedReader stdInput = new BufferedReader(new InputStreamReader(
p.getInputStream()));
while ((s = stdInput.readLine()) != null) {
System.out.println(s);
}
if(comand == "0"){client.close();
socket.close();}
}
}
Thank You in Advance Guys!