如何:使用匿名管道在本地进程之间进行通信(C#)
2011-11-24 15:06
573 查看
匿名管道提供的功能比命名管道少,但它需要的系统开销也少。
您可以使用匿名管道更加轻松地在本地计算机上进行进程间通信。
不能使用匿名管道通过网络进行通信。
示例
下面的示例演示使用匿名管道将字符串从父进程发送到子进程的方式。
此示例使用
Out
的
PipeDirection
值在父进程中创建一个
AnonymousPipeServerStream
对象。
然后,父进程通过使用客户端句柄创建一个
AnonymousPipeClientStream
对象来创建一个子进程。
该子进程的
In
值为
PipeDirection。
然后,父进程将用户提供的字符串发送给子进程。
该字符串将显示在子进程中的控制台上。
下面的示例演示服务器进程。
using System;
using System.IO;
using System.IO.Pipes;
using System.Diagnostics;
class PipeServer
{
static
void Main()
{
Process pipeClient = new Process();
pipeClient.StartInfo.FileName = "pipeClient.exe";
using (AnonymousPipeServerStream pipeServer =
new AnonymousPipeServerStream(PipeDirection.Out,
HandleInheritability.Inheritable))
{
// Show that anonymous pipes do not support Message mode.
try
{
Console.WriteLine("[SERVER] Setting ReadMode to \"Message\".");
pipeServer.ReadMode = PipeTransmissionMode.Message;
}
catch (NotSupportedException e)
{
Console.WriteLine("[SERVER] Exception:\n
{0}", e.Message);
}
Console.WriteLine("[SERVER] Current TransmissionMode: {0}.",
pipeServer.TransmissionMode);
// Pass the client process a handle to the server.
pipeClient.StartInfo.Arguments =
pipeServer.GetClientHandleAsString();
pipeClient.StartInfo.UseShellExecute = false;
pipeClient.Start();
pipeServer.DisposeLocalCopyOfClientHandle();
try
{
// Read user input and send that to the client process.
using (StreamWriter sw =
new StreamWriter(pipeServer))
{
sw.AutoFlush = true;
// Send a 'sync message' and wait for client to receive it.
sw.WriteLine("SYNC");
pipeServer.WaitForPipeDrain();
// Send the console input to the client process.
Console.Write("[SERVER] Enter text: ");
sw.WriteLine(Console.ReadLine());
}
}
// Catch the IOException that is raised if the pipe is broken
// or disconnected.
catch (IOException e)
{
Console.WriteLine("[SERVER] Error: {0}", e.Message);
}
}
pipeClient.WaitForExit();
pipeClient.Close();
Console.WriteLine("[SERVER] Client quit. Server terminating.");
}
}
下面的示例演示客户端进程。
服务器进程启动客户端进程,并为该进程提供一个客户端句柄。
应该将从客户端代码得到的可执行文件命名为
pipeClient.exe
并在运行该服务器进程之前将其复制到服务器可执行文件所在的目录中。
using System;
using System.IO;
using System.IO.Pipes;
class PipeClient
{
static
void Main(string[]
args)
{
if (args.Length > 0)
{
using (PipeStream pipeClient =
new AnonymousPipeClientStream(PipeDirection.In, args[0]))
{
// Show that anonymous Pipes do not support Message mode.
try
{
Console.WriteLine("[CLIENT] Setting ReadMode to \"Message\".");
pipeClient.ReadMode = PipeTransmissionMode.Message;
}
catch (NotSupportedException e)
{
Console.WriteLine("[CLIENT] Execption:\n
{0}", e.Message);
}
Console.WriteLine("[CLIENT] Current TransmissionMode: {0}.",
pipeClient.TransmissionMode);
using (StreamReader sr =
new StreamReader(pipeClient))
{
// Display the read text to the console
string temp;
// Wait for 'sync message' from the server.
do
{
Console.WriteLine("[CLIENT] Wait for sync...");
temp = sr.ReadLine();
}
while (!temp.StartsWith("SYNC"));
// Read the server data and echo to the console.
while ((temp = sr.ReadLine()) !=
null)
{
Console.WriteLine("[CLIENT] Echo: " + temp);
}
}
}
}
Console.Write("[CLIENT] Press Enter to continue...");
Console.ReadLine();
}
}
您可以使用匿名管道更加轻松地在本地计算机上进行进程间通信。
不能使用匿名管道通过网络进行通信。
示例
下面的示例演示使用匿名管道将字符串从父进程发送到子进程的方式。
此示例使用
Out
的
PipeDirection
值在父进程中创建一个
AnonymousPipeServerStream
对象。
然后,父进程通过使用客户端句柄创建一个
AnonymousPipeClientStream
对象来创建一个子进程。
该子进程的
In
值为
PipeDirection。
然后,父进程将用户提供的字符串发送给子进程。
该字符串将显示在子进程中的控制台上。
下面的示例演示服务器进程。
using System;
using System.IO;
using System.IO.Pipes;
using System.Diagnostics;
class PipeServer
{
static
void Main()
{
Process pipeClient = new Process();
pipeClient.StartInfo.FileName = "pipeClient.exe";
using (AnonymousPipeServerStream pipeServer =
new AnonymousPipeServerStream(PipeDirection.Out,
HandleInheritability.Inheritable))
{
// Show that anonymous pipes do not support Message mode.
try
{
Console.WriteLine("[SERVER] Setting ReadMode to \"Message\".");
pipeServer.ReadMode = PipeTransmissionMode.Message;
}
catch (NotSupportedException e)
{
Console.WriteLine("[SERVER] Exception:\n
{0}", e.Message);
}
Console.WriteLine("[SERVER] Current TransmissionMode: {0}.",
pipeServer.TransmissionMode);
// Pass the client process a handle to the server.
pipeClient.StartInfo.Arguments =
pipeServer.GetClientHandleAsString();
pipeClient.StartInfo.UseShellExecute = false;
pipeClient.Start();
pipeServer.DisposeLocalCopyOfClientHandle();
try
{
// Read user input and send that to the client process.
using (StreamWriter sw =
new StreamWriter(pipeServer))
{
sw.AutoFlush = true;
// Send a 'sync message' and wait for client to receive it.
sw.WriteLine("SYNC");
pipeServer.WaitForPipeDrain();
// Send the console input to the client process.
Console.Write("[SERVER] Enter text: ");
sw.WriteLine(Console.ReadLine());
}
}
// Catch the IOException that is raised if the pipe is broken
// or disconnected.
catch (IOException e)
{
Console.WriteLine("[SERVER] Error: {0}", e.Message);
}
}
pipeClient.WaitForExit();
pipeClient.Close();
Console.WriteLine("[SERVER] Client quit. Server terminating.");
}
}
下面的示例演示客户端进程。
服务器进程启动客户端进程,并为该进程提供一个客户端句柄。
应该将从客户端代码得到的可执行文件命名为
pipeClient.exe
并在运行该服务器进程之前将其复制到服务器可执行文件所在的目录中。
using System;
using System.IO;
using System.IO.Pipes;
class PipeClient
{
static
void Main(string[]
args)
{
if (args.Length > 0)
{
using (PipeStream pipeClient =
new AnonymousPipeClientStream(PipeDirection.In, args[0]))
{
// Show that anonymous Pipes do not support Message mode.
try
{
Console.WriteLine("[CLIENT] Setting ReadMode to \"Message\".");
pipeClient.ReadMode = PipeTransmissionMode.Message;
}
catch (NotSupportedException e)
{
Console.WriteLine("[CLIENT] Execption:\n
{0}", e.Message);
}
Console.WriteLine("[CLIENT] Current TransmissionMode: {0}.",
pipeClient.TransmissionMode);
using (StreamReader sr =
new StreamReader(pipeClient))
{
// Display the read text to the console
string temp;
// Wait for 'sync message' from the server.
do
{
Console.WriteLine("[CLIENT] Wait for sync...");
temp = sr.ReadLine();
}
while (!temp.StartsWith("SYNC"));
// Read the server data and echo to the console.
while ((temp = sr.ReadLine()) !=
null)
{
Console.WriteLine("[CLIENT] Echo: " + temp);
}
}
}
}
Console.Write("[CLIENT] Press Enter to continue...");
Console.ReadLine();
}
}
相关文章推荐
- C#使用匿名管道在本地进程之间进行通信
- C#使用命名管道通过网络在进程之间进行通信
- 使用命名管道通过网络在进程之间进行通信(C#)
- [ZZ]使用命名管道通过网络在进程之间进行通信
- linux进程通信之使用匿名管道进行父子进程通信
- 使用命名管道通过网络在进程之间进行通信
- 使用管道实现两个进程之间的通信
- C#中使用SendMessage进行进程通信的实例
- Linux下的有名管道(06)---使用两个管道实现两个进程之间的通信(手机模式)
- [Linux管道和IPC]使用信号量和共享内存进行父子进程通信
- Linux下的有名管道(05)---使用两个管道实现两个进程之间的通信(对讲机模式)
- Linux c 匿名管道实例—创建两个子进程与父进程进行管道通信
- C#中使用SendMessage进行进程通信的实例
- C++和C#进程之间通过命名管道通信(上)
- System.IO系列:局域网内多线程使用命名管道在进程之间通信实例
- Linux下进程的创建及其使用管道进行进程之间的通讯[00原创]
- 关于使用UDP套接字进行本地进程通信
- C++和C#进程之间通过命名管道通信(附源码)—下
- 匿名管道实现父子进程之间的通信....
- C++和C#进程之间通过命名管道通信(上)