What's the recommended way for console applications to talk with each other?

只谈情不闲聊 提交于 2020-01-24 14:14:47

问题


I got a system of console applications written in C# running on different machines. I use MSMQ.

My questions is, how do I make my console applications communicate to each other?

I'm asking because I want to create a new console application that can be queried by other applications to know the number of messages in MSMQ.

Edit 1: Thanks for the replies and comments! About the requirements, I'm estimating maybe about 10-50/sec of queries


回答1:


You need to use a pipe to achieve this: see Named pipe and anonymous pipe

A pipe works like this: The host application (the one launching the console application) has to create a pipe to catch the concole application's std output and an other one to write to the console application's std input.

There is plenty of code example in the link I gave you

Also here is a code example (using existing pipes StandardInput and StandardOutput): it starts a cmd.exe console but it is a layer over: not the actual console...see for yourself:

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());
        }
    }
}

You can achieve the same result by replacing the StreamReader and the StreamWriter by a port connection (socket) buffer for remote communication between two process http://msdn.microsoft.com/en-us/library/system.net.sockets.socket.aspx

It is recommended to secure the communication to minimize intrusion possibility

Here is an example of communication through a sockets...now this is running in one program but everything is on separate threads and it would work if the communication was to be on two separate machines: the server is the one controlling the process cmd.exe and the client is a viewer/writer

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);
                }
            }
        }
    }
}



回答2:


testable and extendable - can be easily changed to work between computers.

you have a few methods. "named pipes" can be achieved easily using WCF. its nice because its very testable.

best performance you can use memory mapped files http://weblogs.asp.net/gunnarpeipman/archive/2009/06/21/net-framework-4-0-using-memory-mapped-files.aspx

they have AMAZING performance. BUT less harder to test.

easiest - if you have very low traffic you can use the registry. theres a Regisrty class in C# its threadsafe and fast.




回答3:


WCF. WCF, WCF, WCF, WCF.

Don't bother reinventing the wheel, just use WCF for communications, and let it worry about the details (whether using a pipe or some other mechanism).



来源:https://stackoverflow.com/questions/13438154/whats-the-recommended-way-for-console-applications-to-talk-with-each-other

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!