我有以下客户端代码,我将内容写入Server
管道,我能够在Server
端读取它,但在我回复之前,客户端已经尝试过读取仍然空的管道。你如何wait
NamedPipeClientStream
?
using (NamedPipeClientStream pipe = new NamedPipeClientStream(".", pipename, PipeDirection.InOut))
{
pipe.Connect(5000);
pipe.ReadMode = PipeTransmissionMode.Byte;
byte[] ba = Encoding.Default.GetBytes("hello world");
pipe.Write(ba, 0, ba.Length);
var result = await Task.Run(() => {
// this would return as soon as Server finished reading
// but then server hasn't wrote anything back yet
pipe.WaitForPipeDrain();
// sample code on how i am planning to read, not tested,
// since the pipe is still empty at this point
using (StreamReader reader = new StreamReader(pipe))
using (MemoryStream ms = new MemoryStream())
{
reader.BaseStream.CopyTo(ms);
return Encoding.Default.GetString(ms.ToArray());
}
});
return result;
}
我不认为我应该使用WaitForPipeDrain
,但是我没有其他选择让我等待,或者知道何时可以阅读?有很多例子,但没有一个显示客户端等待响应的正确方法。
Microsoft显示的示例似乎正在使用ReadLine()
并在发送字符串数据时利用EOL
字符,但我正在处理byte[]
数据("你好世界"只是得到一些字节)。
答案 0 :(得分:1)
您无需等待数据。 NamedPipeClientStream
表示字节流(它来自System.IO.Stream
),如果当前没有数据可用,则从管道(或来自包裹该管道的StreamReader
)读取将直接阻塞,直到数据到达
要传输文字数据,使用StreamReader.ReadLine()
阅读并使用StreamWriter.WriteLine()
撰写文章可以正常使用。要传输二进制数据,您可以将二进制数据编码为文本形式(例如使用base64编码)并继续使用StreamReader.ReadLine()
/ StreamWriter.WriteLine()
。或者您可以将服务器和客户端管道设置为PipeStream.TransmissionMode到Message
模式,并将每个字节数组作为单个消息传输,如下所示(为简洁起见,省略了错误检查):
class Client
{
static async Task Main(string[] args)
{
using (NamedPipeClientStream pipe = new NamedPipeClientStream(".", "testpipe", PipeDirection.InOut))
{
pipe.Connect(5000);
pipe.ReadMode = PipeTransmissionMode.Message;
byte[] ba = Encoding.Default.GetBytes("hello world");
pipe.Write(ba, 0, ba.Length);
var result = await Task.Run(() => {
return ReadMessage(pipe);
});
Console.WriteLine("Response received from server: " + Encoding.UTF8.GetString(result));
Console.ReadLine();
}
}
private static byte[] ReadMessage(PipeStream pipe)
{
byte[] buffer = new byte[1024];
using (var ms = new MemoryStream())
{
do
{
var readBytes = pipe.Read(buffer, 0, buffer.Length);
ms.Write(buffer, 0, readBytes);
}
while (!pipe.IsMessageComplete);
return ms.ToArray();
}
}
}
class Server
{
static void Main(string[] args)
{
using (NamedPipeServerStream pipeServer = new NamedPipeServerStream(
"testpipe",
PipeDirection.InOut,
NamedPipeServerStream.MaxAllowedServerInstances,
PipeTransmissionMode.Message))//Set TransmissionMode to Message
{
// Wait for a client to connect
Console.Write("Waiting for client connection...");
pipeServer.WaitForConnection();
Console.WriteLine("Client connected.");
//receive message from client
var messageBytes = ReadMessage(pipeServer);
Console.WriteLine("Message received from client: " + Encoding.UTF8.GetString(messageBytes));
//prepare some response
var response = Encoding.UTF8.GetBytes("Hallo from server!");
//send response to a client
pipeServer.Write(response, 0, response.Length);
Console.ReadLine();
}
}
private static byte[] ReadMessage(PipeStream pipe)
{
byte[] buffer = new byte[1024];
using (var ms = new MemoryStream())
{
do
{
var readBytes = pipe.Read(buffer, 0, buffer.Length);
ms.Write(buffer, 0, readBytes);
}
while (!pipe.IsMessageComplete);
return ms.ToArray();
}
}
}