【发布时间】:2011-03-31 05:41:38
【问题描述】:
我正在设置一个命名管道服务器,并从服务器管道中读取数据...我允许 5 个可能的并发连接,它们是这样设置的。
Public Sub Start()
Dim i As Integer
While i < mTotalServers
Dim s As New IO.Pipes.NamedPipeServerStream("IPSClient", IO.Pipes.PipeDirection.InOut,
mTotalServers, IO.Pipes.PipeTransmissionMode.Byte, IO.Pipes.PipeOptions.Asynchronous)
i += 1
Dim p As New PipeConnection(s, "Index - " & i)
mServers.Add(p)
s.BeginWaitForConnection(New AsyncCallback(AddressOf ConnectionReceived), p)
End While
End Sub
然后我恢复操作直到收到连接。
Private Sub ConnectionReceived(ar As IAsyncResult)
Try
Dim p As PipeConnection = Nothing
If ar.IsCompleted Then
Diagnostics.Debug.Print("Connection received")
p = CType(ar.AsyncState, PipeConnection)
Dim s As IO.Pipes.NamedPipeServerStream = p.Stream
s.EndWaitForConnection(ar)
Dim conn As Connection = New Connection(p)
While mRunning AndAlso p.Stream.IsConnected
If p.ReadHandle.WaitOne(100) Then
Debug.Print("Set")
Else
'
End If
End While
If mRunning Then
s.BeginWaitForConnection(New AsyncCallback(AddressOf ConnectionReceived), p)
End If
Else
p.Stream.Close()
p.Stream.Dispose()
End If
Catch ex As ObjectDisposedException
' Diagnostics.Debug.Print(ex.ToString)
Catch ex As OperationCanceledException
Diagnostics.Debug.Print(ex.ToString)
Catch ex As IO.IOException
Diagnostics.Debug.Print(ex.ToString)
End Try
End Sub
一旦管道的客户端断开连接,我希望管道可以重复使用。
我在 mRunning 和连接时循环的部分,我应该这样做,还是有更好的方法? (我的阅读代码都发生在Connection Class里面)
在我再次开始等待连接的块的底部,对吗?
【问题讨论】:
标签: .net asynchronous named-pipes