need urgent help for this thin client type program(execution of exe files)
Server program :-
import java.io.*;
import java.net.*;
public class Server1 {
private Player[] players;
private ServerSocket server;
private ExecHelper exh;
String command = null;
String message = null;
public Server1() {
players = new Player[5];
try {
server = new ServerSocket( 12345, 5 );
System.out.println("Server started...");
System.out.println("Waiting for request...");
}
catch( IOException ioe ) {
ioe.printStackTrace();
System.exit( 1 );
}
} //end Server constructor
public void execute() {
for( int i = 0; i < players.length; i++ )
try {
players = new Player( server.accept() );
players.start();
}
catch( IOException ioe ) {
ioe.printStackTrace();
System.exit( 1 );
}
}
public static void main( String args[] ) {
Server1 ser = new Server1();
ser.execute();
System.exit( 1 );
}
private class Player extends Thread {
private Socket connection;
private ObjectOutputStream output;
private ObjectInputStream input;
public Player( Socket socket ) {
connection = socket;
try {
input = new ObjectInputStream( connection.getInputStream());
output = new ObjectOutputStream( connection.getOutputStream());
output.flush();
}
catch( IOException ioe ) {
ioe.printStackTrace();
System.exit( 1 );
}
}
public void run(){
try {
message = "Enter a command:";
output.writeObject( message );
output.flush();
do {
command = ( String ) input.readObject();
String osName = System.getProperty( "os.name" );
String[] cmd = new String[3];
if( osName.equals( "Windows 2000" )) {
cmd[0] = "cmd.exe";
cmd[1] = "/c";
cmd[2] = command;
}
else if( osName.equals( "Windows NT" ) ) {
cmd[0] = "cmd.exe" ;
cmd[1] = "/C" ;
cmd[2] = command ;
}
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec( cmd );
exh = new ExecHelper( proc, output, input);
} while( !command.equals( "TERMINATE" ) );
}
catch( Throwable t ) {
t.printStackTrace();
}
}
} //end class Player
public class ExecHelper implements Runnable {
private Process process;
private InputStream pErrorStream;
private InputStream pInputStream;
private OutputStream pOutputStream;
private InputStreamReader isr;
private InputStreamReader esr;
private PrintWriter outputWriter;
private ObjectOutputStream out;
private ObjectInputStream in;
private BufferedReader inBuffer;
private BufferedReader errBuffer;
private Thread processThread;
private Thread inReadThread;
private Thread errReadThread;
private Thread outWriteThread;
public ExecHelper( Process p, ObjectOutputStream output, ObjectInputStream input ) {
process = p;
pErrorStream = process.getErrorStream();
pInputStream = process.getInputStream();
pOutputStream = process.getOutputStream();
outputWriter = new PrintWriter( pOutputStream, true );
in = input;
out = output;
processThread = new Thread( this );
inReadThread = new Thread( this );
errReadThread = new Thread( this );
outWriteThread = new Thread( this );
processThread.start();
inReadThread.start();
errReadThread.start();
outWriteThread.start();
}
public void processEnded( int exitValue ) {
try {
Thread.sleep( 1000 );
}
catch( InterruptedException ie ) {
ie.printStackTrace();
}
}
public void processNewInput( String input ) {
try {
out.writeObject( "\n" + input );
out.flush();
}
catch( IOException ioe ) {
ioe.printStackTrace();
}
}
public void processNewError( String error ) {
try {
out.writeObject( "\n" + error );
out.flush();
}
catch( IOException ioe ) {
ioe.printStackTrace();
}
}
public void println( String output ) {
outputWriter.println( output + "\n" );
}
public void run() {
if( processThread == Thread.currentThread()) {
try {
processEnded( process.waitFor());
}
catch( InterruptedException ie ) {
ie.printStackTrace();
}
}
else if( inReadThread == Thread.currentThread() ) {
try {
isr = new InputStreamReader( pInputStream );
inBuffer = new BufferedReader( isr );
String line = null;
while(( line = inBuffer.readLine()) != null ) {
processNewInput( line );
}
}
catch( IOException ioe ) {
ioe.printStackTrace();
}
}
else if( outWriteThread == Thread.currentThread() ) {
try {
String nline = null;
nline = ( String ) in.readObject();
println( nline );
}
catch( ClassNotFoundException cnfe ) {
//cnfe.printStackTrace();
}
catch( IOException ioe ) {
ioe.printStackTrace();
}
}
else if( errReadThread == Thread.currentThread() ) {
try {
esr = new InputStreamReader( pErrorStream );
errBuffer = new BufferedReader( esr );
String nline = null;
while(( nline = errBuffer.readLine()) != null ) {
processNewError( nline );
}
}
catch( IOException ioe ) {
ioe.printStackTrace();
}
}
}
}
}
-
Client Program :-
// client.java
import java.io.*;
import java.net.*;
public class Client {
private ObjectOutputStream output;
private ObjectInputStream input;
private String chatServer;
private String message = "";
private Socket client;
public Client( String host ) {
chatServer = host;
}
private void runClient() {
try {
connectToServer();
getStreams();
processConnection();
}
catch( EOFException eofe ) {
System.err.println( "Client terminated connection ");
}
catch( IOException ioe ) {
ioe.printStackTrace();
}
finally {
closeConnection();
}
} //end method runClient
private void connectToServer() throws IOException {
System.out.println( "Attempting connection...\n");
client = new Socket( InetAddress.getByName( chatServer ), 12345);
System.out.println( "Connected to : "+ client.getInetAddress().getHostName());
}
private void getStreams() throws IOException {
output = new ObjectOutputStream( client.getOutputStream());
output.flush();
input = new ObjectInputStream( client.getInputStream());
}
private void processConnection() throws IOException {
while( true ){
try {
message = ( String ) input.readObject();
System.out.print( message );
InputStreamReader isr = new InputStreamReader( System.in);
BufferedReader br = new BufferedReader( isr );
String line = null ;
line = br.readLine();
output.writeObject(line);
output.flush();
}
catch( ClassNotFoundException cnfe) {
System.out.println( "\nUnknown object type received");
}
}
} //end processConnection
private void closeConnection() {
System.out.println( "\nClosing connection");
try {
output.close();
input.close();
client.close();
}
catch( IOException ioe ) {
ioe.printStackTrace();
}
}
public static void main( String args[] ) {
Client c;
if( args.length == 0 )
c = new Client( "127.0.0.1" );
else
c = new Client( args[0] );
c.runClient();
}
}
[7923 byte] By [
Inoulena] at [2007-10-3 5:12:18]

