2

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

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

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

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

4

3 回答 3

2

您需要使用管道来实现此目的:请参阅命名管道和匿名管道

管道的工作原理是这样的:宿主应用程序(启动控制台应用程序的那个)必须创建一个管道来捕获 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);
                }
            }
        }
    }
}
于 2012-11-18T07:05:57.433 回答
1

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

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

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

他们有惊人的表现。但更难测试。

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

于 2012-11-18T07:30:36.853 回答
1

WCF。WCF,WCF,WCF,WCF。

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

于 2012-11-18T07:55:29.713 回答