I'd like to create a Java GUI for the monopoly game in the bsdgames
package, but I am having trouble getting the Java program to connect to
the C program.  Runtime.getRuntime().exec("/usr/games/monop"); works just
fine, but Process.getInputStream() doesn't seem to work.

Has anyone succeeded at getting a Java program to interact with a C
program via stdin/stdout?  If so, how?  If not, can you explain the
behavior of the following code?  (JDK 1.1.6 and Debian 2.0 libc5, i386)

import java.io.*;
import java.awt.*;

public class Monopoly extends Thread {
  private InputStream in;
  private BufferedWriter out;
  private Process game = null;

  public Monopoly() {
    try {
      game = Runtime.getRuntime().exec("/usr/games/monop");
      in = game.getInputStream();
      out = new BufferedWriter(new
OutputStreamWriter(game.getOutputStream()));
      System.out.println(in.available());
    }
    catch(IOException e) { System.err.println("Program not started"); }
  }

  public static void main(String args[]) {
    Monopoly f = new Monopoly();
    System.out.println("ready to begin");
    f.start();
  }

  public void run() {
    InputStreamReader ir = new InputStreamReader(in);
    int count = 0;
    try {
      while(!(ir.ready())) {
        this.yield();
        this.sleep(500);
        if(count++ > 10) break;
      }
      System.out.println(ir.ready());
      out.write('\n');
      System.out.println(ir.ready());
      out.newLine();
      out.flush();
      System.out.println(ir.ready());
      out.write('3');
      out.newLine();
      System.out.println(ir.ready());
      System.out.println(ir.read());
    }
    catch(IOException e2) {}
    catch(InterruptedException ex) {}
    finally { System.out.println("done"); }
  }
}


bash$ /usr/local/jdk1.1.6/bin/java Monopoly
0
ready to begin
false
false
false
false

The program hangs indefinitely here.  Thanks for your time.

Richard Hall
Network Services
University of Tennessee

Reply via email to