gpt4 book ai didi

c# - NamedPipeServerStream.BeginWaitForConnection 失败,出现 System.IO.Exception : The pipe is being closed

转载 作者:行者123 更新时间:2023-11-30 16:49:39 59 4
gpt4 key购买 nike

我用 C# 编写了一个简单的异步 NamedPipeStreamServer 进程,其核心是:

public void Listen()
{
bool shuttingDown = false;
while (!shuttingDown)
{
NamedPipeServerStream serverStream =
new NamedPipeServerStream(
"bobasdfpipe",
PipeDirection.InOut,
254,
PipeTransmissionMode.Message,
PipeOptions.Asynchronous);

IAsyncResult connectionResult =
serverStream.BeginWaitForConnection(
this.HandleConnection,
serverStream);

int waitResult =
WaitHandle.WaitAny(
new[]
{
this.ShutdownEvent,
connectionResult.AsyncWaitHandle
});
switch (waitResult)
{
case 0:
// this.ShutdownEvent
shuttingDown = true;
break;

case 1:
// connectionResult.AsyncWaitHandle
serverStream.EndWaitForConnection(connectionResult);
break;
}
}
}

我也为它写了一个简单的客户端。客户端(不是异步的)只是打开管道然后退出:

static void Main(string[] args)
{
using (
NamedPipeClientStream clientStream =
new NamedPipeClientStream(
".",
"bobasdfpipe",
PipeDirection.InOut))
{
clientStream.Connect();
}
}

如果我启动服务器,然后启动一个或多个客户端,似乎一切正常。

如果我在没有启动服务器的情况下启动客户端,客户端会挂起 Connect() 调用,直到我启动服务器,但是当我启动服务器时,服务器崩溃并出现 System.IO.Exception BeginWaitForConnection() 调用,提示“管道正在关闭。”

我发现其他人在 BeginWaitForConnection() 上遇到“管道正在关闭”错误,但它们都是由于尝试对同一个 NamedPipeServerStream 实例进行第二次 BeginWaitForConnection() 调用而引起的。这不是这里发生的事情 - 我为每个 BeginWaitForConnection() 调用创建了一个不同的 NamedPipeServerStream 实例,即使我没有这样做,它还是在第一个 BeginWaitForConnection() 调用上失败了。

我做错了什么吗?还是这只是正常现象 - 等待服务器启动的命名管道客户端会在服务器的第一个 BeginWaitForConnection() 调用中导致“管道正在关闭”?

我注意到,如果我再试一次 - 即吸收异常并执行另一个 BeginWaitForConnection() - 然后我会为每个一直在等待服务器启动的客户端得到一个这样的异常,但是在处理完所有那些,此后服务器似乎可以正常工作。

编辑:这是 HandleConnection 方法,但我认为它甚至没有命中这段代码:

private void HandleConnection(IAsyncResult iar)
{
NamedPipeServerStream serverStream =
(NamedPipeServerStream)iar.AsyncState;

Log.Info("So, that happened.");
Thread.Sleep(1000);
Log.Info("Giving up.");
}

最佳答案

看起来客户端在服务器完全处理之前关闭了连接。发生这种情况是因为 clientStream.Dispose()clientStream.Connect() 之后被调用,即将建立的连接被终止。提示:尝试在 clientStream.Connect() 之后添加 Thread.Sleep(100)

Am I doing something wrong? Or is this just normal - a named pipe client that is waiting for the server to come up will cause "The pipe is being closed" on the server's first BeginWaitForConnection() call?

无论客户端做什么,服务器代码都应该能够通过捕获 IOException 丢弃服务器的管道句柄来优雅地处理这一系列事件。

   NamedPipeServerStream serverStream =
new NamedPipeServerStream(
"bobasdfpipe",
PipeDirection.InOut,
254,
PipeTransmissionMode.Message,
PipeOptions.Asynchronous);

try
{
IAsyncResult connectionResult = serverStream.BeginWaitForConnection(
this.HandleConnection,
serverStream);


int waitResult =
WaitHandle.WaitAny(
new[]
{
this.ShutdownEvent,
connectionResult.AsyncWaitHandle
});
switch (waitResult)
{
case 0:
// this.ShutdownEvent
shuttingDown = true;
break;

case 1:
// connectionResult.AsyncWaitHandle
serverStream.EndWaitForConnection(connectionResult);
break;
}

}
catch(IOException)
{
// Connection terminated by client, close server pipe's handle
serverStream.Close();
continue;
}

关于c# - NamedPipeServerStream.BeginWaitForConnection 失败,出现 System.IO.Exception : The pipe is being closed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36314004/

59 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com