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

编程入门 行业动态 更新时间:2024-10-28 07:30:04
本文介绍了控制台应用程序相互通信的推荐方式是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个用C#编写的控制台应用程序系统,在不同的计算机上运行.我使用MSMQ.

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?

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

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.

感谢您的回复和评论! 关于需求,我估计大约每秒10-50次查询

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

推荐答案

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

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

管道的工作方式如下: 主机应用程序(一个启动控制台应用程序)必须创建一个管道来捕获conconle应用程序的std输出,而另一个则要写入控制台应用程序的std输入.

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

这也是一个代码示例(使用现有管道StandardInput和StandardOutput):它启动了cmd.exe控制台,但它是一个覆盖层:不是实际的控制台...亲自查看:

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

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

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 msdn.microsoft/en-us/library/system.sockets.socket.aspx

建议确保通信安全,以最大程度地减少入侵可能性

It is recommended to secure the communication to minimize intrusion possibility

这里是通过套接字进行通信的示例...现在,该通信在一个程序中运行,但是所有内容都在单独的线程上,并且如果通信要在两台单独的机器上运行,则可以正常工作:服务器是控制套接字的服务器.处理cmd.exe,并且客户端是查看器/编写器

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

更多推荐

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

本文发布于:2023-07-27 13:46:44,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1222706.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:控制台   应用程序   通信   方式

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!