控制台应用程序相互通信的推荐方法是什么?

时间:2012-11-18 07:00:58

标签: c# console-application msmq

我有一个用不同机器上运行的C#编写的控制台应用程序系统。我使用MSMQ。

我的问题是,如何让我的控制台应用程序相互通信?

我问,因为我想创建一个新的控制台应用程序,可以被其他应用程序查询,以了解MSMQ中的消息数量。

编辑1: 感谢您的回复和评论! 关于要求,我估计可能大约10-50 /秒的查询

3 个答案:

答案 0 :(得分:2)

您需要使用管道来实现此目的:请参阅Named pipe and anonymous pipe

管道的工作原理如下: 主机应用程序(启动控制台应用程序的应用程序)必须创建一个管道来捕获concole应用程序的std输出,另一个应用程序写入控制台应用程序的std输入。

我给你的链接中有很多代码示例

这里还有一个代码示例(使用现有的管道StandardInput和StandardOutput):它启动一个cmd.exe控制台,但它是一个层:而不是实际的控制台......看看你自己:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.IO;
using System.Threading;

namespace ConsoleApplication1
{
    class Program
    {
        private static StreamReader reader;

        static void Main(string[] args)
        {
            Process cmd = new Process();

            cmd.StartInfo.FileName = "cmd.exe";
            cmd.StartInfo.RedirectStandardInput = true;
            cmd.StartInfo.RedirectStandardOutput = true;
            cmd.StartInfo.UseShellExecute = false;

            cmd.Start();

            reader = cmd.StandardOutput;
            StreamWriter writer = cmd.StandardInput;


            Thread t = new Thread(new ThreadStart(writingThread));

            t.Start();

            //Just write everything we type to the cmd.exe process
            while (true) writer.Write((char)Console.Read());

        }

        public static void writingThread()
        {
            //Just write everything cmd.exe writes back to our console
            while (true) Console.Write((char)reader.Read());
        }
    }
}

您可以通过端口连接(套接字)缓冲区替换StreamReader和StreamWriter来实现相同的结果,以便在两个进程之间进行远程通信 http://msdn.microsoft.com/en-us/library/system.net.sockets.socket.aspx

建议保护通信以最大限度地减少入侵可能性

以下是通过套接字进行通信的示例...现在这是在一个程序中运行但是所有内容都在不同的线程上,如果通信是在两台独立的机器上,它将起作用:服务器是控制它的人进程cmd.exe,客户端是查看器/编写器

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.IO;
using System.Threading;
using System.Net.Sockets;
using System.Net;

namespace ConsoleApplication1
{
    class Program
    {
        private static StreamReader reader;
        private static StreamWriter writer;

        static void Main(string[] args)
        {
            //Server initialisation 

            Process cmdServer = new Process();

            cmdServer.StartInfo.FileName = "cmd.exe";
            cmdServer.StartInfo.RedirectStandardInput = true;
            cmdServer.StartInfo.RedirectStandardOutput = true;
            cmdServer.StartInfo.UseShellExecute = false;

            cmdServer.Start();

            reader = cmdServer.StandardOutput;
            writer = cmdServer.StandardInput;


            Thread t1 = new Thread(new ThreadStart(clientListener));
            t1.Start();
            Thread t2 = new Thread(new ThreadStart(clientWriter));
            t2.Start();
            Thread t3 = new Thread(new ThreadStart(serverListener));
            t3.Start();
            Thread t4 = new Thread(new ThreadStart(serverWriter));
            t4.Start();


        }

        public static void clientWriter()
        {
            Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            Int32 size = 0;
            Byte[] buff = new Byte[1024];

            sock.Connect(IPAddress.Parse("127.0.0.1"), 4357);
            Console.WriteLine("clientWriter connected");

            while (true)
            {
                String line = Console.ReadLine();
                line += "\n";
                Char[] chars = line.ToArray();
                size = chars.Length;
                if (size > 0)
                {
                    buff = Encoding.Default.GetBytes(chars);
                    //Console.WriteLine("Sending \"" + new String(chars, 0, size) + "\"");
                    sock.Send(buff, size, 0);
                }
            }
        }

        /// <summary>
        /// Local client that listens to what the server sends on port 4356
        /// </summary>
        public static void clientListener()
        {
            Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            sock.Bind(new IPEndPoint(IPAddress.Any, 4356));
            sock.Listen(0);

            Int32 size = 0;
            Byte[] buff = new Byte[1024];
            Char[] cbuff = new Char[1024];

            while (true)
            {
                Console.WriteLine("clientListener Waiting for connection");
                sock = sock.Accept();
                Console.WriteLine("clientListener Connection accepted " + ((sock.Connected)?"Connected" : "Not Connected"));

                while ((size = sock.Receive(buff)) > 0)
                {
                    for (int i = 0; i < size; i++) cbuff[i] = (Char)buff[i];
                    Console.Write(cbuff, 0, size);
                }

                sock.Close();

            }
        }

        /// <summary>
        /// Remote server that listens to what the client sends on port 4357
        /// </summary>
        public static void serverListener()
        {
            Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            sock.Bind(new IPEndPoint(IPAddress.Any, 4357));
            sock.Listen(0);

            Int32 size = 0;
            Byte[] buff = new Byte[1024];
            Char[] cbuff = new Char[1024];

            while (true)
            {
                Console.WriteLine("serverListener Waiting for connection");
                sock = sock.Accept();
                Console.WriteLine("serverListener Connection accepted " + ((sock.Connected) ? "Connected" : "Not Connected"));

                while ((size = sock.Receive(buff)) > 0)
                {
                    for (int i = 0; i < size; i++) cbuff[i] = (Char)buff[i];
                    //Console.WriteLine("Received \"" + new String(cbuff,0,size) + "\"");
                    writer.Write(cbuff, 0, size);
                }

            }
        }

        /// <summary>
        /// Remote server that writes the output of the colsole application through the socket
        /// </summary>
        public static void serverWriter()
        {
            Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            Int32 size = 0;
            Byte[] buff = new Byte[1024];
            Char[] cbuff = new Char[1024];

            sock.Connect(IPAddress.Parse("127.0.0.1"), 4356);

            Console.WriteLine("serverWriter connected");

            while (true)
            {
                size = reader.Read(cbuff, 0, 1024);
                if (size > 0)
                {
                    for (int i = 0; i < size; i++) buff[i] = (Byte)cbuff[i];
                    sock.Send(buff, 0, size, 0);
                }
            }
        }
    }
}

答案 1 :(得分:1)

可测试且可扩展 - 可以轻松更改为在计算机之间工作。

你有几个方法。使用WCF可以轻松实现“命名管道”。它很好,因为它非常可测试。

最佳性能您可以使用内存映射文件http://weblogs.asp.net/gunnarpeipman/archive/2009/06/21/net-framework-4-0-using-memory-mapped-files.aspx

他们有令人惊叹的表现。但是更难以测试。

最简单 - 如果您的流量非常低,则可以使用注册表。 C#中的Regisrty类是线程安全且快速的。

答案 2 :(得分:1)

WCF。 WCF,WCF,WCF,WCF。

不要费心重新发明轮子,只需使用WCF进行通信,让它担心细节(无论是使用管道还是其他机制)。