-
May 6th, 2009, 11:43 AM
#1
[RESOLVED] Using Namedpipes In 2008 vb .net - peek is hanging
Creating an application that uses both namedpipes and TCPIP connectivity to pass data between 2 legacy applications.
When there is actual data to pass everything works great. But when the application is looking to see if there is any data to read from the named pipe side and there is no data, the Peek hangs until there is data. Is there a problem in the way I have it coded or is there a timeout that needs to be set?
Code:
'
' declarations
'
Private in_pipeName As String
Private out_pipeName As String
Private dwMode, cbRead As Long
Private blnRecvPipeConnected As Boolean
Private blnSendPipeConnected As Boolean
Private blnRecvPortConnected As Boolean
Private blnSendPortConnected As Boolean
Private myTcpListener As TcpListener
Private mySTcpListener As TcpListener
Private myNetworkStream As NetworkStream
Private mySocket As TcpClient
Private mySocketSend As TcpClient
Private SocketStream As NetworkStream
Private ResponseStr As String = ""
Private bArray(BUFFSIZE) As Byte
Private sckin As Socket
Private namedpipe_out As NamedPipeClientStream
Private namedpipe_in As NamedPipeClientStream
Private fSuccess As Boolean
Public pipe_read_process As PipeStream
Private pipe_readmode As PipeTransmissionMode
Public pipe_write_process As PipeStream
'
' main processing function
'
Private Sub connections()
'
' connect named pipe connections to gateway
'
connecttopipenet()
Dim pipe_write_process As New StreamWriter(namedpipe_out)
Dim pipe_read_process As New StreamReader(namedpipe_in)
'
' connect TcpIP connections
'
connecttotcp()
'
' main processing loop
'
While True
sleep(1000)
Try
If (blnRecvPortConnected = True) And (blnSendPipeConnected = True) And (blnRecvPipeConnected = True) And (blnSendPortConnected = True) Then
sckin = mySocket.Client
'
' check to make sure that TCPIP port is available
'
If sckin.Poll(1, SelectMode.SelectRead) = False Then
RecordInfo(String.Concat("Still Connected"), EventLogEntryType.Information, 10003)
ElseIf sckin.Poll(1, SelectMode.SelectRead) Then
RecordInfo(String.Concat("Lost Connected"), EventLogEntryType.Information, 10003)
End If
SocketStream = mySocket.GetStream()
SocketStream.ReadTimeout = -1
If SocketStream.CanRead Then
If SocketStream.DataAvailable = True Then
Dim RecvData As Byte() = New Byte((mySocket.ReceiveBufferSize + 1) - 1) {}
Dim RecvBytes As Integer = SocketStream.Read(RecvData, 0, RecvData.Length)
Dim TranLen As Short = System.Net.IPAddress.NetworkToHostOrder(BitConverter.ToInt16(RecvData, 0))
ResponseStr = System.Text.Encoding.ASCII.GetString(RecvData, 2, RecvBytes - 2)
'
' determine if more to read
'
TranLen = CShort(TranLen - RecvBytes)
If TranLen > 0 Then
Try
RecvBytes = SocketStream.Read(RecvData, 0, RecvData.Length)
Catch ex As Exception
RecordInfo("Multiple Read Failed ", EventLogEntryType.Warning, 10003)
End Try
If RecvBytes <> 0 Then
ResponseStr = String.Concat(ResponseStr, System.Text.Encoding.ASCII.GetString(RecvData, 0, RecvBytes))
End If
End If
RecordInfo("In: " & ResponseStr, EventLogEntryType.Warning, 10003)
write_file("In: " & ResponseStr)
sendtogatewaynet(ResponseStr, RecvBytes)
ResponseStr = recvfromgatewaynet()
RecordInfo("Out: " & ResponseStr, EventLogEntryType.Warning, 10003)
If ResponseStr.Length > 0 Then
If sendtorelay(ResponseStr) = False Then
RecordInfo("send to relay failed ", EventLogEntryType.Warning, 10003)
End If
End If
Else
ResponseStr = recvfromgatewaynet()
If ResponseStr <> "" Then
RecordInfo("Out: " & ResponseStr, EventLogEntryType.Warning, 10003)
If ResponseStr.Length > 0 Then
If sendtorelay(ResponseStr) = False Then
RecordInfo("send to relay failed ", EventLogEntryType.Warning, 10003)
End If
End If
End If
End If
End If
End If
Catch ex As Exception
MessageBox.Show("Error " & ex.Message)
Exit While
End Try
End While
End Sub
'
' subroutine to connect to namedpipes
'
Friend Sub connecttopipenet()
out_pipeName = lblPipename.Text & "RP"
in_pipeName = lblPipename.Text & "SP"
If blnRecvPipeConnected = False Or blnSendPipeConnected = False Then
RecordInfo(String.Concat("Registering NamedPipe: ", lblPipename.Text), EventLogEntryType.Information, 10003)
Try
namedpipe_out = New NamedPipeClientStream(".", out_pipeName, PipeDirection.Out, PipeOptions.None)
namedpipe_out.Connect()
namedpipe_in = New NamedPipeClientStream(".", in_pipeName, PipeDirection.In, PipeOptions.None)
namedpipe_in.Connect()
blnRecvPipeConnected = True
blnSendPipeConnected = True
RecordInfo(String.Concat("NamedPipe: ", lblPipename.Text, " Connected ", hpipeS, ":", hpipeR), EventLogEntryType.Information, 10003)
ScreenInfo()
Catch ex As Exception
MessageBox.Show(ex.Message)
RecordInfo(String.Concat("NamedPipe Error: ", lblPipename.Text, " - ", ex.Message), EventLogEntryType.Information, 10003)
End Try
End If
End Sub
'
' subroutine to send transaction to named pipes
'
Private Sub sendtogatewaynet(ByRef transaction As String, ByRef translength As Integer)
Try
Dim pipe_write_process As New StreamWriter(namedpipe_out)
pipe_write_process.AutoFlush = True
pipe_write_process.WriteLine(transaction)
RecordInfo(String.Concat("Error after writefile: ", Err.LastDllError & ": " & APIErrorDescription(Err.LastDllError)), EventLogEntryType.Error, 10013)
Catch ex As Exception
RecordInfo(String.Concat("Send failed from namedPipe: ", out_pipeName, " - ", ex.Message), EventLogEntryType.Information, 10003)
End Try
End Sub
'
' function to return data from named pipes
'
Private Function recvfromgatewaynet() As String
Dim rtn As Integer
Dim returnvalue As Long = 0
Try
Dim enc As New System.Text.ASCIIEncoding()
Dim pipe_read_process = New StreamReader(namedpipe_in)
rtn = 0
rtn = pipe_read_process.Peek() <-------- hangs here if NO data on pipe to read.
If pipe_read_process.Read(mygatewayrecvbuffer, 0, BUFFSIZE) = Nothing Then
recvfromgatewaynet = 0
End If
recvfromgatewaynet = CStr(mygatewayrecvbuffer)
Catch ex As Exception
RecordInfo(String.Concat("Read failed from namedPipe: ", in_pipeName, " - ", ex.Message), EventLogEntryType.Information, 10003)
recvfromgatewaynet = False
End Try
End Function
Last edited by rknelsen; May 7th, 2009 at 03:06 PM.
-
May 6th, 2009, 05:51 PM
#2
Re: Using Namedpipes In 2008 vb .net - peek is hanging
Try a catch block AFTER this line, and then another for the next parts. It should exit right here:
Code:
namedpipe_in.Connect()
Also, go back and add CODE TAGS to your code (see above). Too hard to read unstructured code.
-
May 7th, 2009, 03:08 PM
#3
Re: Using Namedpipes In 2008 vb .net - peek is hanging
Ok I changed the connect piece to be in a try catch block but the namedpipe_in.connect() does not fail.
Code:
Public Sub connecttopipenet()
out_pipeName = lblPipename.Text & "RP"
in_pipeName = lblPipename.Text & "SP"
If blnRecvPipeConnected = False Or blnSendPipeConnected = False Then
RecordInfo(String.Concat("Registering NamedPipe: ", lblPipename.Text), EventLogEntryType.Information, 10003)
Try
namedpipe_out = New NamedPipeClientStream(".", out_pipeName, PipeDirection.Out, PipeOptions.None)
namedpipe_out.Connect()
Catch ex As Exception
MessageBox.Show(ex.Message)
RecordInfo(String.Concat("NamedPipe Error: ", lblPipename.Text, " - ", ex.Message), EventLogEntryType.Information, 10003)
End Try
Try
namedpipe_in = New NamedPipeClientStream(".", in_pipeName, PipeDirection.In, PipeOptions.None)
namedpipe_in.Connect()
Catch ex As Exception
MessageBox.Show(ex.Message)
RecordInfo(String.Concat("NamedPipe Error: ", lblPipename.Text, " - ", ex.Message), EventLogEntryType.Information, 10003)
End Try
Try
blnRecvPipeConnected = True
blnSendPipeConnected = True
RecordInfo(String.Concat("NamedPipe: ", lblPipename.Text, " Connected ", hpipeS, ":", hpipeR), EventLogEntryType.Information, 10003)
ScreenInfo()
Catch ex As Exception
MessageBox.Show(ex.Message)
RecordInfo(String.Concat("NamedPipe Error: ", lblPipename.Text, " - ", ex.Message), EventLogEntryType.Information, 10003)
End Try
End If
End Sub
-
May 7th, 2009, 11:06 PM
#4
Re: Using Namedpipes In 2008 vb .net - peek is hanging
-
May 8th, 2009, 09:42 AM
#5
Re: Using Namedpipes In 2008 vb .net - peek is hanging
CalledNamePipe appears to write and read to the same pipe.
In my application I need to write to 1 pipe and read from another pipe as this is the way the legacy program works.
so would need to write to something like \\.\pipe\pipeSP and read from pipe \\.\pipe\pipeRP.
-
May 8th, 2009, 05:54 PM
#6
Re: Using Namedpipes In 2008 vb .net - peek is hanging
-
May 11th, 2009, 03:51 PM
#7
Re: Using Namedpipes In 2008 vb .net - peek is hanging
Thanks.
Changing to Async, by specifing FILE_FLAG_OVERLAPPED in hpipeS = CreateFile(out_pipeName, GENERIC_WRITE, 0, sa, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, FILE_FLAG_OVERLAPPED), resolved this issue.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|