When the programs are executed only small exe files/programs could be executed and the value is returned. But executing exe files that takes input the program just hangs.
>
>
> Server program :-
>
> import java.io.*;
> import java.net.*;
>
>
> public class Server1 {
> private Player[] players;
> private ServerSocket server;
> private ExecHelper exh;
> String command = null;
> String message = null;
>
> public Server1() {
> players = new Player[5];
> try {
> server = new ServerSocket( 12345, 5 );
> System.out.println("Server started...");
> System.out.println("Waiting for request...");
>
> }
> catch( IOException ioe ) {
> ioe.printStackTrace();
> System.exit( 1 );
> }
> } //end Server constructor
>
> public void execute() {
> for( int i = 0; i < players.length; i++ )
> try {
> players = new Player( server.accept() );
> players.start();
> }
> catch( IOException ioe ) {
> ioe.printStackTrace();
> System.exit( 1 );
> }
> }
>
>
> public static void main( String args[] ) {
> Server1 ser = new Server1();
> ser.execute();
> System.exit( 1 );
> }
>
>
> private class Player extends Thread {
> private Socket connection;
> private ObjectOutputStream output;
> private ObjectInputStream input;
>
> public Player( Socket socket ) {
> connection = socket;
> try {
> input = new ObjectInputStream(
> eam( connection.getInputStream());
> output = new ObjectOutputStream(
> eam( connection.getOutputStream());
> output.flush();
> }
> catch( IOException ioe ) {
> ioe.printStackTrace();
> System.exit( 1 );
> }
> }
>
>
> public void run(){
> try {
> message = "Enter a command:";
> output.writeObject( message );
> output.flush();
> do {
> command = ( String ) input.readObject();
> String osName = System.getProperty( "os.name"
> name" );
> String[] cmd = new String[3];
> if( osName.equals( "Windows 2000" )) {
> cmd[0] = "cmd.exe";
> cmd[1] = "/c";
> cmd[2] = command;
> }
> else if( osName.equals( "Windows NT" ) ) {
> cmd[0] = "cmd.exe" ;
> cmd[1] = "/C" ;
> cmd[2] = command ;
> }
>
> Runtime rt = Runtime.getRuntime();
> Process proc = rt.exec( cmd );
> exh = new ExecHelper( proc, output, input);
>
> } while( !command.equals( "TERMINATE" ) );
> }
> catch( Throwable t ) {
> t.printStackTrace();
> }
> }
> } //end class Player
>
>
> public class ExecHelper implements Runnable {
> private Process process;
> private InputStream pErrorStream;
> private InputStream pInputStream;
> private OutputStream pOutputStream;
>
> private InputStreamReader isr;
> private InputStreamReader esr;
> private PrintWriter outputWriter;
>
> private ObjectOutputStream out;
> private ObjectInputStream in;
>
> private BufferedReader inBuffer;
> private BufferedReader errBuffer;
>
> private Thread processThread;
> private Thread inReadThread;
> private Thread errReadThread;
> private Thread outWriteThread;
>
>
> public ExecHelper( Process p, ObjectOutputStream
> m output, ObjectInputStream input ) {
> process = p;
> pErrorStream = process.getErrorStream();
> pInputStream = process.getInputStream();
> pOutputStream = process.getOutputStream();
> outputWriter = new PrintWriter( pOutputStream, true
> ue );
> in = input;
> out = output;
>
> processThread = new Thread( this );
> inReadThread = new Thread( this );
> errReadThread = new Thread( this );
> outWriteThread = new Thread( this );
>
> processThread.start();
> inReadThread.start();
> errReadThread.start();
> outWriteThread.start();
> }
>
> public void processEnded( int exitValue ) {
> try {
>
> Thread.sleep( 1000 );
> }
> catch( InterruptedException ie ) {
> ie.printStackTrace();
> }
>
> }
>
> public void processNewInput( String input ) {
> try {
> out.writeObject( "\n" + input );
> out.flush();
> }
> catch( IOException ioe ) {
> ioe.printStackTrace();
> }
> }
>
> public void processNewError( String error ) {
> try {
> out.writeObject( "\n" + error );
> out.flush();
>}
>catch( IOException ioe ) {
> ioe.printStackTrace();
> }
> }
>
>
> public void println( String output ) {
> outputWriter.println( output + "\n" );
> }
>
> public void run() {
> if( processThread == Thread.currentThread()) {
> try {
> processEnded( process.waitFor());
> }
> catch( InterruptedException ie ) {
> ie.printStackTrace();
> }
> }
>
> else if( inReadThread == Thread.currentThread() )
> ) {
> try {
> isr = new InputStreamReader( pInputStream );
> inBuffer = new BufferedReader( isr );
> String line = null;
> while(( line = inBuffer.readLine()) != null ) {
> processNewInput( line );
> }
> }
> catch( IOException ioe ) {
> ioe.printStackTrace();
> }
> }
>
> else if( outWriteThread == Thread.currentThread() )
> ) {
> try {
> String nline = null;
> nline = ( String ) in.readObject();
> println( nline );
> }
> catch( ClassNotFoundException cnfe ) {
>
> //cnfe.printStackTrace();
> }
> catch( IOException ioe ) {
> ioe.printStackTrace();
> }
> }
>
>
> else if( errReadThread == Thread.currentThread() )
> ) {
> try {
> esr = new InputStreamReader( pErrorStream );
> errBuffer = new BufferedReader( esr );
> String nline = null;
> while(( nline = errBuffer.readLine()) != null )
> ll ) {
> processNewError( nline );
> }
> }
> catch( IOException ioe ) {
> ioe.printStackTrace();
> }
> }
> }
> }
>
>
> }
>
>
> -
>
> Client Program :-
>
>
> // client.java
>
>
>
> import java.io.*;
> import java.net.*;
>
>
> public class Client {
> private ObjectOutputStream output;
> private ObjectInputStream input;
> private String chatServer;
> private String message = "";
> private Socket client;
>
>
> public Client( String host ) {
> chatServer = host;
>
>
> }
>
>
> private void runClient() {
> try {
> connectToServer();
> getStreams();
> processConnection();
> }
> catch( EOFException eofe ) {
> System.err.println( "Client terminated connection
> ion ");
> }
> catch( IOException ioe ) {
> ioe.printStackTrace();
> }
> finally {
> closeConnection();
> }
> } //end method runClient
>
>
> private void connectToServer() throws IOException {
> {
>
>
> System.out.println( "Attempting connection...\n");
> client = new Socket( InetAddress.getByName(
> e( chatServer ), 12345);
> System.out.println( "Connected to : "+
> "+ client.getInetAddress().getHostName());
> }
>
>
> private void getStreams() throws IOException {
>
> output = new ObjectOutputStream(
> m( client.getOutputStream());
> output.flush();
> input = new ObjectInputStream(
> m( client.getInputStream());
> }
>
>
> private void processConnection() throws IOException
> n {
> while( true ){
> try {
> message = ( String ) input.readObject();
> System.out.print( message );
> InputStreamReader isr = new InputStreamReader(
> der( System.in);
> BufferedReader br = new BufferedReader( isr );
> String line = null ;
> line = br.readLine();
> output.writeObject(line);
> output.flush();
>
> }
> catch( ClassNotFoundException cnfe) {
> System.out.println( "\nUnknown object type
> type received");
> }
> }
> } //end processConnection
>
>
> private void closeConnection() {
> System.out.println( "\nClosing connection");
> try {
> output.close();
> input.close();
> client.close();
> }
> catch( IOException ioe ) {
> ioe.printStackTrace();
> }
> }
>
>
>
>
> public static void main( String args[] ) {
> Client c;
> if( args.length == 0 )
> c = new Client( "127.0.0.1" );
> else
> c = new Client( args[0] );
>
> c.runClient();
> }
> }