I wrote a piece of code that will monitor a TCP/IP port for data that is sent out from another system upon completion of its task. This information is then placed in a database.
It runs fine but every once in awhile I get a 'An existing connection was forcibly closed by the remote host' and the program stops.
The code I'm using is:
Anyone know what can/is causing this and how to prevent it from happening? This database program is unmanned and I can't have it go down this often.
Thanks!!!
It runs fine but every once in awhile I get a 'An existing connection was forcibly closed by the remote host' and the program stops.
The code I'm using is:
VB.NET:
' Station #10 Torque RS232/Ethernet Logic
Private Sub Stn10TorqueConnectCallback(ByVal ar As IAsyncResult)
Stn10TorqueClientSocket.EndConnect(ar)
Dim Stn10TorqueBytes(4095) As Byte
Stn10TorqueClientSocket.BeginReceive(Stn10TorqueBytes, 0, Stn10TorqueBytes.Length, SocketFlags.None, AddressOf Stn10TorqueReceiveCallback, Stn10TorqueBytes)
End Sub
Private Sub Stn10TorqueReceiveCallback(ByVal ar As IAsyncResult)
Dim Stn10TorqueBytes() As Byte = CType(ar.AsyncState, Byte())
Dim Stn10TorqueNumBytes As Int32 = Stn10TorqueClientSocket.EndReceive(ar)
If Stn10TorqueNumBytes = 0 Then
Stn10TorqueClientSocket.Shutdown(SocketShutdown.Both)
Stn10TorqueClientSocket.Close()
Else
Stn10TorqueComInd.BackColor = Color.Green
Dim Stn10TorqueRecv As String = Stn10TorqueASCII.GetString(Stn10TorqueBytes, 0, Stn10TorqueNumBytes)
'Clear the buffer
Array.Clear(Stn10TorqueBytes, 0, Stn10TorqueBytes.Length)
'Store data
Dim Stn10TorqueDlg As New Stn10TorqueOneStringDelegate(AddressOf Stn10TorqueStoreReceivedData)
Dim Args() As Object = {Stn10TorqueRecv}
Me.Invoke(Stn10TorqueDlg, Args)
Stn10TorqueComInd.BackColor = Color.LightGray
'Begin Receive again
Stn10TorqueClientSocket.BeginReceive(Stn10TorqueBytes, 0, Stn10TorqueBytes.Length, SocketFlags.None, AddressOf Stn10TorqueReceiveCallback, Stn10TorqueBytes)
End If
End Sub
Private Sub Stn10TorqueStoreReceivedData(ByVal Data As String)
' Coded String:
' 1086 Sp:01-01 X.XX Nm XX
' Torque Angle
Dim DecodeTorque As String
Dim DecodeAngle As String
Dim DecodeLenght As Integer
DecodeLenght = Data.Length
If Mid(Data, 1, 1) <> "#" Then
DecodeTorque = Mid(Data, (DecodeLenght - 30), 15)
DecodeAngle = Mid(Data, (DecodeLenght - 15), 15)
If Stn10ValveTorqueLH = True Then
Stn10LHCoverScrewTextBox.Text = "Torque=" & RemoveSpaces(DecodeTorque) & " Angle=" & RemoveSpaces(DecodeAngle) & " deg"
Stn10ValveTorqueLH = False
ElseIf Stn10ValveTorqueRH = True Then
Stn10RHCoverScrewTextBox.Text = "Torque=" & RemoveSpaces(DecodeTorque) & " Angle=" & RemoveSpaces(DecodeAngle) & " deg"
Stn10ValveTorqueRH = False
End If
End If
End Sub
Private Delegate Sub Stn10TorqueOneStringDelegate(ByVal Data As String)
' Thread occupying private sub
Anyone know what can/is causing this and how to prevent it from happening? This database program is unmanned and I can't have it go down this often.
Thanks!!!