简体   繁体   中英

Pipe STDOUT/STDIN with two process and a web output stream in C#

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!

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:

Try CliWrap :

var cmd1 = Cli.Wrap("proc1").WithArguments(a => a.Add("file_to_input").Add("-"));
var cmd2 = Cli.Wrap("proc2").WithArguments(a => a.Add("-").Add("-"));

await (cmd1 | cmd2).ExecuteAsync();

If you want to then redirect the output to some other stream (eg HTTP stream), then:

var output = new MemoryStream();
var cmd1 = ...;
var cmd2 = ...;

await (cmd1 | cmd2 | output).ExecuteAsync();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM