views:

34

answers:

2

Hello.

I have 2 Windows console applications, reading from standard input and writing to standard output. I'd like to assume I don't have the source code to any of them (actually I usually have the source code of one of them).

I want to direct the standard output of one to the standard input of the other and vice versa, thus allowing communication (the goal is to allow two programs playing a certain game to play one against the other).

How can this be done? I'm willing to do some C/C++ programming or anything else required.

Oren.

+1  A: 

This isn't possible to do in a console, since stream redirection always forms an acyclic graph.

To get the cyclic redirect you are looking for, you can code up a small program that invokes both of your executables, and manages the wiring of the input and output streams of each process. Each pair of streams that are connected (an input stream and output stream from each program) will need a thread to pull data from the output stream and push this into the input stream. So, you will need 2 threads, since there are 2 sets of input/output stream pairs.

Here are the steps to connecting the various streams:

  • create two buffers, B1 and B2.
  • launch program 1, (P1) and fetch the input and output stream handles.
  • connect P1 input to the input side of buffer B1, and P1 output to the output side of buffer B2.
  • launch program 2, (P2) and fetch the input and output stream handles
  • connect P2 input to the input side of buffer B2, and P2 output to the output side of buffer B1. (note that this B1/B2 is reversed from P1 connection above.)

The buffers are active threads, reading from the input side and writing to the output side.

The buffer will have to detect the end of stream on the input side and close the output side accordingly. When both sides are closed, the thread running the buffer can exit.

mdma
Thanks! It works. I implemented this threading idea (in python, as kerkeslager suggested). Thanks alot!
A: 

You'll have to make the two processes into subprocesses of a parent process, which will pass the messages between the two processes, and you'll have to write the parent process.

There are a lot of languages this could be solved in. Personally I would approach this with the subprocess module in Python. That's basically a wrapper around _popen() which can be accessed in C/C++, but if a quick Google search is any indicator, you'd have an easier time learning Python than using that. If you know Ruby, there's a similar library for Ruby.

In any case, I think that using sockets would be a cleaner and more reliable approach than using stdin and stdout, if that's at all an option.

kerkeslager
Thank you. Python sounds like a good solution. Sockets are not an option (unfortunately).I still have a problem: Which process to read from at each time. It's a problem if I try to read from one process while it's waiting for the other process to write something. I should probably do something like 'select', but as far as I tried and heard, select doesn't work in Windows.What can be done? Thanks.