本文整理匯總了C#中System.IO.Pipes.PipeStream.Read方法的典型用法代碼示例。如果您正苦於以下問題:C# PipeStream.Read方法的具體用法?C# PipeStream.Read怎麽用?C# PipeStream.Read使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。
在下文中一共展示了PipeStream.Read方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。
示例1: Main
//引入命名空間
using System;
using System.IO;
using System.IO.Pipes;
using System.Diagnostics;
class PipeStreamExample
{
private static byte[] matchSign = {9, 0, 9, 0};
public static void Main()
{
string[] args = Environment.GetCommandLineArgs();
if (args.Length < 2)
{
Process clientProcess = new Process();
clientProcess.StartInfo.FileName = Environment.CommandLine;
using (AnonymousPipeServerStream pipeServer =
new AnonymousPipeServerStream(PipeDirection.In,
HandleInheritability.Inheritable))
{
// Pass the client process a handle to the server.
clientProcess.StartInfo.Arguments = pipeServer.GetClientHandleAsString();
clientProcess.StartInfo.UseShellExecute = false;
Console.WriteLine("[SERVER] Starting client process...");
clientProcess.Start();
pipeServer.DisposeLocalCopyOfClientHandle();
try
{
if (WaitForClientSign(pipeServer))
{
Console.WriteLine("[SERVER] Valid sign code received!");
}
else
{
Console.WriteLine("[SERVER] Invalid sign code received!");
}
}
catch (IOException e)
{
Console.WriteLine("[SERVER] Error: {0}", e.Message);
}
}
clientProcess.WaitForExit();
clientProcess.Close();
Console.WriteLine("[SERVER] Client quit. Server terminating.");
}
else
{
using (PipeStream pipeClient = new AnonymousPipeClientStream(PipeDirection.Out, args[1]))
{
try
{
Console.WriteLine("[CLIENT] Sending sign code...");
SendClientSign(pipeClient);
}
catch (IOException e)
{
Console.WriteLine("[CLIENT] Error: {0}", e.Message);
}
}
Console.WriteLine("[CLIENT] Terminating.");
}
}
private static bool WaitForClientSign(PipeStream inStream)
{
byte[] inSign = new byte[matchSign.Length];
int len = inStream.Read(inSign, 0, matchSign.Length);
bool valid = len == matchSign.Length;
while (valid && len-- > 0)
{
valid = valid && (matchSign[len] == inSign[len]);
}
return valid;
}
private static void SendClientSign(PipeStream outStream)
{
outStream.Write(matchSign, 0, matchSign.Length);
}
}