基于命名管道的全双工通讯实现
关键词:NamedPipeStream
简述:基于控制台应用程序,使用两个管道实现了全双工的进程间通信。其中,客户端和服务器端的管道读线程是新建异步线程实现的,写管道是主线程。本人菜鸟,抛砖引玉,希望能看到单管道实现的全双工通信。
#客户端的实现
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using System.IO;
using System.IO.Pipes;
namespace Named_pipe_client
{
class Program
{
static void Main(string[] args)
{
Thread thd = new Thread(ReadMessage);
thd.Start();
WriteMessage();
System.Environment.Exit(0);
}
private static void ReadMessage(object obj)
{
using (var server = new NamedPipeClientStream(".", "Pipe_Write", PipeDirection.InOut))
{
Console.WriteLine("[Client]:Attemping to connect to Pipe_Write...");
server.Connect();
Console.WriteLine("[Client]:Connected to Pipe_Write");
StreamReader reader = new StreamReader(server);
string message;
while (true)
{
message = reader.ReadLine();
if (message != null)
{
Console.WriteLine("[Server]: " + message);
}
Thread.Sleep(100);
}
}
}
private static void WriteMessage()
{
using (var server = new NamedPipeClientStream(".", "Pipe_Read", PipeDirection.InOut))
{
Console.WriteLine("[Client]:waiting client to connect Pipe_Read...");
server.Connect();
Console.WriteLine("[Client]:Connected to Pipe_Read");
StreamWriter writer = new StreamWriter(server);
writer.AutoFlush = true;
while (true)
{
string input = Console.ReadLine();
if (input.ToUpper() == "EXIT")
{
Console.WriteLine("[Client]: Exiting in 3 second");
try
{
writer.WriteLine("Client is closing...");
server.WaitForPipeDrain();
}
catch (Exception)
{
Console.WriteLine("Sever not connected."); ;
}
Thread.Sleep(3000);
break;
}
writer.WriteLine(input);
server.WaitForPipeDrain();
Thread.Sleep(200);
}
}
return;
}
}
}
服务器端的实现
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using System.IO;
using System.IO.Pipes;
using System.Diagnostics;
namespace NamePipe
{
public class PipeServer
{
public static PipeServer Instance {
get
{
if (instance==null)
{
lock (obj)
{
if (instance==null)
{
instance = new PipeServer();
}
}
}
return instance;
}
}
static void Main(string[] args)
{
Thread thd = new Thread(ReadMessage);
thd.Start();
WriteMessage();
System.Environment.Exit(0);
}
private static void ReadMessage()
{
using (var server = new NamedPipeServerStream("Pipe_Read", PipeDirection.InOut))
{
Console.WriteLine("[Server]:Pipe_Read waiting client to connect...");
server.WaitForConnection();
Console.WriteLine("[Server]:Pipe_Read connected");
StreamReader reader = new StreamReader(server);
string message;
while (true)
{
message = reader.ReadLine();
if (message != null)
{
Console.WriteLine("[Client]: " + message);
}
Thread.Sleep(100);
}
}
}
private static void WriteMessage()
{
using (var server = new NamedPipeServerStream("Pipe_Write", PipeDirection.InOut))
{
Console.WriteLine("[Server]:Pipe_Write waiting client to connect...");
server.WaitForConnection();
Console.WriteLine("[Server]:Pipe_Write connected");
StreamWriter writer = new StreamWriter(server);
writer.AutoFlush = true;
while (true)
{
string input = Console.ReadLine();
if (input.ToUpper() == "EXIT")
{
Console.WriteLine("[Server]: Exiting in 3 second");
try
{
writer.WriteLine("Sever is closing...");
server.WaitForPipeDrain();
}
catch (Exception)
{
Console.WriteLine("Client not connected."); ;
}
Thread.Sleep(3000);
break;
}
writer.WriteLine(input);
server.WaitForPipeDrain();
Thread.Sleep(200);
}
return;
}
}
}
}