C# NamedPipeServerStream.BeginWaitForConnection因System.IO失败。异常:管道正在关闭
我用C#编写了一个简单的异步命名的PipeStreamServer进程,其核心是:C# NamedPipeServerStream.BeginWaitForConnection因System.IO失败。异常:管道正在关闭,c#,.net,asynchronous,named-pipes,namedpipeserverstream,C#,.net,Asynchronous,Named Pipes,Namedpipeserverstream,我用C#编写了一个简单的异步命名的PipeStreamServer进程,其核心是: public void Listen() { bool shuttingDown = false; while (!shuttingDown) { NamedPipeServerStream serverStream = new NamedPipeServerStream( "bobasdfpipe",
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()调用,直到我启动服务器,但当我启动服务器时,服务器会在BeginWaitForConnection()调用上出现System.IO.Exception,并说“管道正在关闭”
我发现其他人在BeginWaitForConnection()上出现“管道正在关闭”错误,但这些错误都是由于尝试对相同名称的PipeServerStream实例进行第二次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.Connect()
之后立即调用了clientStream.Dispose()
,并且即将建立的连接被终止。
提示:尝试在clientStream.Connect()之后添加Thread.Sleep(100)
我做错什么了吗?或者这只是一个普通的命名管道
等待服务器启动的客户端将导致“管道中断”
是否在服务器的第一个BeginWaitForConnection()调用中“正在关闭”
无论客户端做什么,服务器代码都应该能够通过捕获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;
}
你能展示一下手的连接方式吗?如果在调用ConnectNamedPipe()之前客户端请求了连接,我想您需要设置事件(您等待的AsyncWaitHandle)。alexm,当然,我已经在问题的末尾添加了它。不过,我不认为它一开始就已经达到了这个标准。