标准输入和stdout C#双向IPC [英] C# bi-directional IPC over stdin and stdout

查看:235
本文介绍了标准输入和stdout C#双向IPC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎么可以连接两个C#程序,以便能够在标准输入和标准输出相互通信



这样的:



工艺A - >标准输出A - >标准输入b ---电>进程b



工艺A< - 标准输入A< - 标准输出b< ---进程b


解决方案

 使用系统;使用System.Diagnostics程序
;

类节目
{
静态无效的主要(字串[] args)
{
字符串名称;
如果(args.Length大于0和放大器;&安培; ARGS [0] ==奴隶)
{
NAME =奴隶;
}
,否则
{
NAME =主人;
VAR信息=新的ProcessStartInfo();
info.FileName =BidirConsole.exe;
info.Arguments =奴隶;
info.RedirectStandardInput = TRUE;
info.RedirectStandardOutput = TRUE;
info.UseShellExecute = FALSE;
VAR等=的Process.Start(信息);
Console.SetIn(other.StandardOutput);
Console.SetOut(other.StandardInput);
}
Console.WriteLine(名称+开始了。);
,而(真)
{
变种传入=到Console.ReadLine();
变种传出=名+了:+传入;
Console.WriteLine(拨出);
System.Threading.Thread.Sleep(100);
}
}
}


How can I connect two C# processes so they can communicate with each other over stdin and stdout?

Like this:

Process A --> stdout A --> stdin B ---> Process B

Process A <-- stdin A <-- stdout B <--- Process B

解决方案

using System;
using System.Diagnostics;

class Program
{
  static void Main(string[] args)
  {
    string name;
    if (args.Length > 0 && args[0] == "slave")
    {
      name = "slave";
    }
    else
    {
      name = "master";
      var info = new ProcessStartInfo();
      info.FileName = "BidirConsole.exe";
      info.Arguments = "slave";
      info.RedirectStandardInput = true;
      info.RedirectStandardOutput = true;
      info.UseShellExecute = false;
      var other = Process.Start(info);
      Console.SetIn(other.StandardOutput);
      Console.SetOut(other.StandardInput);
    }
    Console.WriteLine(name + " started.");
    while (true)
    {
      var incoming = Console.ReadLine();
      var outgoing = name + " got : " + incoming;
      Console.WriteLine(outgoing);
      System.Threading.Thread.Sleep(100);
    }
  }
}

这篇关于标准输入和stdout C#双向IPC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