名前付きパイプ経由で通信するクライアントとサーバーを作成しようとしています。クライアントにクエリを送信して、サーバーに応答を送信させる必要があります。 Web上のSystem.IO.pipesのすべてのサンプルは、単一の文字列をクライアントに送信するか、単一の文字列をサーバーに送信します。私は古いWin32パイプでそれを行う方法のいくつかの例を見つけました。しかし、私はそれを使用することはできません。要求を送信して、system.io.pipesを使用して応答を得る方法
これまで私がこれまで持っていたことは次のとおりです。私はそれが多くのコードであることを知っています。しかし、私は問題がパイプストリームのコンストラクタの1つにあると考えています。
クライアントコード
Private Sub cmdSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSend.Click
Dim strMsg As String = ""
Dim strRequest As String = "Send Key"
Dim strErrMsg As String = ""
PrintText("Creating new pipe client")
'pipeStream = New NamedPipeClientStream(strPipeName)
'pipeStream = New NamedPipeClientStream(strPipeName, PipeDirection.InOut, 1, PipeTransmissionMode.Message, PipeOptions.None)
pipeStream = New NamedPipeClientStream(strServerName, strPipeName, PipeDirection.InOut, PipeOptions.None, Security.Principal.TokenImpersonationLevel.None)
PrintText("Connecting to server")
pipeStream.Connect()
pipeStream.ReadMode = PipeTransmissionMode.Message
PrintText("Sending request")
'Send Request
If SendPipeMessage(pipeStream, strRequest, strErrMsg) Then
Else
PrintText(strErrMsg)
End If
PrintText("Receiving response")
'Process Response
If ReadPipeMessage(pipeStream, strMsg, strErrMsg) Then
PrintText(strMsg)
Else
PrintText(strErrMsg)
End If
pipeStream.Dispose()
pipeStream = Nothing
End Sub
Private Function SendPipeMessage(ByRef pipeStream As NamedPipeClientStream, ByVal strMsg As String, ByRef strErrMsg As String) As Boolean
Dim blnRetVal As Boolean = True
Dim bytMessage() As Byte = Nothing
Dim encoding As UTF8Encoding = Nothing
Try
encoding = New UTF8Encoding
bytMessage = encoding.GetBytes(strMsg)
pipeStream.Write(bytMessage, 0, bytMessage.Length)
Catch ex As Exception
blnRetVal = False
strErrMsg = ex.ToString
End Try
Return blnRetVal
End Function
Private Function ReadPipeMessage(ByRef pipeStream As NamedPipeClientStream, ByRef strPipeText As String, ByRef strErrMsg As String) As Boolean
Dim blnRetVal As Boolean = True
Dim strTextChunck As String = ""
Dim intNumBytes As Integer = 0
Dim intNumChars As Integer = 0
Dim bytMessage(10) As Byte
Dim chars(10) As Char
Dim decoder As Decoder = Nothing
Try
decoder = Encoding.UTF8.GetDecoder
strPipeText = ""
Do
strTextChunck = ""
Do
intNumBytes = pipeStream.Read(bytMessage, 0, bytMessage.Length)
intNumChars = decoder.GetChars(bytMessage, 0, intNumBytes, chars, 0)
strTextChunck = strTextChunck & New String(chars, 0, intNumChars)
Loop While Not pipeStream.IsMessageComplete
strPipeText = strPipeText & strTextChunck
Loop While intNumBytes <> 0
Catch ex As Exception
blnRetVal = False
strErrMsg = ex.ToString
End Try
Return blnRetVal
End Function
Serverコード
Private Sub cmdListen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdListen.Click
bw.RunWorkerAsync()
End Sub
Private Sub bw_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles bw.DoWork
Dim strRequest As String = ""
Dim strErrMsg As String = ""
bw.ReportProgress(0, "Create new pipe server stream")
pipeStream = New NamedPipeServerStream(strPipeName, PipeDirection.InOut, 1, PipeTransmissionMode.Message, PipeOptions.None)
'Wait for connection
bw.ReportProgress(0, "Listening for connection")
pipeStream.WaitForConnection()
bw.ReportProgress(0, "Receiving request")
'Receive Request
If ReadPipeMessage(pipeStream, strRequest, strErrMsg) Then
bw.ReportProgress(0, "Request : " & strRequest)
Else
bw.ReportProgress(0, strErrMsg)
End If
'Get response
strResponse = GetResponse(strRequest)
bw.ReportProgress(0, "Sending response")
'Send Response
If SendPipeMessage(pipeStream, strResponse, strErrMsg) Then
bw.ReportProgress(0, "Sent response")
Else
bw.ReportProgress(0, strErrMsg)
End If
bw.ReportProgress(0, "Done!")
pipeStream.Disconnect()
pipeStream.Dispose()
pipeStream = Nothing
End Sub
Private Sub bw_ProgressChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles bw.ProgressChanged
PrintText(e.UserState)
End Sub
Private Sub bw_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles bw.RunWorkerCompleted
PrintText(e.Result)
End Sub
Private Function SendPipeMessage(ByRef pipeStream As NamedPipeServerStream, ByVal strMsg As String, ByRef strErrMsg As String) As Boolean
Dim blnRetVal As Boolean = True
Dim bytMessage() As Byte = Nothing
Dim encoding As UTF8Encoding = Nothing
Try
encoding = New UTF8Encoding
bytMessage = encoding.GetBytes(strMsg)
pipeStream.Write(bytMessage, 0, bytMessage.Length)
Catch ex As Exception
blnRetVal = False
strErrMsg = ex.ToString
End Try
Return blnRetVal
End Function
Private Function ReadPipeMessage(ByRef pipeStream As NamedPipeServerStream, ByRef strPipeText As String, ByRef strErrMsg As String) As Boolean
Dim blnRetVal As Boolean = True
Dim strTextChunck As String = ""
Dim intNumBytes As Integer = 0
Dim intNumChars As Integer = 0
Dim bytMessage(10) As Byte
Dim chars(10) As Char
Dim decoder As Decoder = Nothing
Try
decoder = Encoding.UTF8.GetDecoder
strPipeText = ""
Do
strTextChunck = ""
Do
intNumBytes = pipeStream.Read(bytMessage, 0, bytMessage.Length)
intNumChars = decoder.GetChars(bytMessage, 0, intNumBytes, chars, 0)
strTextChunck = strTextChunck & New String(chars, 0, intNumChars)
Loop While Not pipeStream.IsMessageComplete
strPipeText = strPipeText & strTextChunck
Loop While intNumBytes <> 0
Catch ex As Exception
blnRetVal = False
strErrMsg = ex.ToString
End Try
Return blnRetVal
End Function
私は、クライアントを起動すると、)(それは(ReadPipMessageでハング)と、サーバはReceivingRequestにハングアップします。私がクライアントを殺すと、サーバーはクライアントから送られた要求を読み込みます。しかし、クライアントがもはや実行されていないため、レスポンスを送信することに爆撃する。
サーバーが同じ接続でメッセージを送受信することはできませんか?私はそれがPipeDirection.InOutが意味するものだと思った。
おかげで、
マイク