Как отправить запрос и получить ответ, используя system.io.pipes

Я пытаюсь написать клиент и сервер, которые общаются через именованные каналы. Мне нужно, чтобы клиент мог отправить запрос на сервер и чтобы сервер отправил ответ. Все примеры в Интернете для System.IO.pipes отправляют одну строку клиенту или одну строку серверу. Я нашел несколько примеров того, как это сделать со старыми каналами Win32. Но я не могу это использовать.

Вот что у меня так далеко. Я знаю, что это много кода. Но я думаю, что проблема может заключаться только в одном из конструкторов потоковых потоков.

Код клиента

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

Код сервера

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.

Спасибо,

Майк

1 ответ

Вы можете посмотреть на привязку именованных каналов для WCF. Есть много хороших примеров того, как его использовать, например: http://www.jmedved.com/2010/03/named-pipes-in-wcf/

Другие вопросы по тегам