2011-09-14 144 views
0

我正在嘗試編寫通過命名管道進行通信的客戶端和服務器。我需要客戶端能夠發送查詢到服務器並讓服務器發送響應。 System.IO.pipes Web上的所有示例都將單個字符串發送到客戶端,或者將單個字符串發送到服務器。我發現了一些如何使用舊的Win32管道的例子。但是,我不能使用它。如何發送請求並使用system.io.pipes獲取響應

這是我到目前爲止。我知道這是很多代碼。但是,我認爲這個問題可能只是在管道流的其中一個構造函數中。

客戶端代碼

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的意思。

感謝,

邁克

回答

相關問題