2011-09-14 3 views
0

명명 된 파이프를 통해 통신하는 클라이언트와 서버를 작성하려고합니다. 서버에 쿼리를 보내고 서버가 응답을 보낼 수 있도록 클라이언트가 필요합니다. 웹상의 모든 샘플은 System.IO.pipes에 대해 클라이언트에 단일 문자열을 보내거나 서버에 단일 문자열을 보냅니다. 이전 Win32 파이프를 사용하여이를 수행하는 방법에 대한 몇 가지 예를 발견했습니다. 그러나, 나는 그것을 사용할 수 없다.요청을 보내고 system.io.pipes를 사용하여 응답받는 방법

여기까지 제가 지금까지 있습니다. 나는 그것이 많은 코드라는 것을 안다. 그러나 문제는 파이프 스트림의 생성자 중 하나에있을 수 있습니다.

클라이언트 코드 나 클라이언트를 시작할 때

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에 달려

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 

서버 코드() 서버가 ReceivingRequest에 달려(). 클라이언트를 죽이면 서버는 클라이언트가 보낸 요청을 읽습니다. 그러나 클라이언트가 더 이상 실행 중이 아니기 때문에 응답을 보내면 폭탄이 터집니다.

서버가 동일한 연결에서 메시지를 보내고받을 수 있습니까? 나는 이것이 PipeDirection.InOut이 의미하는 바라고 생각했다.

감사합니다,

마이크

답변