views:

192

answers:

1

I have two processes that I'd like to pipe together with C#, and then send the output of the second one directly to an HTTP output stream.

In a command line, I can do this and it works perfectly (where "-" represents stdin/stdout):

proc1 "file_to_input" - | proc2 - -

My problem is connecting these two processes in C# and being able to take proc2's STDOUT and port it directly to the web, without buffering the entire thing first. The inputs and outputs will be a binary datatype, so I'll need to convert it from the default StreamReader/Writer.

Is this possible, and what is the best way to go about it? Thanks!

A: 

Unless I'm misunderstanding you, this is the normal "read from one stream, write to another" situation, where you declare a buffer of some size and just keep reading from the input and writing to the output until there's nothing left to read from the input.

In .NET 4.0 they added a CopyTo method on to Stream to make this simpler, but you can just do the same code yourself.

Stream source = ...;
Stream destination = ...;
byte[] buffer = new byte[4096];
int read;
while ((read = source.Read(buffer, 0, buffer.Length)) != 0) {
    destination.Write(buffer, 0, read);
}

In your case, it sounds like you can get source via Console.OpenStandardInput - for the destination I'm not 100% sure what you mean by "port it directly to the web", but since we're a console app and not an asp.net page, I'm assuming you're sending it to some destination page. If you're using WebClient, you can use its OpenWrite method. If it were an asp.net page reading this from a separate process, it could write it to Response.OutputStream instead.

Related links:

James Manning
The complicated part about this is feeding one stream while simultaneously reading from another. It's a 3-way kind of situation, where you have to read from one, write to a second, and read from the second at the same time. I'm not sure how to receive the output of the 2nd while feeding it new data at the same time.
jocull