Problems with java (input streams)
Posted: Mon Mar 27, 2006 3:48 pm
I have a client program that connects to a server and is able to send DOS commands. The command is sent to the server, a new Process is created and BufferedReader to get the input stream from the process. The BufferedReader is used to send information back to the client, that information being the output from the command they chose.
The problem is that the loop that runs on the client program seems to be inifinite so I can only enter one command each time I run the program (i have to force quit out of the loop every time);
Here is the necessary code for the server:
And the code for the client:
The last while loop in the client code is the one that appears to be in an inifinite loop. Any ideas on why in.readLine() is never turning null?
The problem is that the loop that runs on the client program seems to be inifinite so I can only enter one command each time I run the program (i have to force quit out of the loop every time);
Here is the necessary code for the server:
Code: Select all
String[] commands = new String[3];
commands[0] = "cmd";
commands[1] = "/C";
String command, output;
BufferedReader commandInfo = null;
Process current;
while((command = in.readLine()) != null) {
commands[2] = now.decrypt(command);
System.out.println(now.returnDate() + ": " + clientSocket.getInetAddress() + " - " + now.decrypt(command));
current = Runtime.getRuntime().exec(commands);
commandInfo = new BufferedReader(new InputStreamReader(current.getInputStream()));
while((output = commandInfo.readLine()) != null) {
out.println(now.encrypt(output));
}
}Code: Select all
boolean loop = true;
String command, line;
while(loop) {
System.out.print("Command: ");
command = keys.readLine();
out.println(now.encrypt(command));
if(command.equalsIgnoreCase("quit")) {
System.out.println("Exiting...");
loop = false;
} else {
while((line = in.readLine()) != null) {
System.out.println(now.decrypt(line));
}
System.out.println("Command executed successfully\n");
}
}