I have two processes ( both .NET Framework
applications) and i am trying to communicate using Named Pipes
The client connects to the pipe , but when it tries to ReadAsync
the message sent by the server, it starts waiting indifinetly , even though the server has already sent a message !!
What surprises me is that if i close the Server
app the Client
finally continues to the next line of code (after the ReadAsync
line), having read 0 bytes.
Is there anything i must to on the server after WriteAsync
-ing the payload? Do i need to flush or anything ?
Server
static async Task Main(string[] args) {
var server = new NamedPipeServerStream(
"somePipe",
PipeDirection.InOut,
2,
PipeTransmissionMode.Message
);
await server.WaitForConnectionAsync();
using (StreamReader reader = new StreamReader(server)) {
using (StreamWriter writer = new StreamWriter(server)) {
await writer.WriteAsync("Hello from server");
char[] buffer = new char[20];
var read = await reader.ReadAsync(buffer, 0, buffer.Length);
}
}
}
Client
static async Task Main() {
NamedPipeClientStream client = new NamedPipeClientStream(
".",
"somePipe",
PipeDirection.InOut,
PipeOptions.Asynchronous);
await client.ConnectAsync();
try {
using (StreamReader reader = new StreamReader(client)) {
using (StreamWriter writer = new StreamWriter(client)) {
var buffer = new char[20];
int readChars = await reader.ReadAsync(buffer, 0,buffer.Length); //starts waiting indifinetly , until i close the Server
await writer.WriteAsync("From Client");
}
}
} catch (Exception ex) {
throw;
}
}
Update
It seems using the NamedPipeServerStream
directly to write instead of the StreamWriter
on the server side makes the client get the data !
Server Changes
byte[] data=Encoding.UTF8.GetBytes("Hello from server");
server.WriteAsync(data,0,data.Length);
P.S
However using again the server
to ReadAsync
blocks the server.So there is a problem when wrapping the NamedPipeServerStream
and ClientStream
into StreamReader
and StreamWriter
.