Код сервера выглядит следующим образом:
Код: Выделить всё
// PipeSecurity ps = new PipeSecurity() and ACL rules here...
NamedPipeServerStream pipeServer = NamedPipeServerStreamAcl.Create("my_pipe", PipeDirection.InOut, 10, PipeTransmissionMode.Message, PipeOptions.Asynchronous, default, default, ps);
StreamReader sr = new StreamReader(pipeServer);
StreamWriter sw = new StreamWriter(pipeServer);
while (true)
{
try
{
// Wait for a connection
pipeServer.WaitForConnection();
// Read the message from the client
string messageFromClient = sr.ReadLine();
Trace.WriteLine($"IPC Message Reveived: {messageFromClient}", "info");
// Reply back to the client
sw.WriteLine("PONG!");
sw.Flush();
pipeServer.WaitForPipeDrain();
pipeServer.Close();
} catch (Exception ex) {
Trace.WriteLine($"ipc_server error: {ex}", "error");
} finally {
pipeServer.WaitForPipeDrain();
if (pipeServer.IsConnected) {
pipeServer.Disconnect();
}
}
Код: Выделить всё
var pipeClient = new NamedPipeClientStream(".",
"my_pipe", PipeDirection.InOut, PipeOptions.None);
try
{
// Connect to pipe
if (pipeClient.IsConnected != true) { pipeClient.Connect(); }
StreamReader sr = new StreamReader(pipeClient);
StreamWriter sw = new StreamWriter(pipeClient);
// Send a message
sw.WriteLine("PING!");
sw.Flush();
pipeClient.WaitForPipeDrain();
// Perhaps my client is closing or not waiting long enough here
// for the server to reply??
// Receive a message
string message_from_server = sr.ReadLine();
Console.WriteLine(message_from_server);
pipeClient.Close();
} catch (Exception ex) {
Console.WriteLine($"Error: {ex}");
}
Код: Выделить всё
info: IPC Message Reveived: PING!
Exception thrown: 'System.ObjectDisposedException' in System.IO.Pipes.dll
An unhandled exception of type 'System.ObjectDisposedException' occurred in System.IO.Pipes.dll
Cannot access a closed pipe.
Любая помощь приветствуется! Также буду рад отзывам о моей обработке ошибок и о том, как сделать сервер неблокирующим, это моя следующая работа.
Подробнее здесь: https://stackoverflow.com/questions/786 ... client-can
Мобильная версия