c# - 是否有可能在运行过程中出现 "talk"?

标签 c# .net xml sockets

我想创建一些服务,该服务将作为简单进程运行,并为其他应用程序提供向他发送 xml 流的可能性。

我的意思是创建具有无限循环的简单进程 (exe) - 任何应用程序都能够将 XML(文件/流)发送到该进程 => 并且该进程会将 xml 发送到某个套接字。

没有管道可以做到吗? 我想做一些类似 COM 的事情——它可以“捕捉”工作过程的实例。

最佳答案

当然。

您可以在 C# 中使用命名管道类:

服务器:

using (var s = new NamedPipeServerStream ("myPipe"))
{
 s.WaitForConnection();
 s.WriteByte (100);
 Console.WriteLine (s.ReadByte());
}

客户端代码:

using (var s = new NamedPipeClientStream ("myPipe"))
{
 s.Connect();
 Console.WriteLine (s.ReadByte());
 s.WriteByte (200);  
}

编辑

你可以通过文件来完成。 + systemfileWatcher 类

将文件放入文件夹。

其他进程将审核此文件夹。

现在您可以传输信息了。

编辑2

你可以使用memoryMappedFile

并在每个进程中打开一个 View 以查看相同的内存区域 - 并传输数据。 我认为这是最好的。

进程A:

 static void Main(string[] args)
        {
            using (MemoryMappedFile mmf = MemoryMappedFile.CreateNew("testmap", 4000))
            {
                bool mutexCreated;
                Mutex mutex = new Mutex(true, "testmapmutex", out mutexCreated);
                using (MemoryMappedViewStream stream = mmf.CreateViewStream())
                {
                    BinaryWriter writer = new BinaryWriter(stream);
                    string st = "Hellow";
                    int stringSize = Encoding.UTF8.GetByteCount(st); //6
                    writer.Write(st);
                    writer.Write(123); //6+4 bytes = 10 bytes
                }
                mutex.ReleaseMutex();
                Console.WriteLine("Start Process B and press ENTER to continue.");
                Console.ReadLine();
                mutex.WaitOne();
                using (MemoryMappedViewStream stream = mmf.CreateViewStream())
                {
                    BinaryReader reader = new BinaryReader(stream);
                    Console.WriteLine("Process  A  says: {0}", reader.ReadString());
                    Console.WriteLine("Process  A says: {0}", reader.ReadInt32());
                    Console.WriteLine("Process  B says: {0}", reader.ReadInt32());
                }
                mutex.ReleaseMutex();
            }
        }

进程B写入其区域

 static void Main(string[] args)
        {
            try
            {
                using (MemoryMappedFile mmf = MemoryMappedFile.OpenExisting("testmap"))
                {
                    Mutex mutex = Mutex.OpenExisting("testmapmutex");
                    mutex.WaitOne();
                    using (MemoryMappedViewStream stream = mmf.CreateViewStream(11, 0)) // From the 11 byte....
                    {
                        BinaryWriter writer = new BinaryWriter(stream, Encoding.UTF8);
                        writer.Write(2);
                    }
                    mutex.ReleaseMutex();
                }
            }
            catch (FileNotFoundException)
            {
                Console.WriteLine("Memory-mapped file does not exist. Run Process A first.");
            }
        }

关于c# - 是否有可能在运行过程中出现 "talk"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10390829/

相关文章:

c# - NULL值检查

c# - Blazor:IServiceCollection 不包含 AddDefaultIdentity 的定义

.net - XmlSerializer 反序列化时自动检测 XML 类型(.NET 2.0)?

c# - 可空类型和运算符

.net - .NET 4.6.2 中的 WCF 是否支持 CNG KSP 中带有 PK 的服务器证书?

android - KOTLIN-BottomNavigationBar中的第一项不可见,标题问题

javascript - 将 iframe 的内容类型更改为 xml 以显示 xml 并通过 DOM 操作它

java - 为什么我在从第三方 url 读取 xml 时遇到此异常

c# - 使用存储库模式时如何返回可枚举对象?

c# - 有没有一种方法可以简化基于字符串中包含的值的设置?