0
点赞
收藏
分享

微信扫一扫

AnonymousPipeStream的使用案例

AnonymousPipeStream的使用具体案例如下:

服务端:

using System;
using System.Data;
using System.Data.SQLite;
using System.IO;
using System.IO.Pipes;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace SupremeConsole
{
class Program
{
static void Main(string[] args)
{
#region 测试 NamedPipeStream
new Thread(new ThreadStart(AnonymousPipeServer)).Start();
#endregion
}

public static void AnonymousPipeServer()
{
string clientExe = @"F:\Person\Longteng\LongtengSln\ConsoleAppTestAnonymousPipe\bin\Debug\ConsoleAppTestAnonymousPipe.exe";
HandleInheritability inherit = HandleInheritability.Inheritable;
using (var tx = new AnonymousPipeServerStream(PipeDirection.Out, inherit))
using (var rx = new AnonymousPipeServerStream(PipeDirection.In, inherit))
{
txID = tx.GetClientHandleAsString();
rxID = rx.GetClientHandleAsString();
var startInfo = new ProcessStartInfo(clientExe, txID + " " + rxID);
startInfo.UseShellExecute = false; // Required for child process
Process p = Process.Start(startInfo);
tx.DisposeLocalCopyOfClientHandle(); // Release unmanaged
rx.DisposeLocalCopyOfClientHandle(); // handle resources.
//tx.WriteByte(100);
//Console.WriteLine("Server received: " + rx.ReadByte());
//p.WaitForExit();
while (true)
{
tx.WriteByte(100);
Console.WriteLine("Server received: " + rx.ReadByte());
}

}
}
}
}

 

客户端(新建一个控制台程序):

using System;
using System.IO.Pipes;
using System.Threading;

namespace ConsoleAppTestAnonymousPipe
{
class Program
{
static void Main(string[] args)
{
string rxID = args[0]; // Note we're reversing the
string txID = args[1]; // receive and transmit roles.
using (var rx = new AnonymousPipeClientStream(PipeDirection.In, rxID))
using (var tx = new AnonymousPipeClientStream(PipeDirection.Out, txID))
{
//Console.WriteLine("Client received: " + rx.ReadByte());
//tx.WriteByte(200);
while (true)
{
Console.WriteLine("Client received: " + rx.ReadByte());
tx.WriteByte(200);
Thread.Sleep(TimeSpan.FromSeconds(1));
}
}
}
}
}

 

运行结果:

AnonymousPipeStream的使用案例_sqlite

 

龙腾一族至尊龙骑

举报

相关推荐

0 条评论